大家好,又见面了,我是你们的朋友全栈君。
在spring的bean的生命周期中,实例化->生成对象->属性填充后会进行afterPropertiesSet方法,这个方法可以用在一些特殊情况中,也就是某个对象的某个属性需要经过外界得到,比如说查询数据库等方式,这时候可以用到spring的该特性,只需要实现InitializingBean即可:
@Component("a")
public class A implements InitializingBean {
private B b;
public A(B b) {
this.b = b;
}
@Override
public void afterPropertiesSet() throws Exception {
}
}
这样可以在afterPropertiesSet方法中进行你的额外操作。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/125759.html原文链接:https://javaforall.net