Java常用的三大集合:List,Set,Map在Spring文件的配置
集合属性:
在Spring中可以通过一组内置的xml标签<list>、<set>、<map>来配置集合属性当然,需要指定具体标签,对应集合类型。
可以通过简单的value、或是用ref指向对其他bean的引用,或是通过手写内部bean
数组定义和list一样,set集合也是。
<bean id="boss" class="com.harry.spring.learning.Boss">
<property name="name" value = "nucky"></property>
<property name="age" value = "25"></property>
<property name="cars" >
<list>
<ref bean = "car"/>
<bean class="com.harry.spring.learning.Car">
<property name="brand" value = "BMW" ></property>
<property name="price" value = "320000"></property>
<property name="maxSpeed" value = "20"></property>
<property name="corp" >
<value><![CDATA[深圳]]></value>
</property>
</bean>
</list>
</property>
</bean>
<bean id="test" class="com.harry.spring.listString.Grade">
<property name="name" value="nucky"></property>
<property name="list">
<list>
<value>nice</value>
<value>to</value>
<value>meet</value>
<value>you</value>
<value>~</value>
</list>
</property>
</bean>
<bean id="test" class="com.harry.spring.listString.Grade">
<property name="name" value="nucky"></property>
<property name="list">
<list>
<value>nice</value>
<value>to</value>
<value>meet</value>
<value>you</value>
<value>~</value>
</list>
</property>
</bean>
Map集合
map 通过entry标签。以key - value 的形式进行书写
<bean id="nuckyBoss" class="com.harry.spring.learning.BigBoss">
<property name="name" value = "nuckyBoss"></property>
<property name="age" value = "29"></property>
<property name="cars" >
<map>
<entry key = "one" >
<ref bean = "car"/>
</entry>
<entry key ="two" value-ref="car"></entry>
</map>
</property>
</bean>
<bean id = "map" class="com.harry.spring.listString.Glass">
<property name="name" value="harry"></property>
<property name="map" >
<map>
<entry key = "one" value="one"></entry>
<entry key = "two" value="two"></entry>
</map>
</property>
</bean>
Properties的配置
properties ,后期和dataSource打交道,比如hibernate框架等,数据持久层。也是通过map的形式进行配置
!-- 配置properties Demo -->
<bean id = "properties" class="com.harry.spring.learning.DataSource">
<property name="properties">
<!-- 使用props 和prop子节点 -->
<props>
<prop key = "user">root</prop>
<prop key = "password">123456</prop>
<prop key = "jdbc">jdbc:mysql:///test</prop>
<prop key = "driverClass">test</prop>
</props>
</property>
</bean>
Util命名空间
引入Util 命名空间,可将多个bean对象进行封装,可被多个其他bean共享
<util:list id ="cars">
<ref bean ="car"/>
<ref bean ="car2"/>
</util:list>
P命名空间
通过引入p命令空间,可以直接为bean的属性赋值,具体见例子
<bean id="p" class="com.harry.spring.learning.Person"
p:age = "30" p:name="harrysir" p:car-ref="car" ></bean>
自动装配:
Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式一共有两种装配模式:byType(根据类型自动装配) byName(根据属性自动装配)
比如有个person,属性为Car car和city。
byType,容器中有Car的对象,用上autowire="byType" 就会自动装配
byName,容器中为id跟person的属性名相同的bean,在person中用上autowire="byName" 就会自动装配了。
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!--
<bean id = "test" class="com.harry.spring.learning.User">
<property name="username" value="huanghong"></property>
<property name="password" value="harrysir"></property>
</bean>
-->
<!-- 使用构造器注入属性值,可指定参数的位置和类型以区分重载的构造器 -->
<bean id="car" class="com.harry.spring.learning.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="300000" type="int"></constructor-arg>
<constructor-arg type = "java.lang.String">
<value><![CDATA[shanghai~~]]></value>
</constructor-arg>
</bean>
<bean id="car2" class="com.harry.spring.learning.Car">
<constructor-arg value="Audi Sp" index="0"></constructor-arg>
<constructor-arg value="500000" type="int"></constructor-arg>
<constructor-arg type = "java.lang.String">
<value><![CDATA[shanghai~~]]></value>
</constructor-arg>
</bean>
<bean id="person" class="com.harry.spring.learning.Person">
<property name="name" value = "Harry"></property>
<property name="age" value="20"></property>
<property name="car">
<bean class="com.harry.spring.learning.Car">
<constructor-arg value = "Ford"></constructor-arg>
<constructor-arg value = "250000"></constructor-arg>
<constructor-arg value = "shenzhen "></constructor-arg>
</bean>
</property>
<!--如果上面的car对象注释掉了,下面这行执行就会报错,Spring不会自动创建 -->
<property name="car.maxSpeed" value="200"></property> <!-- 级联属性 类对象的属性-->
</bean>
<bean id="boss2" class="com.harry.spring.learning.Boss">
<property name="name" value = "Harry22"></property>
<property name="age" value="20"></property>
<property name="cars" ref="cars">
</property>
</bean>
<bean id="boss" class="com.harry.spring.learning.Boss">
<property name="name" value = "nucky"></property>
<property name="age" value = "25"></property>
<property name="cars" >
<list>
<ref bean = "car"/>
<bean class="com.harry.spring.learning.Car">
<property name="brand" value = "BMW" ></property>
<property name="price" value = "320000"></property>
<property name="maxSpeed" value = "20"></property>
<property name="corp" >
<value><![CDATA[深圳]]></value>
</property>
</bean>
</list>
</property>
</bean>
<bean id="nuckyBoss" class="com.harry.spring.learning.BigBoss">
<property name="name" value = "nuckyBoss"></property>
<property name="age" value = "29"></property>
<property name="cars" >
<map>
<entry key = "one" >
<ref bean = "car"/>
</entry>
<entry key ="two" value-ref="car"></entry>
</map>
</property>
</bean>
<!-- 配置properties Demo -->
<bean id = "properties" class="com.harry.spring.learning.DataSource">
<property name="properties">
<!-- 使用props 和prop子节点 -->
<props>
<prop key = "user">root</prop>
<prop key = "password">123456</prop>
<prop key = "jdbc">jdbc:mysql:///test</prop>
<prop key = "driverClass">test</prop>
</props>
</property>
</bean>
<bean id="p" class="com.harry.spring.learning.Person"
p:age = "30" p:name="harrysir" p:car-ref="car" ></bean>
<util:list id ="cars">
<ref bean ="car"/>
<ref bean ="car2"/>
</util:list>
</beans>
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="car" class="com.harry.spring.autowire.Car"
p:brand="audi" p:price="10000"
> </bean>
<bean id="address" class="com.harry.spring.autowire.Address"
p:city="shenzhen" p:street="nanshan"
></bean>
<bean id = "person" class="com.harry.spring.autowire.Person"
p:name="nucky" autowire="byType"
></bean>
</beans>