java 如何使用java ee 6 @Resource注解

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2495271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:28:33  来源:igfitidea点击:

How to use java ee 6 @Resource annotation

javajakarta-ee

提问by javamonkey79

The java ee 6 api has an annotation @Resourcewith an attribute 'lookup', however, so does the java se 6 api (here). However, since java ee 6 is dependent on java se 6, it seems you can not get at the ee version of the annotation and the 'lookup' attribute.

java ee 6 api 有一个带有属性“lookup”的注释@Resource,但是,java se 6 api(这里)也是如此。但是,由于 java ee 6 依赖于 java se 6,因此您似乎无法获得 ee 版本的注释和“查找”属性。

Is this a bug or is there some other way to use this annotation that I am missing.

这是错误还是有其他方法可以使用我缺少的此注释。

TIA

TIA

回答by Pascal Thivent

Your JDK (and mine) doesn't have the latest version of the javax.annotation.Resourcefrom the JSR-250. To use the latest version during compilation, you'll have to override the compiler's endorsed directory (e.g. to point to your glassfishv3 endorsed directory):

您的 JDK(和我的)没有javax.annotation.Resource来自JSR-250的最新版本。要在编译期间使用最新版本,您必须覆盖编译器的认可目录(例如,指向您的 glassfishv3 认可目录):

-Djava.endorsed.dirs=${GLASSFISH_HOME}/modules/endorsed

回答by skaffman

It's the same class in both cases (javax.annotation.Resource). It's in both sets of API docs for convenience only. Actual JavaEE 6 implementations will just use the class from JavaSE 6.

在这两种情况下都是同一个类 ( javax.annotation.Resource)。只是为了方便起见,它都在两组 API 文档中。实际的 JavaEE 6 实现将只使用 JavaSE 6 中的类。

回答by LeChe

Thread necro at its best, but for my taste - trying to do things clean and neat - the approach of javamonkey79 is just too much of a hack.

Thread necro 处于最佳状态,但就我的口味而言 - 试图将事情做得干净整洁 - javamonkey79 的方法太过分了。

This is what I put in my pom.xml to make it work:

这是我放入 pom.xml 以使其工作的内容:

<profiles>
        <profile>
            <id>endorsed</id>
            <activation>
                <property>
                    <name>sun.boot.class.path</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <configuration>
                            <!-- javaee6 contains upgrades of APIs contained within the JDK itself.
                                 As such these need to be placed on the bootclasspath, rather than classpath of the
                                 compiler.
                                 If you don't make use of these new updated API, you can delete the profile.
                                 On non-SUN jdk, you will need to create a similar profile for your jdk, with the similar property as sun.boot.class.path in Sun's JDK.-->
                            <compilerArguments>
                                <bootclasspath>${settings.localRepository}/javax/javaee-endorsed-api/6.0/javaee-endorsed-api-6.0.jar${path.separator}${sun.boot.class.path}</bootclasspath>
                            </compilerArguments>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>6.0</version>
                            </dependency>
                        </dependencies>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

By the way, I found this here. Thanks a lot, Frederik!

顺便说一下,我在这里找到了这个。非常感谢,弗雷德里克!