在spring配置文件中不仅可以将String,int注入,还可以注入集合等类型,此外还可以注入配置文件中定义的其他的bean
1. 字面值注入
特殊字符使用CDATA或对应的xml转移序列符号
<bean id="dog" class="com.ghq.cloud.source.Dog">
<property name="name">
<value><![CDATA[阿黄&123]]></value>
</property>
</bean>
<bean id="dog2" class="com.ghq.cloud.source.Dog">
<property name="name">
<value>阿黄&123</value>
</property>
</bean>
2. 引用其他bean
<bean id="dog" class="com.ghq.cloud.source.Dog">
<property name="name">
<value><![CDATA[阿黄&123]]></value>
</property>
<property name="age" value="1"/>
<property name="weight" value="23.4"/>
</bean>
<bean id="master" class="com.ghq.cloud.source.Master">
<property name="name" value="master"/>
<property name="dog">
<ref bean="dog"/>
</property>
</bean>
关于ref标签 < ref bean=“dog”/> 这里需要说明,ref属性有个parent。这里重点说明:
这里有两个配置文件:
beans.xml, 配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dog" class="com.ghq.cloud.source.Dog">
<property name="name">
<value><![CDATA[阿黄&123]]></value>
</property>
<property name="age" value="1"/>
<property name="weight" value="23.4"/>
</bean>
</beans>
beans2.xml, 配置如下:注意 < ref parent=“dog”/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dog" class="com.ghq.cloud.source.Dog">
<property name="name">
<value><![CDATA[阿黄&123wer]]></value>
</property>
<property name="age" value="1"/>
<property name="weight" value="23.4"/>
</bean>
<bean id="master" class="com.ghq.cloud.source.Master">
<property name="name" value="master"/>
<property name="dog">
<ref parent="dog"/>
</property>
</bean>
</beans>
main方法如下:
//1. 父容器
ClassPathXmlApplicationContext pcontext = new ClassPathXmlApplicationContext("classpath:beans.xml");
/**
* 2. 子容器
* 指定子容器的父容器为 pcontext
*/
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:beans2.xml"},pcontext);
Master master = (Master) context.getBean("master");
System.out.println(master.getDog());
输出结果如下:
Dog{name=‘阿黄&123’, age=1, weight=23.4}
3. 内部bean
<bean id="master" class="com.ghq.cloud.source.Master">
<property name="name" value="master"/>
<property name="dog">
<bean class="com.ghq.cloud.source.Dog">
<property name="name">
<value><![CDATA[阿黄&123wer]]></value>
</property>
<property name="age" value="1"/>
<property name="weight" value="23.4"/>
</bean>
</property>
</bean>
4. null值(spring5好像不支持了)
<property name="name">
<value><null/></value>
</property>
5. 级联属性
Master实体:(这里必须将dog属性赋值)
public class Master {
private String name;
private Dog dog = new Dog();
}
<bean id="master" class="com.ghq.cloud.source.Master">
<property name="name" value="master"/>
<property name="dog.name" value="阿黄"/>
<property name="dog.age" value="1"/>
<property name="dog.weight" value="23.4"/>
</bean>
6. 集合类型
Master类:
public class Master {
private String name;
private Dog dog = new Dog();
private List<String> favorites = new ArrayList<>();
private String[] arr1=new String[3];
private int[] arr2=new int[3];
private Set<String> set = new HashSet<>();
private Map<String,String> jobs = new HashMap<>();
private Properties mails = new Properties();
//省略getter和setter
}
xml配置:
<bean id="master" class="com.ghq.cloud.source.Master">
<property name="name" value="master"/>
<property name="dog.name" value="阿黄"/>
<property name="dog.age" value="1"/>
<property name="dog.weight" value="23.4"/>
配置list
<property name="favorites">
<list>
<value>爱好1</value>
<value>爱好2</value>
<value>爱好3</value>
<value>爱好4</value>
</list>
</property>
配置String[]
<property name="arr1">
<list>
<value>爱好5</value>
<value>爱好6</value>
<value>爱好7</value>
<value>爱好8</value>
</list>
</property>
配置int[]
<property name="arr2">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</list>
</property>
配置Set
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
<value>set4</value>
</set>
</property>
配置Map
<property name="jobs">
<map>
<entry>
<key><value>1</value></key>
<value>work1</value>
</entry>
<entry>
<key><value>2</value></key>
<value>work2</value>
</entry>
<entry>
<key><value>3</value></key>
<value>work3</value>
</entry>
</map>
</property>
配置Properties
<property name="mails">
<props>
<prop key="a">123</prop>
<prop key="b">456</prop>
<prop key="c">789</prop>
</props>
</property>
</bean>
如果Map的key和value都是非String类型的对象,可以采用如下配置方式
<entry>
<key><ref bean="keyBean"></key>
<ref bean="valueBean"/>
</entry>
7. 集合合并(merge=“true”:表示和父类中相同属性的集合元素合并)
<bean id="parent" class="com.ghq.cloud.source.Master" abstract="true">
<property name="name" value="master"/>
<property name="dog.name" value="阿黄"/>
<property name="dog.age" value="1"/>
<property name="dog.weight" value="23.4"/>
<property name="favorites">
<list>
<value>爱好1</value>
<value>爱好2</value>
<value>爱好3</value>
<value>爱好4</value>
</list>
</property>
</bean>
<bean id="master" class="com.ghq.cloud.source.Master"
parent="parent">
<property name="favorites">
merge="true":表示和父类中相同属性的集合元素合并
<list merge="true">
<value>爱好5</value>
<value>爱好6</value>
<value>爱好3</value>
</list>
</property>
</bean>
8.util命名空间定义集合
util 命名空间引入:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
xml配置:
<util:list id="list" list-class="java.util.ArrayList">
<value>爱好11</value>
<value>爱好21</value>
<value>爱好31</value>
<value>爱好41</value>
</util:list>
<util:set id="set" set-class="java.util.HashSet">
<value>set1</value>
<value>set2</value>
<value>set3</value>
<value>set4</value>
</util:set>
<util:map id="map" key-type="java.lang.String" map-class="java.util.HashMap" value-type="java.lang.Integer">
<entry key="a" value="1"/>
<entry key="b" value="2"/>
<entry key="c" value="3"/>
</util:map>
9.p命名空间简化配置
p命名空间引入,没有对应的Schema文件。
xmlns:p="http://www.springframework.org/schema/p"
xml配置
<!--
简化前
-->
<bean id="dog" class="com.ghq.cloud.source.Dog">
<property name="name" value="阿黄"/>
<property name="age" value="1"/>
<property name="weight" value="23.4"/>
</bean>
<bean id="master" class="com.ghq.cloud.source.Master">
<property name="dog" ref="dog"/>
</bean>
<!--
简化后
-->
<bean id="dog2" class="com.ghq.cloud.source.Dog"
p:name="阿黄" p:age="1" p:weight="23.4"/>
<util:list id="list" list-class="java.util.ArrayList">
<value>爱好11</value>
<value>爱好21</value>
<value>爱好31</value>
<value>爱好41</value>
</util:list>
<bean id="master2" class="com.ghq.cloud.source.Master"
p:name="master" p:dog-ref="dog2" p:favorites-ref="list"/>
采用p命名空间,
对于一般的属性值: p:属性名=属性值
对于引用数据类型(不是String): p:属性名-ref=属性值