在使用注释实现 bean 生命周期时,遇到了一个问题,即我的 xml 文件在“context:annotation-config/”标签中显示错误。 我补充说:
`<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>`
仍然没有应用注释@PreConstruct 和@PreDestroy。
只需将其添加到您的配置文件中即可。
`xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"`
它解决了我的问题。
将此添加到您的 pom.xml 文件
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
然后简单地将其添加到您的配置文件中。
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
并在java bean中添加
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
用于使用注解@PreDestroy 和@PostConstruct 您还可以尝试将鼠标悬停在注释上,看看您遗漏了哪些导入。 希望这有帮助!