我无法在Java 11中使用@PostConstruct和@PostDestroy
问题内容:
我在项目中使用@PostConstruct
和@PostDestroy
注释时遇到问题。我不能使用这些注释,尽管我导入了Java的注释,但这些注释似乎并不存在。我正在使用Java
11,这就是我build.gradle
文件的内容:
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中已被删除,因此我们必须添加一个附加依赖项才能使用这些注释:
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
在这里找到:https :
//www.baeldung.com/spring-postconstruct-
predestroy