1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 16 <bean id="car" class="com.asm.Car" > 17 <property name="maxSpeed"> 18 <value>200</value> 19 </property> 20 <property name="brand"> 21 <value><![CDATA[奥迪&CA72]]></value> 22 </property> 23 </bean> 24 25 </beans>
当有特殊字符的时候<![CDATA[]]>
2.引用其他bean
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 16 17 <bean id="car" class="com.asm.Car" /> 18 <bean id="boss" class="com.asm.Boss"> 19 <property name="car"> 20 <ref bean="car"/> 21 </property> 22 </bean> 23 </beans>
3.集合类属性
List;
1 package com.asm; 2 3 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.springframework.beans.BeansException; 8 import org.springframework.beans.factory.BeanFactory; 9 import org.springframework.beans.factory.BeanFactoryAware; 10 import org.springframework.beans.factory.BeanNameAware; 11 import org.springframework.beans.factory.DisposableBean; 12 import org.springframework.beans.factory.InitializingBean; 13 14 public class Car { 15 private String brand; 16 private String color; 17 private int maxSpeed; 18 19 private List abc = new ArrayList(); 20 21 public List getAbc() { 22 return abc; 23 } 24 public void setAbc(List abc) { 25 this.abc = abc; 26 } 27 public String getBrand() { 28 return brand; 29 } 30 public void setBrand(String brand) { 31 this.brand = brand; 32 } 33 public String getColor() { 34 return color; 35 } 36 public void setColor(String color) { 37 this.color = color; 38 } 39 public int getMaxSpeed() { 40 return maxSpeed; 41 } 42 public void setMaxSpeed(int maxSpeed) { 43 this.maxSpeed = maxSpeed; 44 } 45 46 47 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 16 <bean id="car" class="com.asm.Car" > 17 <property name="maxSpeed"> 18 <value>200</value> 19 </property> 20 <property name="brand"> 21 <value><![CDATA[奥迪&CA72]]></value> 22 </property> 23 <property name="abc"> 24 <list> 25 <value>看报</value> 26 <value>赛车</value> 27 <value>高尔夫</value> 28 </list> 29 </property> 30 </bean> 31 32 </beans>
Map:
1 package com.asm; 2 3 4 import java.util.ArrayList; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 import org.springframework.beans.BeansException; 10 import org.springframework.beans.factory.BeanFactory; 11 import org.springframework.beans.factory.BeanFactoryAware; 12 import org.springframework.beans.factory.BeanNameAware; 13 import org.springframework.beans.factory.DisposableBean; 14 import org.springframework.beans.factory.InitializingBean; 15 16 public class Car { 17 private String brand; 18 private String color; 19 private int maxSpeed; 20 21 private List abc = new ArrayList(); 22 private Map map = new HashMap(); 23 24 25 26 public Map getMap() { 27 return map; 28 } 29 public void setMap(Map map) { 30 this.map = map; 31 } 32 public List getAbc() { 33 return abc; 34 } 35 public void setAbc(List abc) { 36 this.abc = abc; 37 } 38 public String getBrand() { 39 return brand; 40 } 41 public void setBrand(String brand) { 42 this.brand = brand; 43 } 44 public String getColor() { 45 return color; 46 } 47 public void setColor(String color) { 48 this.color = color; 49 } 50 public int getMaxSpeed() { 51 return maxSpeed; 52 } 53 public void setMaxSpeed(int maxSpeed) { 54 this.maxSpeed = maxSpeed; 55 } 56 57 58 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 16 <bean id="car" class="com.asm.Car" > 17 <property name="maxSpeed"> 18 <value>200</value> 19 </property> 20 <property name="brand"> 21 <value><![CDATA[奥迪&CA72]]></value> 22 </property> 23 <property name="abc"> 24 <list> 25 <value>看报</value> 26 <value>赛车</value> 27 <value>高尔夫</value> 28 </list> 29 </property> 30 <property name="map"> 31 <map> 32 <entry> 33 <key><value>AM</value></key> 34 <value>abcabc</value> 35 </entry> 36 <entry> 37 <key><value>AM111</value></key> 38 <value>abcabc1111</value> 39 </entry> 40 </map> 41 </property> 42 </bean> 43 44 </beans>
property
1 package com.asm; 2 3 4 import java.util.ArrayList; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Properties; 9 10 import org.springframework.beans.BeansException; 11 import org.springframework.beans.factory.BeanFactory; 12 import org.springframework.beans.factory.BeanFactoryAware; 13 import org.springframework.beans.factory.BeanNameAware; 14 import org.springframework.beans.factory.DisposableBean; 15 import org.springframework.beans.factory.InitializingBean; 16 17 public class Car { 18 private String brand; 19 private String color; 20 private int maxSpeed; 21 22 private List abc = new ArrayList(); 23 private Map map = new HashMap(); 24 private Properties mail = new Properties(); 25 26 public Properties getMail() { 27 return mail; 28 } 29 public void setMail(Properties mail) { 30 this.mail = mail; 31 } 32 public Map getMap() { 33 return map; 34 } 35 public void setMap(Map map) { 36 this.map = map; 37 } 38 public List getAbc() { 39 return abc; 40 } 41 public void setAbc(List abc) { 42 this.abc = abc; 43 } 44 public String getBrand() { 45 return brand; 46 } 47 public void setBrand(String brand) { 48 this.brand = brand; 49 } 50 public String getColor() { 51 return color; 52 } 53 public void setColor(String color) { 54 this.color = color; 55 } 56 public int getMaxSpeed() { 57 return maxSpeed; 58 } 59 public void setMaxSpeed(int maxSpeed) { 60 this.maxSpeed = maxSpeed; 61 } 62 63 64 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 16 <bean id="car" class="com.asm.Car" > 17 <property name="maxSpeed"> 18 <value>200</value> 19 </property> 20 <property name="brand"> 21 <value><![CDATA[奥迪&CA72]]></value> 22 </property> 23 <property name="abc"> 24 <list> 25 <value>看报</value> 26 <value>赛车</value> 27 <value>高尔夫</value> 28 </list> 29 </property> 30 <property name="map"> 31 <map> 32 <entry> 33 <key><value>AM</value></key> 34 <value>abcabc</value> 35 </entry> 36 <entry> 37 <key><value>AM111</value></key> 38 <value>abcabc1111</value> 39 </entry> 40 </map> 41 </property> 42 <property name="mail"> 43 <props> 44 <prop key="mail"> 45 abcbaacacac 46 </prop> 47 </props> 48 </property> 49 </bean> 50 51 </beans>
Spring3系列8- Spring 自动装配 Bean
1. Auto-Wiring ‘no’
2. Auto-Wiring ‘byName’
3. Auto-Wiring ‘byType
4. Auto-Wiring ‘constructor’
5. Auto-Wiring ‘autodetect’
Spring Auto-Wiring Beans——Spring自动装配Bean
所谓自动装配,就是将一个Bean注入到其他Bean的Property中,类似于以下:
<bean id="customer" class="com.lei.common.Customer" autowire="byName" />
Spring支持5种自动装配模式,如下:
no ——默认情况下,不自动装配,通过“ref”attribute手动设定。
buName ——根据Property的Name自动装配,如果一个bean的name,和另一个bean中的Property的name相同,则自动装配这个bean到Property中。
byType ——根据Property的数据类型(Type)自动装配,如果一个bean的数据类型,兼容另一个bean中Property的数据类型,则自动装配。
constructor ——根据构造函数参数的数据类型,进行byType模式的自动装配。
autodetect ——如果发现默认的构造函数,用constructor模式,否则,用byType模式。
下例中演示自动装配
Customer.java如下:
package com.lei.common;
public class Customer
{
private Person person;
public Customer(Person person) {
this.person = person;
}
public void setPerson(Person person) {
this.person = person;
}
//...
}
Person.java如下:
package com.lei.common;
public class Person
{
//...
}
1. Auto-Wiring ‘no’
默认情况下,需要通过'ref’来装配bean,如下:
<bean id="customer" class="com.lei.common.Customer">
<property name="person" ref="person" />
</bean>
<bean id="person" class="com.lei.common.Person" />
2. Auto-Wiring ‘byName’
根据属性Property的名字装配bean,这种情况,Customer设置了autowire="byName",Spring会自动寻找与属性名字“person”相同的bean,找到后,通过调用setPerson(Person person)将其注入属性。
<bean id="customer" class="com.lei.common.Customer" autowire="byName" />
<bean id="person" class="com.lei.common.Person" />
如果根据 Property name找不到对应的bean配置,如下
<bean id="customer" class="com.lei.common.Customer" autowire="byName" />
<bean id="person_another" class="com.lei.common.Person" />
Customer中Property名字是person,但是配置文件中找不到person,只有person_another,这时就会装配失败,运行后,Customer中person=null。
3. Auto-Wiring ‘byType
根据属性Property的数据类型自动装配,这种情况,Customer设置了autowire="byType",Spring会总动寻找与属性类型相同的bean,找到后,通过调用setPerson(Person person)将其注入。
<bean id="customer" class="com.lei.common.Customer" autowire="byType" />
<bean id="person" class="com.lei.common.Person" />
如果配置文件中有两个类型相同的bean会怎样呢?如下:
<bean id="customer" class="com.lei.common.Customer" autowire="byType" />
<bean id="person" class="com.lei.common.Person" />
<bean id="person_another" class="com.lei.common.Person" />
一旦配置如上,有两种相同数据类型的bean被配置,将抛出UnsatisfiedDependencyException异常,见以下:
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException:
所以,一旦选择了’byType’类型的自动装配,请确认你的配置文件中每个数据类型定义一个唯一的bean。
4. Auto-Wiring ‘constructor’
这种情况下,Spring会寻找与参数数据类型相同的bean,通过构造函数public Customer(Person person)将其注入。
<bean id="customer" class="com.lei.common.Customer" autowire="constructor" />
<bean id="person" class="com.lei.common.Person" />
这种情况下,Spring会先寻找Customer中是否有默认的构造函数,如果有相当于上边的’constructor’这种情况,用构造函数注入,否则,用’byType’这种方式注入,所以,此例中通过调用public Customer(Person person)将其注入。
<bean id="customer" class="com.lei.common.Customer" autowire="autodetect" />
<bean id="person" class="com.lei.common.Person" />
注意:
项目中autowire结合dependency-check一起使用是一种很好的方法,这样能够确保属性总是可以成功注入。
<bean id="customer" class="com.lei.common.Customer"
autowire="autodetect" dependency-check="objects" />
<bean id="person" class="com.lei.common.Person" />
最后,我认为,自动装配虽然让开发变得更快速,但是同时却要花更大的力气维护,因为它增加了配置文件的复杂性,你甚至不知道哪一个bean会被自动注入到另一个bean中。我更愿意写配置文件来手工装配。