焊接生产者方法不注入

编程入门 行业动态 更新时间:2024-10-09 01:14:31
焊接生产者方法不注入 - NPE(Weld producer methods not injecting - NPE)

我试图创建一个使用具有生成器方法的类的简单示例,目的是生成器方法注入依赖项而不是Weld尝试创建bean。 这就是为什么我@Vetoed DefaultEngine我也希望坚持@Default @Any限定符

所以我有以下内容

Engine.java

public interface Engine { void start(); void stop(); }

DefaultEngine.java

@Vetoed public class DefaultEngine implements Engine { public void start() { System.out.println("Cough cough vrummmmm"); } public void stop() { System.out.println("Phhhut clank"); } }

Car.java

public class Car { @Inject private Engine engine; public void startCar() { engine.start(); } public void stopCar() { engine.stop(); } }

App.java

public class App { public static void main(@Observes ContainerInitialized event) { Car car = new Car(); car.startCar(); car.stopCar(); } }

EngineProducer.java

public class EngineProducer { @Produces public Engine getEngine() { return new DefaultEngine(); } }

我有beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_1.xsd"> </beans>

但似乎Engine没有注入,因为我收到以下内容。

Exception in thread "main" java.lang.NullPointerException at com.test.testing.Car.startCar(Car.java:12) at com.test.testing.App.main(App.java:12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:88) at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:78) at org.jboss.weld.injection.MethodInvocationStrategy$SimpleMethodInvocationStrategy.invoke(MethodInvocationStrategy.java:129) at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:308) at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:274) at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:264) at org.jboss.weld.event.ObserverNotifier.notifySyncObservers(ObserverNotifier.java:271) at org.jboss.weld.event.ObserverNotifier.notify(ObserverNotifier.java:260) at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:154) at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:136) at org.jboss.weld.manager.BeanManagerImpl.fireEvent(BeanManagerImpl.java:673) at org.jboss.weld.environment.se.WeldContainer.initialize(WeldContainer.java:142) at org.jboss.weld.environment.se.Weld.initialize(Weld.java:544) at org.jboss.weld.environment.se.StartMain.go(StartMain.java:44) at org.jboss.weld.environment.se.StartMain.main(StartMain.java:53)

I am trying to create a simple example of using classes that have producer methods, with the aim that the producer method injects the dependency instead of Weld trying to create the bean. Which is why I've @Vetoed the DefaultEngine I also wish to stick with the @Default @Any qualifiers

So I have the following

Engine.java

public interface Engine { void start(); void stop(); }

DefaultEngine.java

@Vetoed public class DefaultEngine implements Engine { public void start() { System.out.println("Cough cough vrummmmm"); } public void stop() { System.out.println("Phhhut clank"); } }

Car.java

public class Car { @Inject private Engine engine; public void startCar() { engine.start(); } public void stopCar() { engine.stop(); } }

App.java

public class App { public static void main(@Observes ContainerInitialized event) { Car car = new Car(); car.startCar(); car.stopCar(); } }

EngineProducer.java

public class EngineProducer { @Produces public Engine getEngine() { return new DefaultEngine(); } }

And I have the beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_1.xsd"> </beans>

But it seems the Engine is not injected as I receive the following.

Exception in thread "main" java.lang.NullPointerException at com.test.testing.Car.startCar(Car.java:12) at com.test.testing.App.main(App.java:12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:88) at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:78) at org.jboss.weld.injection.MethodInvocationStrategy$SimpleMethodInvocationStrategy.invoke(MethodInvocationStrategy.java:129) at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:308) at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:274) at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:264) at org.jboss.weld.event.ObserverNotifier.notifySyncObservers(ObserverNotifier.java:271) at org.jboss.weld.event.ObserverNotifier.notify(ObserverNotifier.java:260) at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:154) at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:136) at org.jboss.weld.manager.BeanManagerImpl.fireEvent(BeanManagerImpl.java:673) at org.jboss.weld.environment.se.WeldContainer.initialize(WeldContainer.java:142) at org.jboss.weld.environment.se.Weld.initialize(Weld.java:544) at org.jboss.weld.environment.se.StartMain.go(StartMain.java:44) at org.jboss.weld.environment.se.StartMain.main(StartMain.java:53)

最满意答案

OK找到了答案,看来Weld无法管理用new创建的bean也无法将bean实例注入容器事件的观察者。 所以主要的App.java必须改为

public class App { @Inject private Car car; public void main(@Observes ContainerInitialized event) { car.startCar(); car.stopCar(); } }

OK found the answer, it appears Weld cannot manage beans created with new also it cannot inject bean instances into observers of container events. So the main App.java has to change to

public class App { @Inject private Car car; public void main(@Observes ContainerInitialized event) { car.startCar(); car.stopCar(); } }

更多推荐

本文发布于:2023-08-04 18:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1419222.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:生产者   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!