一、外在参数配置
通过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());
<!-- PropertyPlaceholderConfigurer 解析${ }型参数 -->
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>userinfo.properties</value>
</list>
</property>
<!-- systemPropertiesMode设置systemProperties中寻找参数优先级 0:SYSTEM_PROPERTIES_MODE_NEVER
1:SYSTEM_PROPERTIES_MODE_FALLBACK (默认值) 2:SYSTEM_PROPERTIES_MODE_OVERRIDE -->
<property name="systemPropertiesMode" value="1" />
<!-- 检查JVM环境变量后是否检查系统环境变量 默认是true 即检查 注意:systemPropertiesMode必须为1或者2 -->
<property name="searchSystemEnvironment" value="true" />
</bean>
<!-- propertyPlaceholderConfigurer 可以用 <context:property-placeholder/>简化应用 -->
<!-- <context:property-placeholder location="userinfo.properties" />! -->
<bean id="user1" class="cn.beanfactory.bean.UserInfo">
<property name="userName" value="${username}" />
<property name="passwd" value="${passwd}" />
</bean>
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());
<!-- PropertyOverrideConfigurer 替换beanName.propertyName 型实例属性 -->
<bean id="propertyOverrideConfiger"
class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="location">
<value>override.properties</value>
</property>
<property name="ignoreResourceNotFound" value="true" />
</bean>
<!--找不到元素 <context:property-override location="override.properties"/> -->
<bean id="userInfo" class="cn.beanfactory.bean.UserInfo">
<property name="userName" value="myusername" />
<property name="passwd" value="mypasswd" />
</bean>
二<util/>命名空间
package cn.beanfactory.bean;
public class ConstantBean {
public static String staticString = "StaticString";
public String beanString = "beanSring";
public static String getStaticString(){
return "static-method_"+ConstantBean.staticString;
}
public String getString(){
return "method_"+beanString;
}
public String getBeanString() {
return beanString;
}
public void setBeanString(String beanString) {
this.beanString = beanString;
}
public static void setStaticString(String staticString) {
ConstantBean.staticString = staticString;
}
}
<!-- <util:constant/> 常量配置-->
<bean id="constant_field" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="cn.beanfactory.bean.ConstantBean.staticString"/>
</bean>
<bean id="constant_method" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="cn.beanfactory.bean.ConstantBean.getStaticString"/>
</bean>
<bean id="cn.beanfactory.bean.ConstantBean.staticString" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
<util:constant id="utilConstant" static-field="cn.beanfactory.bean.ConstantBean.staticString"/>
<bean id="entity_field" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="targetObject">
<bean class="cn.beanfactory.bean.ConstantBean"></bean>
</property>
<property name="targetField" value="beanString"/>
</bean>
<bean id="entity_method" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<bean class="cn.beanfactory.bean.ConstantBean"></bean>
</property>
<property name="targetMethod" value="getString"/>
</bean>
log.info("========<util:constant/>========");
log.info("静态变量:"+gbfa.getBean("constant_field"));
log.info("实体变量:"+gbfa.getBean("entity_field"));
log.info("变量 from 静态方法:"+gbfa.getBean("constant_method"));
log.info("变量 form 实体方法:"+gbfa.getBean("entity_method"));
log.info("变量 form cn.beanfactory.bean.ConstantBean.staticString:"+gbfa.getBean("cn.beanfactory.bean.ConstantBean.staticString"));
log.info("util:Constant静态变量:"+gbfa.getBean("utilConstant"));
cn.beanfactory.bean.ConstantBean.staticString="changeStaticString";
log.info("-------------------------------------------");
log.info("修改后 静态变量:"+gbfa.getBean("constant_field"));
log.info("修改后 变量 from 静态方法:"+gbfa.getBean("constant_method"));
log.info("修改后 util:Constant静态变量:"+gbfa.getBean("utilConstant"));
log.info("修改后 变量 form cn.beanfactory.bean.ConstantBean.staticString:"+gbfa.getBean("cn.beanfactory.bean.ConstantBean.staticString"));
//输出结果
========<util:constant/>========
静态变量:StaticString
实体变量:beanSring
变量 from 静态方法:static-method_StaticString
变量 form 实体方法:method_beanSring
变量 form cn.beanfactory.bean.ConstantBean.staticString:StaticString
util:Constant静态变量:StaticString
-------------------------------------------
修改后 静态变量:changeStaticString
修改后 变量 from 静态方法:static-method_StaticString
修改后 util:Constant静态变量:changeStaticString
修改后 变量 form cn.beanfactory.bean.ConstantBean.staticString:changeStaticString
三、<util:property-path/>
<!-- <util:property-path/> -->
<bean id="propertyPath1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName">
<idref local="paopertypath_Hello"/>
</property>
<property name="propertyPath" value="beanString"/>
</bean>
<bean id="paopertypath_Hello" class="cn.beanfactory.bean.ConstantBean">
<property name="beanString" value="paopertypath_value"/>
</bean>
<util:property-path id="propertyPath2" path="paopertypath_Hello.beanString"/>
<bean id="propertyPath3" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetObject">
<!-- 内部Bean -->
<bean class="cn.beanfactory.bean.ConstantBean">
<property name="beanString" value="paopertypath_value2"/>
</bean>
</property>
<property name="propertyPath" value="beanString"/>
</bean>
log.info("========<util:property-path/>========");
log.info("PropertyPathFactoryBean--propertyPath:"+gbfa.getBean("propertyPath1"));
log.info("<util:property-path/>--propertyPath:"+gbfa.getBean("propertyPath2"));
log.info("PropertyPathFactoryBean2--propertyPath:"+gbfa.getBean("propertyPath3"));
//输出结果
========<util:property-path/>========
PropertyPathFactoryBean--propertyPath:paopertypath_value
<util:property-path/>--propertyPath:paopertypath_value
PropertyPathFactoryBean2--propertyPath:paopertypath_value2
四、<util:properties/>
<!-- <util:properties/> -->
<bean id="abstractPropertiesBean" abstract="true">
<property name="properties">
<props>
<prop key="pk1">pk_value_0_1</prop>
<prop key="pk2">pk_value_0_2</prop>
<prop key="pk3">pk_value_0_3</prop>
</props>
</property>
</bean>
<bean id="propertiesBean" class="cn.beanfactory.bean.PropertiesBean" parent="abstractPropertiesBean">
<property name="properties">
<props merge="true">
<prop key="pk2">pk_value_1_2</prop>
<prop key="pk4">pk_value_1_4</prop>
</props>
</property>
</bean>
<bean id="propertiesBean2" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties" >
<props>
<prop key="pk1">pk_value_2_1</prop>
<prop key="pk2">pk_value_2_2</prop>
<prop key="pk3">pk_value_2_3</prop>
</props>
</property>
</bean>
<bean id="propertiesBean3" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:propertyFactoryBean.properties"/>
</bean>
<util:properties id="propertiesBean4" location="classpath:propertyFactoryBean.properties" />
log.info("========<util:properties/>========");
log.info("propertiesBean:--------------");
PropertiesBean propertiesBean= gbfa.getBean("propertiesBean");
Properties properties = propertiesBean.getProperties();
for(Object key :properties.keySet()){
log.info(key+"="+properties.getProperty(key+""));
}
log.info("PropertiesFactoryBean:--------------");
properties = gbfa.getBean("propertiesBean2");
for(Object key :properties.keySet()){
log.info(key+"="+properties.getProperty(key+""));
}
log.info("PropertiesFactoryBean 属性文件:--------------");
properties = gbfa.getBean("propertiesBean3");
for(Object key :properties.keySet()){
log.info(key+"="+properties.getProperty(key+""));
}
log.info("<util:properties/> 属性文件:--------------");
properties = gbfa.getBean("propertiesBean4");
for(Object key :properties.keySet()){
log.info(key+"="+properties.getProperty(key+""));
}
//输出结果
========<util:properties/>========
propertiesBean:--------------
pk3=pk_value_0_3
pk2=pk_value_1_2
pk1=pk_value_0_1
pk4=pk_value_1_4
PropertiesFactoryBean:--------------
pk3=pk_value_2_3
pk2=pk_value_2_2
pk1=pk_value_2_1
PropertiesFactoryBean 属性文件:--------------
pk3=pk_value_3_3
pk2=pk_value_3_2
pk1=pk_value_3_1
<util:properties/> 属性文件:--------------
pk3=pk_value_3_3
pk2=pk_value_3_2
pk1=pk_value_3_1
五、<util:list/>
<!-- <util:list/> -->
<bean id="abstractListBean" abstract="true">
<property name="list">
<list>
<value>list_0_1</value>
<value>list_0_2</value>
</list>
</property>
</bean>
<bean id="listBean" class="cn.beanfactory.bean.PropertiesBean" parent="abstractListBean">
<property name="list">
<list merge="true" value-type="java.lang.String">
<value>list_1_1</value>
<value>list_1_2</value>
<null></null>
</list>
</property>
</bean>
<bean id="listBean2" class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="targetListClass" value="java.util.ArrayList"/>
<property name="sourceList">
<list>
<value>list_2_1</value>
<value>list_2_2</value>
<value>list_2_3</value>
<value>list_2_4</value>
</list>
</property>
</bean>
<util:list id="listBean3" list-class="java.util.ArrayList" >
<value>list_3_1</value>
<value>list_3_2</value>
<value>list_3_3</value>
<value>list_3_4</value>
</util:list>
log.info("========<util:list/>========");
log.info("listBean:--------------");
PropertiesBean propertiesBean2= gbfa.getBean("listBean");
List list = propertiesBean2.getList();
for(Object o:list){
log.info(o);
}
log.info("ListFactoryBean:--------------");
list = gbfa.getBean("listBean2");
for(Object o:list){
log.info(o);
}
log.info("<util:list/>:--------------");
list = gbfa.getBean("listBean3");
for(Object o:list){
log.info(o);
}
//输出结果
========<util:list/>========
listBean:--------------
list_0_1
list_0_2
list_1_1
list_1_2
ListFactoryBean:--------------
list_2_1
list_2_2
list_2_3
list_2_4
<util:list/>:--------------
list_3_1
list_3_2
list_3_3
list_3_4
六、<util:p/>
<!-- <util:p/> -->
<util:list id="locations" list-class="java.util.ArrayList" >
<value>userinfo1.properties</value>
</util:list>
<util:list id="locations2" list-class="java.util.ArrayList" >
<value>userinfo2.properties</value>
</util:list>
<bean id="propertyPlaceholderConfigure" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" ref="locations"/>
<property name="order" value="1"/>
</bean>
<!-- order 1 优先级高 -->
<bean id="propertyPlaceholderConfigure2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:locations-ref="locations2" p:order="2" />