gpt4 book ai didi

java - AutowireCapableBeanFactory.autowireBean(bean) 从不调用 InitlizingBean.afterPropertiesSet()?

转载 作者:行者123 更新时间:2023-12-01 22:57:19 25 4
gpt4 key购买 nike

我需要在 bean 属性设置后做一些初始化工作,

ApplicationContext context = new ...;
AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory();

// autowireBean only populate the fields, but never invoke afterPropertiesSet().
factory.autowireBean(bean);

// Should I set it manually?
// if (bean instanceof InitializingBean) {
// ((InitializingBean) bean).afterPropertiesSet();
// }
// if (bean instanceof ApplicationContextAware) {
// ((ApplicationContextAware) bean).setApplicationContext(context);
// }
// if ...

最佳答案

试试这个:

factory.autowireBean(bean);
bean = (YourBean) factory.initializeBean(bean, "anyName");

它与@PostConstruct(我推荐)一起工作,因此它也应该执行afterPropertiesSet()anyName 是bean的名字,可能在BeanNameAware时用到涉及接口(interface)。

关于java - AutowireCapableBeanFactory.autowireBean(bean) 从不调用 InitlizingBean.afterPropertiesSet()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5610699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com