In my @Configuration
class I have dependendies like the following:
@Configuration
public class MyConfig {
@Resource(name = "firstDataSource")
private DataSource firstDataSource;
// more code
}
The dependency injection worked in Oracle JDK 8: firstDataSource
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 @Resource
does not trigger dependency injection anymore: everything annotated with this annotation remains null
.
What could be the reason for @Resource
to stop working?
Spring 4.0.9 is used in the project.
Here is a test project demonstrating the problem: https://github.com/rpuch/test-spring-injection-jdk9
It contains a single test: MainTest
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
OK
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>(Bean2.java:7)
at Config2.bean2(Config2.java:16)
which happens when the dependency is not injected.