多个PostConstruct方法? [英] Multiple PostConstruct methods?
问题描述
在 Java的文档页中显示了PostConstruct那
It says in Java's documentation page for PostConstruct that
此注释只能注释一种方法
Only one method can be annotated with this annotation
但是我只是尝试使用PostConstruct注释一个独立应用程序的三种方法.没有编译错误,所有这三个错误都可以顺利调用和执行.
But I just tried annotating three methods of a standalone application with PostConstruct. No compile errors, and all three of them are invoked and executed smoothly.
那我想念什么?什么样的类可以并且不能存在多个PostConstruct注释?
So what am I missing? In what kind of class can and cannot exist multiple PostConstruct annotations?
推荐答案
是的,似乎Spring没有遵循此限制.我已经找到了用于处理此注释的代码,该注释为InitDestroyAnnotationBeanPostProcessor
,以及特定的方法:
Yes, it's seem Spring doesn't follow this restriction. I have found code to process this annotation which is InitDestroyAnnotationBeanPostProcessor
, and the specific method:
public void invokeInitMethods(Object target, String beanName) throws Throwable {
Collection<LifecycleElement> initMethodsToIterate =
(this.checkedInitMethods != null ? this.checkedInitMethods : this.initMethods);
if (!initMethodsToIterate.isEmpty()) {
boolean debug = logger.isDebugEnabled();
for (LifecycleElement element : initMethodsToIterate) {
if (debug) {
logger.debug("Invoking init method on bean '" + beanName + "': " + element.getMethod());
}
element.invoke(target);
}
}
}
因此,spring支持多个PostConstruct
So, spring support multi PostConstruct
这篇关于多个PostConstruct方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!