Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java EE 6 Injection Into Abstract Class

In a Java EE 6 project I am working on, there is a lone field annotated with @EJB that is not being injected. Injection is working fine everywhere else.

Being new to Java EE, I don't know if it is related to the field being in an abstract class, nor can I find any output from Glassfish (3.1.2) about why this injection is not occurring.

There are no errors or warnings in the server log until the NullPointerException occurs because the dataSourceControl field is null. I have verified that the DataSourceControl Singleton is being instantiated by putting logging in it's constructor.

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

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 I have missed regarding injection that is defined in an abstract class?

Anything else that cries 'noob'?

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

like image 631
rusty_turkey Avatar asked May 02 '12 00:05

rusty_turkey


People also ask

Can I inject into abstract class?

First, an abstract class isn't component-scanned since it can't be instantiated without a concrete subclass. Second, setter injection is possible in an abstract class, but it's risky if we don't use the final keyword for the setter method. The application may not be stable if a subclass overrides the setter method.

What does @inject do in Java?

Injectable constructors are annotated with @Inject and accept zero or more dependencies as arguments. @Inject can apply to at most one constructor per class. @Inject is optional for public, no-argument constructors when no other constructors are present. This enables injectors to invoke default constructors.

Can we use abstract class instead of interface in dependency injection?

As always, the answer is: "It depends." You can't do these approaches with interfaces as interfaces only specify the contract and don't have implementation. If the contract is all you need, then go with an interface. If you need code to be shared by the subclasses, then go with an abstract class.

Can we declare Datamember as abstract?

Abstract classes in Java are classes which cannot be instantiated, meaning we cannot create new instances of an abstract class. The purpose of an abstract class is to function as a base for subclasses. but why java allows to define data member in an abstract class, where we can not create object of an abstract class.


1 Answers

Injection will work in any class (base class, superclass, abstract superclass, etc.). However, injection will only work so long as you obtain an instance of CountryMap from the container (i.e., injection or lookup) rather than via new CountryMap. How are you obtaining an instance of CountryMap?

like image 94
Brett Kail Avatar answered Sep 29 '22 21:09

Brett Kail