Why can you not use @Inject in POJOs instantiated with new

Solution Verified - Updated -

Issue

I have @Stateless object which I would like to inject:

@Stateless
public class MyEJB {
    public String myMethod(){
        String res = "myMethod";
        System.out.println(res);
        return res;
    }
}

When I try to inject it into a POJO, it does not work and is null.

public class MyPojo {
    @Inject MyEJB myEjb;

    public String myPojoMethod(){
        String res = "myPojoMethod .. ejb = " + myEjb;
        System.out.println(res);
        return res;
    }
}

Why? I do not think that Java EE spec puts any requirements on lifecycle of objects that use @Inject?

Environment

  • JBoss Enterprise Application Platform (EAP) 6

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.