chris21k
chris21k

Reputation: 241

@Inject delivers null in Weld 3 using Java SE

I am using CDI 2.0 with Weld 3.0.0 final (complete weld-se-shaded.jar in the classpath) in a plain Java SE 8 program, as shown below. What is wrong with it or am I missing something, since @Inject does nothing, i.e., the references stay null? Programmatic access as illustrated works.

I thought to post this into JBoss/Weld Jira Bug tracking system. However, after registration and login, I cannot find the button to create a new entry.

Thanks for your help,

StartUp.java

import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;

public class StartUp {

    public static void main(String[] args) {
        // start CDI Container
        SeContainerInitializer initializer = SeContainerInitializer.newInstance();        
        try (SeContainer container = initializer.initialize()) {
            Test t = new Test();

            // start tests
            t.test(container);
        }
    }

}

Test.java

import javax.enterprise.event.Event;
import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.spi.CDI;
import javax.inject.Inject;

public class Test {

    @Inject
    // IMyBean myBean;    // does not work
    MyBean myBean;    // does not work

    @Inject
    Event<UserEvent> event;     // does not work

    public void test(SeContainer container) {
        myBean.greete("World");               // NullPointerException

        // manual lookup
        // MyBean myBean2 = container.select(MyBean.class).get();
        IMyBean myBean2 = CDI.current().select(IMyBean.class).get();
        myBean2.greete("World");

        event.fire(new UserEvent("info"));    // NullPointerException
    }

}

MyBean.java

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Named
@ApplicationScoped
public class MyBean implements IMyBean {

    public void greete(String s) {
        System.out.println("Hello, " + s + "!");
    }

}

SyncEventObserver.java

import javax.enterprise.event.Observes;

public class SyncEventObserver {

    public void observeUserEvent(@Observes UserEvent userEvent) {
        System.out.println("Received event:" + userEvent.getMessage());
    }

}

UserEvent.java

public class UserEvent {
    private String message;

    public UserEvent() {
    }

    public UserEvent(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

}

META-INF/beans.xml

<beans version="2.0" bean-discovery-mode="all"
       xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                           http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd">

</beans>

IMyBean.java

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Named
@ApplicationScoped
public interface IMyBean {

    void greete(String s);

}

Upvotes: 3

Views: 1253

Answers (1)

John Ament
John Ament

Reputation: 11723

Your main method should look like this

public static void main(String[] args) {
    // start CDI Container
    SeContainerInitializer initializer = SeContainerInitializer.newInstance();        
    try (SeContainer container = initializer.initialize()) {
        Test t = container.select(Test.class).get();

        // start tests
        t.test(container);
    }
}

The reason being - you have injection points in your test class, but you instantiate it. That removes CDI from managing your beans.

Upvotes: 1

Related Questions