Java EE 6 Injection into an Abstract Class

In the Java EE 6 project I'm working on, there is one field annotated with @EJB that is not entered. Injection works great everywhere.

As a newbie to Java EE, I don’t know if this is due to a field in an abstract class, and I can not find any conclusion from Glassfish (3.1.2) about why this injection does not occur.

There are no errors or warnings in the server log until a NullPointerException is raised because the dataSourceControl field is null. I checked that an instance of the DataSourceControl Singleton is created by entering a constructor into it.

As far as I can tell, the dataSourceControl field is not entered, but the logs give me no reason why this is so.

public abstract class AbstractDataMap<T> { @EJB private DataSourceControl dataSourceControl; // This is not being injected DataSourceControl getDataSourceControl() { return dataSourceControl; } // Other methods } public abstract class AbstractDataMapDBProd<T> extends AbstractDataMap<T> { @Override protected Connection getDBConnection() { return getDataSourceControl().getConnectionX(); // NullPointerException here } // Other methods } @Stateless public class CountryMap extends AbstractDataMapDBProd<Country> { public boolean update(final Country current, final Country legacy) { Connection connection = getDBConnection(); // More code 'n stuff } } 

Are there any rules that I skipped regarding the injections that are defined in the abstract class?

Anything else that screams "noob"?

If there are no obvious errors, any ideas on how to debug this?

+7
source share
2 answers

Injection will work in any class (base class, superclass, abstract superclass, etc.). However, the injection will only work until you get the CountryMap instance from the container (i.e. injection or search), and not through the new CountryMap . How do you get a copy of CountryMap ?

+11
source

Injection works great everywhere.

Since the container is responsible for injection in managed classes, the @EJB annotation will not work with abstract classes. You need to manually search for EJB through JNDI.

0
source

All Articles