wikk
wikk

Reputation: 175

Resource injection not work

I create new DataSource to MySQL in administration console. (Name: MySqlDS, JNDI: java:/jdbc/MySqlDB)

If I load it by:

initialContext = new InitialContext();
dataSource = (DataSource) initialContext.lookup("java:/jdbc/MySqlDB");
connection = dataSource.getConnection();`

It work correctly.

But if I load it by:

@Resource(lookup="java:/jdbc/MySqlDB")
private static DataSource dataSource;

It's not working and dataSource is null.

Other info:

Upvotes: 1

Views: 2053

Answers (1)

wikk
wikk

Reputation: 175

Well, the problem is solved. It was my mistake.

Dependency injection works only if a container manages of lifecycle of beans. And the container will embed dependencies in this case.

My mistake: I created new instance of bean manually instead of using @EJB annotation. It was on the top of the hierarchy of dependencies. Thats why I lost sight of it. And container can't fill all dependencies for all nested injected beans.

Upvotes: 1

Related Questions