Spring:@Resource注入在JDK9下停止工作 [英] Spring: @Resource injection stopped working under JDK9

查看:246
本文介绍了Spring:@Resource注入在JDK9下停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 @Configuration 课程中,我有如下的dependendies:

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.

推荐答案

java.xml。 ws.annotation 在jdk9中被弃用可能是你所面临的原因。由于注释 @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-注释作为 java.xml.ws.annotation ,它导出javax.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)Common Annotations使用Java平台模块系统自动模块名称java.annotation,以匹配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.

搜索中心建议,你可以使用: -

The search on central suggests, you can use:-

<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 依赖项于2017年9月28日发布: -

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>

编辑 :请看一下罗马的答案,供替代人员使用 - 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆