Roman Puchkovskiy Roman Puchkovskiy - 3 years ago 202
Java Question

Spring: @Resource injection stopped working under JDK9

In my

class I have dependendies like the following:

public class MyConfig {
@Resource(name = "firstDataSource")
private DataSource firstDataSource;

// more code

The dependency injection worked in Oracle JDK 8:
field was successfully injected with a non-null value.

Now I tried to run the application (with no modifications) in JDK 9. The result is that
does not trigger dependency injection anymore: everything annotated with this annotation remains

What could be the reason for
to stop working?

Spring 4.0.9 is used in the project.

Here is a test project demonstrating the problem:

It contains a single test:
which I run from my IDE. When I use JDK 8, it outputs

сен 29, 2017 10:45:13 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5f8ed237: startup date [Fri Sep 29 22:45:13 SAMT 2017]; root of context hierarchy

But under JDK 9, it throws an exception during startup which is caused by the following:

Caused by: java.lang.IllegalStateException: bean1 is not injected
at Bean2.<init>(
at Config2.bean2(

which happens when the dependency is not injected.

Answer Source

The being deprecated in jdk9 is possibly the cause of what you're facing. Since the annotation @Resource is from the package javax.annotation exported by the same module.

You might want to provide javatm-common-annotations as an upgradeable module for which exports the javax.annotation.

This standalone release of Java(TM) Common Annotations uses a Java Platform Module System "automatic" module name of java.annotation, to match the module name used in JDK 9.

The search on central suggests, you can use:-


This runs fine on my machine with your shared code.

Also, a note about the code that you've shared. Since you're migrating to using JDK9, you should migrate to using latest springframework dependencies released as on 28-9-2017:-

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download