[英]@PostConstruct and @PreDestroy annotations do not work
I have TennisCoach class:我有网球教练课:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component
public class TennisCoach implements Coach {
@Autowired
@Qualifier("randomFortuneService")
private FortuneService fortuneService;
public TennisCoach() {
System.out.println("Inside default constructor");
}
@PostConstruct
public void doMyStartupStuff() {
System.out.println("Inside postconstructor");
}
@PreDestroy
public void doMyFinalStuff() {
System.out.println("Inside predestroyer");
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
@Override
public String getDailyWorkout() {
return "Practice your backhand volley";
}
}
And such main class:还有这样的主类:
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnotationBeanScopeDemoApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Coach theCoach = context.getBean("tennisCoach", Coach.class);
System.out.println(theCoach.getDailyFortune());
System.out.println(theCoach.getDailyWorkout());
context.close();
}
}
Expected output is:预期输出为:
Inside default constructor
默认构造函数内部
Inside postconstructor
内部后构造函数
DailyFortune
每日财富
Practice your backhand volley
练习你的反手截击
Inside predestroyer
驱逐舰内部
But I get this:但我明白了:
Inside default constructor
默认构造函数内部
DailyFortune
每日财富
Practice your backhand volley
练习你的反手截击
It seems like @PostContruct and @PreDestroy annotations do not work correctly. @PostContruct 和 @PreDestroy 注释似乎无法正常工作。 I cannot find the reason of my problem.
我找不到问题的原因。
Spring might not handle JSR-250 lifecycle annotations by default.默认情况下,Spring 可能不处理 JSR-250 生命周期注释。 Check your applicationContext.xml that it has element:
检查您的applicationContext.xml是否包含元素:
<beans ...
<context:annotation-config/>
....
</beans>
that configures Spring to be aware of @PostConstruct
& @PrePersist
.配置 Spring 以了解
@PostConstruct
和@PrePersist
。
Fo more info see this article .欲了解更多信息,请参阅这篇文章。
From the Spring Core
documentation, paragraph 1.9.8 - Using @PostConstruct and @PreDestroy
来自
Spring Core
文档的第1.9.8 - Using @PostConstruct and @PreDestroy
段1.9.8 - Using @PostConstruct and @PreDestroy
Provided that the
CommonAnnotationBeanPostProcessor
is registered within the SpringApplicationContext
, a method carrying one of these annotations is invoked at the same point in the lifecycle as the corresponding Spring lifecycle interface method or explicitly declared callback method.如果
CommonAnnotationBeanPostProcessor
已在 SpringApplicationContext
注册,则在生命周期中与相应的 Spring 生命周期接口方法或显式声明的回调方法相同的点调用携带这些注释之一的方法。
Declaring申报
<context:annotation-config/>
inside you applicationContext.xml
file, implicitly register a CommonAnnotationBeanPostProcessor
.在
applicationContext.xml
文件中,隐式注册一个CommonAnnotationBeanPostProcessor
。
Otherwise, you need to manually register it, using ( docs )否则,您需要使用 ( docs ) 手动注册它
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
If you use java 9+ then add dependency javax.annotation:javax.annotation-api
(or use java 8).如果您使用 java 9+,则添加依赖项
javax.annotation:javax.annotation-api
(或使用 java 8)。
Or use spring-boot 2 if you using spring-boot 1.或者,如果您使用 spring-boot 1,请使用 spring-boot 2。
Your Spring don't know about annotation's @PostContruct and @PreDestroy.您的 Spring 不知道注释的 @PostContruct 和 @PreDestroy。 Add this code in your applicationContext.xml
在 applicationContext.xml 中添加此代码
<bean class = "org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
Or implements InitializingBean,DisposableBean interfaces in your TenisCoach class.或者在 TenisCoach 类中实现 InitializingBean、DisposableBean 接口。
Eclipse is unable to import @PostConstruct or @PreDestroy Eclipse 无法导入 @PostConstruct 或 @PreDestroy
This happens because of Java 9 and higher.这是因为 Java 9 及更高版本。
When using Java 9 and higher, javax.annotation has been removed from its default classpath.使用 Java 9 及更高版本时,javax.annotation 已从其默认类路径中删除。 Solution
解决方案
Use the following steps to add it to your Java Build Path.使用以下步骤将其添加到您的 Java 构建路径。
Right-click your project, select Properties右键单击您的项目,选择属性
On left-hand side, click Java Build Path在左侧,单击 Java Build Path
In top-center of dialog, click Libraries在对话框的顶部中心,单击库
Click Classpath and then Click Add JARs ...单击 Classpath,然后单击 Add JARs ...
Navigate to the JAR file /lib/javax.annotation-api-1.3.2.jar导航到 JAR 文件 /lib/javax.annotation-api-1.3.2.jar
Click OK then click Apply and Close单击确定,然后单击应用并关闭
Eclipse will perform a rebuild of your project and it will resolve the related build errors. Eclipse 将重新构建您的项目并解决相关的构建错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.