關於Spring中基於xml文件配置bean的詳細總結(spring 4.1.0)
一、Spring中的依賴注入方式介紹
依賴注入有三種方式
- 屬性注入
- 構造方法注入
- 工廠方法注入(很少使用,不推薦,本文不再介紹)
屬性注入
通過 setter 方法注入Bean 的屬性值或依賴的對象。屬性注入使用 <property>
元素, 使用 name 屬性指定 Bean 的屬性名稱,value 屬性或 <value>
子節點指定屬性值 。屬性注入是實際應用中最常用的注入方式。HelloWorld類中的setName()方法,對應上邊代碼中的name屬性,例如:把setName()方法名改為setName2(),property中的name屬性值為name時則會報錯,需要將name屬性改為name2。
構造方法
構造方法注入Bean 的屬性值或依賴的對象,它保證了 Bean 實例在實例化后就可以使用。
構造器注入在 <constructor-arg>
元素里聲明屬性, <constructor-arg>
中沒有 name 屬性。使用value屬性值或value子節點為屬性賦值。可以同時使用索引 index 和type屬性對應為哪個屬性賦值。index的值表示構造函數中參數的位置。type表示成員屬性的類型,例如type=“double”
二、屬性注入和構造方法注入詳解:
1.applicationContext.xml文件配置如下
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"
4 xmlns:p="http://www.springframework.org/schema/p"
5 xmlns:util="http://www.springframework.org/schema/util"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 7 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
8
9 <!--
10 配置bean 11 class:bean的全類名,通過反射的方式在IOC容器中創建bean 12 id:表示容器中的bean,id唯一 13 -->
14 <!-- 通過setter注入配置bean的屬性 -->
15 <bean id="helloWorld" class="me.spring.beans.HelloWorld">
16 <property name="name" value="Spring"></property>
17 </bean>
18 <!-- 通過構造方法配置bean的屬性 -->
19 <bean id="car" class="me.spring.beans.Car">
20 <constructor-arg value="Audi"></constructor-arg>
21 <constructor-arg value="ShangHai"></constructor-arg>
22 <constructor-arg value="300000"></constructor-arg>
23 </bean>
24
25 <!--
26 使用構造器注入的屬性值可以指定參數的類型和參數的位置,以區分重載的構造器 27 如果字面值包含特殊字符,可以使用<![CDATA[]]>包裹起來 28 屬性值也可以使用value子節點進行配置 29 -->
30 <bean id="car2" class="me.spring.beans.Car">
31 <constructor-arg value="Baoma"></constructor-arg>
32 <constructor-arg type="java.lang.String">
33 <value><![CDATA[<Beijing>]]></value>
34 </constructor-arg>
35 <constructor-arg value="240" type="int"></constructor-arg>
36 </bean>
37
38 <!-- 可以使用property的ref屬性建立bean之間的引用關系 -->
39 <bean id="person" class="me.spring.beans.Person">
40 <property name="name" value="Tom"></property>
41 <property name="age" value="24"></property>
42 <!--
43 <property name="car" ref="car2"></property> 44 -->
45
46 <!--
47 <property name="car"> 48 <ref bean="car2"/> 49 </property> 50 -->
51
52 <!-- 內部bean,不能被外部引用 -->
53 <property name="car">
54 <bean class="me.spring.beans.Car">
55 <constructor-arg value="Ford"></constructor-arg>
56 <constructor-arg value="ChangAn"></constructor-arg>
57 <constructor-arg value="2354395" type="double"></constructor-arg>
58 </bean>
59 </property>
60 </bean>
61
62 <bean id="person2" class="me.spring.beans.Person">
63 <constructor-arg value="Jerry"></constructor-arg>
64 <constructor-arg value="25"></constructor-arg>
65 <constructor-arg ref="car2"></constructor-arg>
66
67 <!-- 測試賦值null -->
68 <!--
69 <constructor-arg><null/></constructor-arg> 70 -->
71 <!--
72 為級聯屬性賦值 73 注意:屬性需要初始化后才可以為級聯屬性賦值,和Struts2不同 74 這里必須依據person的setter和getter方法,不能為car2 75 -->
76 <property name="car.price" value="4546"></property>
77 </bean>
78
79 <!-- 測試如何配置集合屬性 -->
80 <bean id="person3" class="me.spring.beans.collections.Person">
81 <property name="name" value="Mike"></property>
82 <property name="age" value="34"></property>
83 <property name="cars">
84 <!-- 使用list結點為屬性為list的屬性賦值 -->
85 <list>
86 <ref bean="car"/>
87 <ref bean="car2"/>
88 <bean class="me.spring.beans.Car">
89 <constructor-arg value="Ford"></constructor-arg>
90 <constructor-arg value="ChangAn"></constructor-arg>
91 <constructor-arg value="2354395" type="double"></constructor-arg>
92 </bean>
93 </list>
94 </property>
95 </bean>
96 <bean id="newPerson" class="me.spring.beans.collections.NewPerson">
97 <property name="name" value="Rose"></property>
98 <property name="age" value="23"></property>
99 <property name="cars">
100 <!-- 使用map結點及map的entry子節點配置Map類型的成員變量 -->
101 <map>
102 <entry key="AA" value-ref="car"></entry>
103 <entry key="BB" value-ref="car2"></entry>
104 </map>
105 </property>
106 </bean>
107
108 <!-- 配置properties屬性值 -->
109 <bean id="dataSource" class="me.spring.beans.collections.DataSource">
110 <property name="properties">
111 <!-- 使用props和prop子節點來為properties屬性值賦值 -->
112 <props>
113 <prop key="user">root</prop>
114 <prop key="password">123456</prop>
115 <prop key="jdbcURL">jdbc:mysql://localhost:3306/test</prop>
116 <prop key="driverClass">com.mysql.jdbc.Driver</prop>
117 </props>
118 </property>
119 </bean>
120
121 <!-- 配置單例的集合bean,以供多個bean進行引用,需要導入util命名空間 -->
122 <util:list id="cars">
123 <ref bean="car"/>
124 <ref bean="car2"/>
125 </util:list>
126
127 <bean id="person4" class="me.spring.beans.collections.Person">
128 <property name="name" value="Jack"></property>
129 <property name="age" value="34"></property>
130 <property name="cars" ref="cars"></property>
131 </bean>
132
133 <!-- 通過p命名空間為bean的屬性賦值,需要導入p命名空間,相對於傳統的配置較為簡潔 -->
134 <bean id="person5" class="me.spring.beans.collections.Person" p:name="Queen" p:age="45" p:cars-ref="cars"></bean>
135 </beans>
2.相關的實體類:
(1)me.spring.beans 包下:
1 package me.spring.beans; 2
3 public class Car { 4
5 private String brand; 6 private String corp; 7 private double price; 8 private int maxSpeed; 9 public Car(String brand, String corp, double price) { 10 super(); 11 this.brand = brand; 12 this.corp = corp; 13 this.price = price; 14 } 15
16 public Car(String brand, String corp, int maxSpeed) { 17 super(); 18 this.brand = brand; 19 this.corp = corp; 20 this.maxSpeed = maxSpeed; 21 } 22
23 public void setPrice(double price) { 24 this.price = price; 25 } 26
27 public double getPrice() { 28 return price; 29 } 30 @Override 31 public String toString() { 32 return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]"; 33 } 34
35 } 36
37 package me.spring.beans; 38
39 public class Person { 40
41 private String name; 42 private int age; 43 private Car car; 44
45 public String getName() { 46 return name; 47 } 48
49 public void setName(String name) { 50 this.name = name; 51 } 52
53 public int getAge() { 54 return age; 55 } 56
57 public void setAge(int age) { 58 this.age = age; 59 } 60
61 public Car getCar() { 62 return car; 63 } 64
65 public void setCar(Car car) { 66 this.car = car; 67 } 68
69 public Person() { 70
71 } 72 public Person(String name, int age, Car car) { 73 super(); 74 this.name = name; 75 this.age = age; 76 this.car = car; 77 } 78
79 @Override 80 public String toString() { 81 return "Person [name=" + name + ", age=" + age + ", car=" + car + "]"; 82 } 83
84 } 85
86 package me.spring.beans; 87
88 public class HelloWorld { 89
90 private String name; 91 public void setName(String name) { 92 this.name = name; 93 } 94 public void hello() { 95 System.out.println("hello:" + name); 96 } 97 } 98
99 package me.spring.beans; 100
101 import org.springframework.context.ApplicationContext; 102 import org.springframework.context.support.ClassPathXmlApplicationContext; 103
104 public class Main { 105
106 public static void main(String[] args) { 107 // HelloWorld helloWorld = new HelloWorld(); 108 // helloWorld.setName("Spring"); 109 // helloWorld.hello(); 110
111 //1.創建Spring的IOC容器對象 112 //ApplicationContext代表IOC容器 113 //ClassPathXmlApplicationContext是ApplicationContext的實現類,該實現類從類路徑下加載配置文件
114 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 115 //2.從IOC容器中獲取bean實例 116 //利用id定位到IOC容器中的bean
117 HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld"); 118 //利用類型返回IOC容器中的bean,擔憂求IOC容器中只能有一個該類型的bean 119 //HelloWorld helloWorld = ctx.getBean(HelloWorld.class); 120 //3.調用hello方法
121 helloWorld.hello(); 122 Car car = (Car) ctx.getBean("car"); 123 System.out.println(car); 124 car = (Car) ctx.getBean("car2"); 125 System.out.println(car); 126 Person person = (Person) ctx.getBean("person2"); 127 System.out.println(person); 128 } 129
130 }
(2)me.spring.beans.collections 測試集和屬性
1 package me.spring.beans.collections; 2 3 import java.util.List; 4 5 import me.spring.beans.Car; 6 7 public class Person { 8 9 private String name; 10 private int age; 11 12 private List<Car> cars; 13 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public int getAge() { 23 return age; 24 } 25 26 public void setAge(int age) { 27 this.age = age; 28 } 29 30 31 public Person() { 32 33 } 34 35 public List<Car> getCars() { 36 return cars; 37 } 38 39 public void setCars(List<Car> cars) { 40 this.cars = cars; 41 } 42 43 public Person(String name, int age, List<Car> cars) { 44 super(); 45 this.name = name; 46 this.age = age; 47 this.cars = cars; 48 } 49 50 @Override 51 public String toString() { 52 return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]"; 53 } 54 } 55 56 package me.spring.beans.collections; 57 58 import java.util.Map; 59 60 import me.spring.beans.Car; 61 62 public class NewPerson { 63 64 private String name; 65 private int age; 66 private Map<String, Car> cars; 67 public String getName() { 68 return name; 69 } 70 public void setName(String name) { 71 this.name = name; 72 } 73 public int getAge() { 74 return age; 75 } 76 public void setAge(int age) { 77 this.age = age; 78 } 79 public Map<String, Car> getCars() { 80 return cars; 81 } 82 public void setCars(Map<String, Car> cars) { 83 this.cars = cars; 84 } 85 86 public NewPerson() { 87 // TODO Auto-generated constructor stub 88 } 89 public NewPerson(String name, int age, Map<String, Car> cars) { 90 super(); 91 this.name = name; 92 this.age = age; 93 this.cars = cars; 94 } 95 @Override 96 public String toString() { 97 return "NewPerson [name=" + name + ", age=" + age + ", cars=" + cars + "]"; 98 } 99 100 } 101 102 package me.spring.beans.collections; 103 104 import java.util.Properties; 105 106 public class DataSource { 107 108 private Properties properties; 109 110 public Properties getProperties() { 111 return properties; 112 } 113 114 public void setProperties(Properties properties) { 115 this.properties = properties; 116 } 117 118 @Override 119 public String toString() { 120 return "DataSource [properties=" + properties + "]"; 121 } 122 123 } 124 package me.spring.beans.collections;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) ctx.getBean("person5");
System.out.println(person);
NewPerson newPerson = (NewPerson) ctx.getBean("newPerson");
System.out.println(newPerson);
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getProperties());
}
}