焊接生产者方法不注入 - NPE

时间:2015-10-26 09:00:04

标签: java cdi weld

我正在尝试创建一个使用具有生成器方法的类的简单示例,目的是生成器方法注入依赖项而不是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)

1 个答案:

答案 0 :(得分:1)

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

public class App 
{
    @Inject
    private Car car;

    public void main(@Observes ContainerInitialized event)
    {
        car.startCar();
        car.stopCar();
    }
}