我无法在Java 11中使用@PostConstruct和@PostDestroy [英] I can't use @PostConstruct and @PostDestroy with Java 11

查看:677
本文介绍了我无法在Java 11中使用@PostConstruct和@PostDestroy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用@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:

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

在这里找到: https://www.baeldung.com/spring-postconstruct-predestroy

这篇关于我无法在Java 11中使用@PostConstruct和@PostDestroy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆