I can't use @PostConstruct and @PostDestroy wi

2020-06-03 04:24发布

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' 
}

2条回答
再贱就再见
2楼-- · 2020-06-03 04:53

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>

Found here: https://www.baeldung.com/spring-postconstruct-predestroy

查看更多
混吃等死
3楼-- · 2020-06-03 05:01

You have only spring-webmvc, you need the rest of the spring to be able to use their annotations. Probably spring-core and spring-annotations.

查看更多
登录 后发表回答