Spring:@Resource 注入在 JDK9 下停止工作 [英] Spring: @Resource injection stopped working under JDK9
问题描述
在我的 @Configuration
类中,我有如下依赖项:
In my @Configuration
class I have dependendies like the following:
@Configuration
public class MyConfig {
@Resource(name = "firstDataSource")
private DataSource firstDataSource;
// more code
}
依赖注入在 Oracle JDK 8 中有效:firstDataSource
字段已成功注入非空值.
The dependency injection worked in Oracle JDK 8: firstDataSource
field was successfully injected with a non-null value.
现在我尝试在 JDK 9 中运行应用程序(没有修改).结果是 @Resource
不再触发依赖注入:所有用这个注解注解的东西都保持 null代码>.
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
.
@Resource
停止工作的原因可能是什么?
What could be the reason for @Resource
to stop working?
项目使用的是Spring 4.0.9.
Spring 4.0.9 is used in the project.
这是一个演示问题的测试项目:https://github.com/rpuch/test-spring-injection-jdk9
Here is a test project demonstrating the problem: https://github.com/rpuch/test-spring-injection-jdk9
它包含一个测试:MainTest
,我从我的 IDE 中运行它.当我使用 JDK 8 时,它输出
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
但是在JDK 9下,它在启动时抛出异常,原因如下:
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.
推荐答案
jdk9 中不推荐使用的 java.xml.ws.annotation
可能是您面临的问题的原因.由于注释 @Resource
来自同一模块导出的包 javax.annotation
.
The java.xml.ws.annotation
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.
您可能希望提供 javatm-common-annotations 作为 的可升级模块导出 javax.annotation 的 java.xml.ws.annotation
.
You might want to provide javatm-common-annotations as an upgradeable module for java.xml.ws.annotation
which exports the javax.annotation.
Java(TM) 通用注释的这个独立版本使用 java.annotation 的 Java 平台模块系统自动"模块名称,以匹配JDK 9 中使用的模块名称.
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.
在 central 上的搜索提示,你可以使用:-
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.1</version>
</dependency>
使用您的共享代码,这在我的机器上运行良好.
This runs fine on my machine with your shared code.
另外,关于您共享的代码的注释.由于您要迁移到使用 JDK9,您应该迁移到使用最新的 springframework 依赖关系 于 28-9-2017 发布:-
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:-
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
编辑:一定要看看Roman 的回答替代使用 --add-modules
以防您不打算迁移到 javaee
爆炸模块.
Edit : Do take a look at Roman's answer for an alternate to make use of --add-modules
in case you are not planning to migrate to the javaee
explosed module yet.
这篇关于Spring:@Resource 注入在 JDK9 下停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!