简体   繁体   中英

How to use java ee 6 @Resource annotation

The java ee 6 api has an annotation @Resource with 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.

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

TIA

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

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

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.

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.

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

<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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM