Ted Spradley
Ted Spradley

Reputation: 3436

Injection of @Startup @Singleton bean instance is null - Tomee Webprofile 1.6.0.2

My instance of a @Singleton @Startup bean that Tomee is successfully creating, starting and placing in 'My Singleton Container' is null when I try to use it in a POJO. I've tried with and without the @ManagedBean anotation for the POJO. Have I missed something basic in the spec or tutorial?

Many thanks in advance, Ted S.

Tomee startup log:

Dec 21, 2014 2:48:24 PM org.apache.openejb.assembler.classic.Assembler startEjbs
INFO: Started Ejb(deployment-id=MyStartupBean, ejb-name=MyStartupBean, container=My Singleton Container)

MyStartupBean.java:

@Startup
@Singleton
public class MyStartupBean
{
    private final Properties _companyNames = new Properties();

    @PostConstruct
    public void init() 
    {   
        _companyNames.put("key1", "ABC Company");
        _companyNames.put("key2", "XYZ Company");
     }

    public Properties getCompanyNames()
    {
        return _companyNames;
     }
}

MyPojo.java:

public class MyPojo
{
    @EJB
    private MyStartupBean _startupBean;
    private String _companyName;    

    public MyPojo(String inputKey)
    {
        Properties companyNames = _startupBean.getCompanyNames(); // <== _startupBean is null
        String name = companyNames.getProperty(inputKey);
        setCompanyName(name);
    }

    public void setCompanyName(String name)
    {
        _companyName = name;
    }

    public String getCompanyName()
    {
        return _companyName;
    }
}

Upvotes: 0

Views: 1395

Answers (1)

John Ament
John Ament

Reputation: 11723

You're probably instantiating your pojo aren't you, e.g.

MyPojo mp = new MyPojo(someInput);

When you do that, injection doesn't work. You need to use managed references to work with CDI/EJB components.

Upvotes: 1

Related Questions