一、外在参数配置
通过PropertyPlaceholderConfiger和PropertyOverideConfigurer对象可以实现外在配置,他们实现了BeanFactoryPostProcessor接口。
PropertyPlaceholderConfiger
#userinfo.properties
#username=scott
passwd=tiger
System.setProperty("username", "sys_username");
ApplicationContext acc = new ClassPathXmlApplicationContext(
"config.xml");
GenericBeanFactoryAccessor gbfa = new GenericBeanFactoryAccessor(acc);
log.info("========外在参数配置========");
UserInfo user1 =gbfa.getBean("user1");
log.info("user1.name="+user1.getUserName());
log.info("user1.passwd="+user1.getPasswd());
PropertyOverideConfigurer
#override.properties
userInfo.userName=overideUserName
#userInfo.passwd=overrideUserPasswd
UserInfo userInfo =gbfa.getBean("userInfo");
log.info("userInfo.name="+userInfo.getUserName());
log.info("userInfo.passwd="+userInfo.getPasswd());
本文出自“追梦”