导入javax.annotation。*无法在Eclipse的Java 10编译器中解析
在我的机器(Windows 10)中,有两个版本的Java,Java 1.8(JRE和JDK)和Java 10(JRE和JDK)。
In my machine (Windows 10), there are two versions of Java, Java 1.8 (JRE and JDK) and Java 10 (JRE and JDK).
以前 IF 我将 Eclipse 设置为:
- Java编译器(JDK Complience):1.8
- Java构建路径(JRE系统库):1.8
- Java Compiler (JDK Complience) : 1.8
- Java Build Path (JRE System Libraries) : 1.8
那么我使用以下Spring代码
THEN IF i use following Spring code
import javax.annotation.PostConstruct;
...
...
...
@PostConstruct
...
...
一切正常很好。完全没有错误。
Everything works fine. No error at all.
但是,如果我将Eclipse设置为:
However, IF I set my Eclipse to:
- Java编译器(JDK Complience):10
- Java构建路径(JRE系统库):10
- Java Compiler (JDK Complience) : 10
- Java Build Path (JRE System Libraries) : 10
现在, import
语句抛出错误讯息:
The import javax.annotation.PostConstruct cannot be resolved
此错误也会在 @PreDestroy
注释上发生。
and this error also happen on @PreDestroy
annotation too.
为什么会这样? Java 10会发生什么?如果我仍想将Java Compiler和JRE系统库版本保留到Java 10,如何解决这个问题?
Why is this happening? What happen to Java 10? How to solve this problem if i still want to retain Java Compiler and JRE System Libraries version to Java 10?
谢谢。
您可以尝试向pom.xml添加注释依赖项,以便它们可用于Spring:
You can try to add annotation dependencies to pom.xml, so that they would be available for Spring:
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>