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, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content