經過構造函數初始化對象java
建立一個類後,爲類編寫帶參的構造函數(順便要編寫無參的),在spring配置文件中初始化對象。spring
package cn.net.bysoft.lesson1; public class Car { public Car() { } public Car(String name, int maxSpeed, String color) { this.name = name; this.maxSpeed = maxSpeed; this.color = color; } public Car(String name, double price, String color) { this.name = name; this.price = price; this.color = color; } @Override public String toString() { return "Car [name=" + name + ", maxSpeed=" + maxSpeed + ", price=" + price + ", color=" + color + "]"; } //getter and setter ... private String name; private int maxSpeed; private double price; private String color; }
<?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:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!-- 經過構造函數初始化bean對象 --> <bean id="car" class="cn.net.bysoft.lesson1.Car"> <constructor-arg index="0" value="BMW"></constructor-arg> <constructor-arg index="1" value="100" type="int"></constructor-arg> <constructor-arg index="2" value="Red"></constructor-arg> </bean> </beans>
package cn.net.bysoft.lesson1; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Lesson1Test { private ApplicationContext ctx; @Before public void init() { // 建立Spring容器 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testInitByConstructor() { // 使用對象的構造函數初始化對象 Car car = (Car) ctx.getBean("car"); System.out.println(car); /** * output: Car [name=BMW, maxSpeed=100, price=0.0, color=Red] * */ } }
類中有兩個帶參的構造函數。配置文件中使用constructor-arg爲構造函數的參數賦值,能夠使用index來聲明該arg是構造函數中的第幾個參數,在用type來區分參數類型。數據庫
字段中帶有特殊字符app
<!-- 包含特殊字符的參數值 --> <bean id="car2" class="cn.net.bysoft.lesson1.Car"> <constructor-arg index="0"> <value><![CDATA[<Audi>]]></value> </constructor-arg> <constructor-arg index="1" value="500000" type="double"></constructor-arg> <constructor-arg index="2" value="Red"></constructor-arg> </bean>
@Test public void testSpecialCharacter() { // 字段中帶有特殊字符 Car car = (Car) ctx.getBean("car2"); System.out.println(car); /** * output:Car [name=<Audi>, maxSpeed=0, price=500000.0, color=Red] * */ }
若是字段中帶特殊字符,能夠使用<![CDATA[...]]>修飾字符串。less
字段是引用類型ide
若是一個類中的字段是引用類型,好比有一個Person類,該類有一個字段爲Car類型,能夠使用ref屬性進行初始化,具體代碼以下:函數
package cn.net.bysoft.lesson1; public class Person { public Person() { } @Override public String toString() { return "Person [name=" + name + ", car=" + car + "]"; } //getter and setter private String name; private Car car; }
<!-- 初始化引用類型 --> <bean id="person" class="cn.net.bysoft.lesson1.Person"> <property name="name" value="Jack"></property> <!-- 使用ref指向一個bean表明傳遞引用類型,car2在上面的配置文件中配置好了 --> <property name="car" ref="car2"></property> </bean>
@Test public void testInitRef() { Person person = (Person) ctx.getBean("person"); System.out.println(person.toString()); /** * output:Person [name=Jack, car=Car [name=<Audi>, maxSpeed=0, * price=500000.0, color=Red]] * * */ }
還能夠使用內部bean初始化引用類型的字段,內部bean不能夠被引用。this
<!-- 初始化引用類型時內部bean --> <bean id="person2" class="cn.net.bysoft.lesson1.Person"> <property name="name" value="Kobe"></property> <!-- 內部bean --> <property name="car"> <bean class="cn.net.bysoft.lesson1.Car"> <constructor-arg index="0"> <value><![CDATA[<Ferrari>]]></value> </constructor-arg> <constructor-arg index="1" value="1000000" type="double"></constructor-arg> <constructor-arg index="2" value="Black"></constructor-arg> </bean> </property> </bean>
@Test public void testInitRefByInner() { Person person = (Person) ctx.getBean("person2"); System.out.println(person.toString()); /** * output:Person [name=Kobe, car=Car [name=<Ferrari>, maxSpeed=0, * price=1000000.0, color=Black]] * */ }
初始化字段爲NULLspa
<!-- 屬性初始化爲空 --> <bean id="person3" class="cn.net.bysoft.lesson1.Person"> <property name="name" value="Mark"></property> <!-- 引用null --> <property name="car"> <null /> </property> </bean>
@Test public void testInitRefByNull() { Person person3 = (Person) ctx.getBean("person3"); System.out.println(person3.toString()); /** * output:Person [name=Mark, car=null] * */ }
級聯設置引用類型的字段.net
在使用ref引用了對象後,能夠經過car.price,用對象.字段的方式爲引用對象賦值其屬性:
<!-- 級聯設置引用類型的屬性 --> <bean id="person4" class="cn.net.bysoft.lesson1.Person"> <property name="name" value="Mary"></property> <!-- 引用null --> <property name="car" ref="car"></property> <!-- 級聯屬性設置 --> <property name="car.price" value="250000"></property> </bean>
@Test public void testInitRefByCascade() { Person person4 = (Person) ctx.getBean("person4"); System.out.println(person4.toString()); /** * output:Person [name=Mary, car=Car [name=BMW, maxSpeed=100, * price=250000.0, color=Red]] * */ }
初始化集合屬性
有的對象中,存在集合屬性,好比List或者Map。例如,有一個Company類中有兩個屬性,一個是List<Car>一個是Map<Car>。在spring中能夠list和map標籤進行設置,具體以下:
package cn.net.bysoft.lesson1; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Company { public Company() { } @Override public String toString() { return "Company [name=" + name + ", cars=" + cars + ", carList=" + carList + "]"; } // getter and setter private String name; private List<Car> cars = new ArrayList<Car>(); private Map<String, Car> carList = new HashMap<String, Car>(); }
<!-- 初始化集合屬性 --> <bean id="company" class="cn.net.bysoft.lesson1.Company"> <property name="name" value="Spring"></property> <!-- list集合 --> <property name="cars"> <list> <ref bean="car" /> <ref bean="car2" /> </list> </property> <!-- map集合 --> <property name="carList"> <map> <entry key="FirstCar" value-ref="car"></entry> <entry key="SecondCar" value-ref="car2"></entry> </map> </property> </bean>
@Test public void testInitListOrMap() { Company company = (Company) ctx.getBean("company"); System.out.println(company.toString()); /** * output:Company [name=Spring, cars=[Car [name=BMW, maxSpeed=100, * price=250000.0, color=Red], Car [name=<Audi>, maxSpeed=0, * price=500000.0, color=Red]], carList={FirstCar=Car [name=BMW, * maxSpeed=100, price=250000.0, color=Red], SecondCar=Car [name=<Audi>, * maxSpeed=0, price=500000.0, color=Red]}] * */ }
初始化Properties對象
spring爲咱們提供了java.util.Properties對象的初始化功能,例若有一個DataSource類,存放着數據庫的鏈接信息,能夠在spring配置文件中初始化它:
package cn.net.bysoft.lesson1; import java.util.Properties; public class DataSource { public DataSource() { } @Override public String toString() { return "DataSource [prop=" + prop + "]"; } // getter and setter private Properties prop; }
<!-- 配置properties屬性 --> <bean id="dataSource" class="cn.net.bysoft.lesson1.DataSource"> <property name="prop"> <props> <prop key="user">root</prop> <prop key="password">root</prop> </props> </property> </bean>
@Test public void testInitProperties() { DataSource ds = (DataSource) ctx.getBean("dataSource"); System.out.println(ds.toString()); /** * output:DataSource [prop={user=root, password=root}] * */ }
使用util標籤設置公用數據
上訴的初始化集合都是在bean的內部,若是好多個bean都須要初始化一樣的集合就須要定義多邊,spring爲咱們提供了公用數據的配置,在spring配置文件的頂部,導入util的信息:
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
接着配置util:list與util:map的信息:
<!-- 通用數據 --> <util:list id="util-cars-list"> <ref bean="car" /> <ref bean="car2" /> </util:list> <util:map id="util-cars-map"> <entry key="FirstCar" value-ref="car"></entry> <entry key="SecondCar" value-ref="car2"></entry> </util:map> <!-- 初始化集合屬性 --> <bean id="company2" class="cn.net.bysoft.lesson1.Company"> <property name="name" value="Hibernate"></property> <!-- list集合 --> <property name="cars" ref="util-cars-list"></property> <!-- map集合 --> <property name="carList" ref="util-cars-map"></property> </bean>
@Test public void testInitByUtil() { Company company2 = (Company) ctx.getBean("company2"); System.out.println(company2); /** * output:Company [name=Hibernate, cars=[Car [name=BMW, maxSpeed=100... * */ }
使用p標籤
導入p標籤的xmlns,使用p標籤初始化對象的屬性:
xmlns:p="http://www.springframework.org/schema/p"
<bean id="xx" class="xxx.Person" p:car-ref="car" p:name="Nick"></bean>