spring 某些類(lèi) 從 DB 初始化 一些屬性
在spring初始化過(guò)程中加載數(shù)據(jù)庫(kù)中的數(shù)據(jù)
項(xiàng)目中有這個(gè)需求 需啟動(dòng)時(shí)將某些屬性賦DB讀到的值
達(dá)到同步數(shù)據(jù)的目的
考慮 1:構(gòu)造函數(shù) 不可以 dao還未初始化 null point
2:xx 屬性 = dao.getXX.... dao還未初始化
使用BeanPostProcessor可以
public class PBSTrackManagerPostProcessor implements BeanPostProcessor
{
public Object postProcessAfterInitialization(Object obj, String s)
throws BeansException
{
if(obj instanceof PBSTrackManager)
{
((PBSTrackManager) obj).plcObjectInit();
}
return obj;
}
public Object postProcessBeforeInitialization(Object obj, String s)
throws BeansException
{
return obj;
}
}
但要注意配置文件
<bean id="pbsTrackManagerPostProcess" class="cn.edu.hust.mes.service.manager.productionplan.impl.PBSTrackManagerPostProcessor">
!!!!
default-lazy-init="false"
實(shí)時(shí)加載才可以