CDI 容器 - Weld SE
HelloWorld 示例
本示例演示在 Weld SE 容器中注入一个 HelloWorld Bean,以及从 Weld SE 容器中获取该 Bean 并运行相应的方法。
HelloWorld 类
import javax.inject.Singleton;
@Singleton
public class HelloWorld {
public void sayHello(){
System.out.println("Hello World");
}
}
注入和运行该 HelloWorld Bean:
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
public class Main {
public static void main(String[] args) throws Exception {
Weld weld = new Weld();
WeldContainer container = weld.initialize();
HelloWorld test = container.select(HelloWorld.class).get();
test.sayHello();
container.shutdown();
}
}