如果非要这么做,用BeanPostProcessor.postProcessBeforeInitialization的hook扩展接口试试。这个的执行顺序在PostConstruct之前,应该能满足你的要求
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if ("xxx".equals(beanName)) {
((XXX)bean).setTheField(xxx);
}
return bean;
}
}
另外想到另一个办法是写个自定义PropertyPlaceholderConfigurer或者PropertyOverrideConfigurer,在那里覆盖缺省值。
【 在 BlackHouse (小黑屋) 的大作中提到: 】
: 已知在默认配置的情况下,运行时通过 applicationContext.getBean("xxx") 可以获取到一个 bean 对象。
: 这个默认的bean对象里有很多属性(里面又嵌套了很多层对象、属性),spring boot都会在启动时初始化好,我希望保留不变,因为重新初始化一个太麻烦。
: 但我想设置其中的一个属性 theField,我应该怎么办呢?
: ...................