如何在Java 9中运行时访问javax.annotation.Resource [英] How to get access to javax.annotation.Resource at run time in Java 9

查看:1881
本文介绍了如何在Java 9中运行时访问javax.annotation.Resource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试:

public class ResourceTest {
    @Test
    public void test() throws ClassNotFoundException {
        Class.forName("javax.annotation.Resource");
    }
}

它试图访问 javax .annotation.Resource 。在java 8中它可以工作,但是在java 9中(我使用的是Oracle JDK 9),它失败了 ClassNotFoundException
如此处所述 Spring:@Resource注射停止工作在JDK9 下,来自JDK的 javax.annotation.Resource 在Java 9中默认不可用。

It tries to access javax.annotation.Resource. In java 8 it worked, but in java 9 (I'm using Oracle JDK 9) it fails with ClassNotFoundException. As explained here Spring: @Resource injection stopped working under JDK9 , javax.annotation.Resource from the JDK is not available by default in Java 9.

我正在尝试使用模块描述符访问它:

I'm trying to get access to it using module descriptor:

module test {
    requires java.xml.ws.annotation;
    requires junit;
}

在这里,我特意请求访问 java.xml .ws.annotation 模块(包含 javax.annotation.Resource )。但测试仍然失败。

Here, I specifically request access to java.xml.ws.annotation module (which contains javax.annotation.Resource). But the test still fails.

当我删除需要子句并添加依赖项(作为库)时包含 javax.annotations.Resource ,它有效:

When I remove that requires clause and add a dependency (as a library) which contains javax.annotations.Resource, it works:

    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.1</version>
    </dependency>

当我同时添加它们时(Maven依赖于 pom.xml 需要java.xml.ws.annotation ),IDEA中的编译失败,并显示以下消息:

When I add them both (Maven dependency in pom.xml and requires java.xml.ws.annotation), compilation in IDEA fails with the following message:

the unnamed module reads package javax.annotation from both java.xml.ws.annotation and java.annotation

但是Maven构建仍然成功!

But Maven build still succeeds!

如果我添加 java.xml.ws.annotation 模块通过命令行,它可以工作(没有Maven依赖, 需要子句):

If I add java.xml.ws.annotation module via command line, it works (with no Maven dependency and with requires clause):

mvn clean test -DargLine="--add-modules java.xml.ws.annotation"

我的模块描述有问题吗?如何在不使用命令行开关的情况下访问JDK提供的 javax.annotation.Resource

Do I do something wrong with my module description? How can I get access to JDK-supplied javax.annotation.Resource without command line switches?

测试项目可在 https://github.com/rpuch/test-resource-jdk9 上找到/ p>

The test project is available at https://github.com/rpuch/test-resource-jdk9

推荐答案

这里只是为了清除一些困惑。您在问题中陈述的工作方式是替代方案,不应该像您已经看到的那样进行组合。

Just to clear out some confusion here. The ways to work stated in the question by you are alternatives and should not be combined as you have already seen.


未命名的模块读取包来自
java.xml.ws.annotation和java.annotation的javax.annotation

the unnamed module reads package javax.annotation from both java.xml.ws.annotation and java.annotation






所以它的工作方式是:


So the way it would work is either:

你可以使用编译器args来添加模块

You can use the compiler args to add modules

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <release>9</release>
        <compilerArgs>
            <arg>--add-modules</arg>
            <arg>java.xml.ws.annotation</arg>
        </compilerArgs>
    </configuration>
</plugin>



OR



使用 javax.xml.ws.annotation 作为可升级模块,即可以使用依赖项

OR

Make use of the javax.xml.ws.annotation being an upgradeable module which is when you can make use of the dependency

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.1</version>
</dependency>

理想情况下,这是一个可以选择的选择,因为前者只是替代使用< a href =https://docs.oracle.com/javase/9​​/docs/api/java.xml.ws.annotation-summary.html\"rel =noreferrer> @ Deprecated 模块标记为 forRemoval

Ideally this would be a preferrable option to stick to as the former is just an alternate to use the @Deprecated module marked forRemoval.


所以必需条款本身并不足以访问
模块......对于所有JDK提供的模块(不包括
java.base)都是如此,或者对于不推荐使用的模块是否也是如此?

So the required clause by itself it not enough to get access to a module... is this true for all JDK-supplied modules (excluding java.base), or it is only true for deprecated modules?

不,要求只是声明的一部分。 [想想这个,在JDK 9之前,如果你在你的类中使用了一个语句 import some.foo.bar; ,那么它没有被添加为库(类路径)工作?]。标记为必需的模块必须位于模块路径上才能访问它。

No, the requires is just a part of declaration. [Think about this, prior to JDK 9 if you used a statement import some.foo.bar; in your class which was not added as a library(classpath) would that have worked?]. The module marked as required has to be on the modulepath for you to access it.

更新 - 使用JDK / 11或更高版本时,不会再支持第一个选项,其中JEP为删除Java EE和CORBA模块

这篇关于如何在Java 9中运行时访问javax.annotation.Resource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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