我正在尝试在 java SE 中建立一个非常简单的焊接实现。
我有扩展类:
public class MyExtension implements Extension {
void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) {
System.out.println("Starting scan...");
}
<T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> annotatedType, BeanManager beanManager) {
System.out.println("Scanning type: " + annotatedType.getAnnotatedType().getJavaClass().getName());
}
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd) {
System.out.println("Finished the scanning process");
}
public void main(@Observes ContainerInitialized event) {
System.out.println("Starting application");
new Test();
}
}
然后我有一个要注入的简单类:
public class SimpleClass {
public void doSomething() {
System.out.println("Consider it done");
}
}
最后是我想注入的类:
public class Test {
@Inject
private SimpleClass simple;
@PostConstruct
public void initialize() {
simple.doSomething();
}
@PreDestroy
public void stop() {
System.out.println("Stopping");
}
}
结果输出是:
80 [main] INFO org.jboss.weld.Version - WELD-000900 1.1.10 (Final)
272 [main] INFO org.jboss.weld.Bootstrap - WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Starting scan...
Scanning type: test.Test
Scanning type: test.SimpleClass
Scanning type: test.MyExtension
640 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
640 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled
Finished the scanning process
Starting application
我希望在构造 Test() 时注入简单的类,并调用应该输出预期文本的 postconstruct 方法。
我到底做错了什么?