我不能在 Java 11 中使用 @PostConstruct 和 @PostDestroy [英] I can't use @PostConstruct and @PostDestroy with Java 11
问题描述
我在项目中使用 @PostConstruct
和 @PostDestroy
注释时遇到问题.我无法使用这些注释,尽管我导入了 Java 的注释,但看起来这些注释并不存在.我正在使用 Java 11,这是我的 build.gradle
文件的内容:
I've got problem with using @PostConstruct
and@PostDestroy
annotations in my project. I can't use these annotations and it looks like these doesn't exist despite the fact that I imported Java's annotations. I am using Java 11 and that is content of my build.gradle
file:
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework', name: 'spring-webmvc', version: '5.1.0.RELEASE'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.7'
compile group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
provided group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
}
推荐答案
请注意,@PostConstruct
和 @PreDestroy
批注都是 Java EE 的一部分.由于 Java EE 已在 Java 9 中弃用并在 Java 11 中删除,因此我们必须添加额外的依赖项以使用这些注释:
Note that both @PostConstruct
and @PreDestroy
annotations are part of Java EE. And since Java EE has been deprecated in Java 9 and removed in Java 11 we have to add an additional dependency to use these annotations:
对于 Maven
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
如果使用 Gradle
If using Gradle
implementation "javax.annotation:javax.annotation-api:1.3.2"
在这里找到:https://www.baeldung.com/spring-postconstruct-predestroy一个>
这篇关于我不能在 Java 11 中使用 @PostConstruct 和 @PostDestroy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!