1. 程式人生 > >Spring學習(1)--使用xml配置Bean屬性

Spring學習(1)--使用xml配置Bean屬性

使用xml配置Bean屬性之前需要先定義對應的XML和對應xsd檔案:

<?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">

依賴注入主要有三種方式:

1.setter注入

2.構造器注入

3.介面注入

由於介面注入使用很少,這裡主要寫下前面兩種注入屬性的方式。

1.使用setter方式

Java檔案程式碼:

package com.study.beans;

public class HelloWorld {
	private String name;

	public void setName(String name) {
		System.out.println("setName()"+name);
		this.name = name;
	}
	public HelloWorld(){
		System.out.println("HelloWorld的無參構造器");
	}
	public void hello(){
		System.out.println("hello"+name);
	}
	
}

xml檔案程式碼:
<!--setter方式屬性注入  -->
<bean id="helloWorld" class="com.study.beans.HelloWorld">
	<property name="name" value="zhang"></property>
</bean>

測試程式碼:

public class TestDemo {
	
	@Test
	public void test(){
		//1.載入spring配置檔案,根據配置建立物件
		//ClassPathXmlApplicationContext是ApplicationContext介面的實現類,該類從類路徑下載入配置檔案
		ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
		//2.獲取Bean
		HelloWorld helloWorld=(HelloWorld) app.getBean("helloWorld");
		System.out.println(helloWorld);
	}
}
測試結果:
HelloWorld的無參構造器
setName()zhang

2.使用構造器方式

Java檔案程式碼:

package com.study.beans;

public class CarDemo {
	private String brand;
	private String corp;
	private double price;
	private int maxSpeed;
	
	public void setMaxSpeed(int maxSpeed) {
		this.maxSpeed = maxSpeed;
	}

	public CarDemo(String brand, String corp, double price) {
		super();
		this.brand = brand;
		this.corp = corp;
		this.price = price;
	}

	public CarDemo(String brand, String corp, int maxSpeed) {
		super();
		this.brand = brand;
		this.corp = corp;
		this.maxSpeed = maxSpeed;
	}

	public CarDemo(String brand, String corp, double price, int maxSpeed) {
		super();
		this.brand = brand;
		this.corp = corp;
		this.price = price;
		this.maxSpeed = maxSpeed;
	}

	@Override
	public String toString() {
		return "CarDemo [brand=" + brand + ", corp=" + corp + ", price="
				+ price + ", maxSpeed=" + maxSpeed + "]";
	}
	
}

xml檔案程式碼:
<!--構造器方式屬性注入  -->
<bean id="carDemo" class="com.study.beans.CarDemo">
	<constructor-arg value="BMW"></constructor-arg>
	<constructor-arg value="HEFEI"></constructor-arg>
	<constructor-arg value="6900"></constructor-arg>
</bean>
3.屬性值特殊字元的注入,如xml中的特殊字元"<"">"使用<!CDATA[xxx]>

xml檔案程式碼:

<!--特殊字元可以使用<!CDATA[xxx]>方式注入  -->
<bean id="carDemo1" class="com.study.beans.CarDemo">
	<constructor-arg  index="0" type="String">
		<value><![CDATA[<DGDG>]]></value>
	</constructor-arg>
	<constructor-arg  index="1" type="String">
		<value>822</value>
	</constructor-arg>
	<constructor-arg value="6900" index="2" type="int"></constructor-arg>
</bean>
測試結果:
CarDemo [brand=<DGDG>, corp=822, price=0.0, maxSpeed=6900]
4.null值的注入
<!-- null值使用<null/>注入 -->
<bean id="carDemo2" class="com.study.beans.CarDemo">
 <constructor-arg value="Bmw" index="0" type="String"></constructor-arg>
	<constructor-arg>
	<null/>
	</constructor-arg>
	<constructor-arg >
		<value>822.0</value>
	</constructor-arg>
</bean>
5.使用ref引用其他Bean,引用的是其他Bean的id

java檔案程式碼:

package com.study.beans;

public class Person {
	private String name;
	private int age;
	private CarDemo car;
	
	public void setName(String name) {
		this.name = name;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	public CarDemo getCar() {
		return car;
	}

	public void setCar(CarDemo car) {
		this.car = car;
	}
	
	public Person() {
	}

	public Person(String name, int age, CarDemo car) {
		super();
		this.name = name;
		this.age = age;
		this.car = car;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
	}
}

xml檔案程式碼:

<!-- 使用ref引用其他Bean -->
<bean id="person1" class="com.study.beans.Person">
	<property name="name" value="zhangshiwei"></property>
	<property name="age" value="23"></property>
	<property name="car" ref="carDemo2"></property>
</bean>

6.使用內部Bean
<!-- 內部Bean,只能在內部使用,不能被外部引用 -->
<bean id="person2" class="com.study.beans.Person">
	<property name="name" value="zhangshiwei"></property>
	<property name="age" value="23"></property>
	<property name="car">
		<bean class="com.study.beans.CarDemo">
			<constructor-arg value="Bmw" index="0" type="String"></constructor-arg>
			<constructor-arg><null/></constructor-arg>
			<constructor-arg >
				<value>822.0</value>
			</constructor-arg>
		</bean>
	</property>
</bean>

7.為級聯屬性賦值
<!--為級聯屬性賦值  -->
<bean id="person3" class="com.study.beans.Person">
	<constructor-arg name="name" value="zhangshiwei"></constructor-arg>
	<constructor-arg name="age" value="23"></constructor-arg>
	<constructor-arg name="car" ref="carDemo2"></constructor-arg>
	<property name="car.maxSpeed" value="444"></property>
</bean>

上面程式碼中必須先初始化id為car的Bean,否則報錯

8.為list集合屬性賦值

將person類中的car屬性改為:

public class PersonListCar {
	private String name;
	private int age;
	private List<CarDemo> cars;

<!--為list集合屬性賦值  -->
<bean id="personListCar" class="com.study.beans.PersonListCar">
	<constructor-arg name="name" value="zhangshiwei"></constructor-arg>
	<constructor-arg name="age" value="23"></constructor-arg>
	<constructor-arg name="cars">
		<list>
			<ref bean="carDemo"/>
			<ref bean="carDemo1"/>
			<ref bean="carDemo2"/>
		</list>
	</constructor-arg>
</bean>
測試結果:
PersonListCar [name=zhangshiwei, age=23, cars=[CarDemo [brand=BMW, corp=HEFEI, price=0.0, maxSpeed=6900], 
CarDemo [brand=<DGDG>, corp=822, price=0.0, maxSpeed=6900], CarDemo [brand=Bmw, corp=null, price=822.0, maxSpeed=444]]]

如上面程式碼所示<list>節點引用了其他Bean,若注入的值是基本資料型別,寫法如下:

<list>
<value>list1</value>
	<value>list2</value>
	<value>list3</value>
</list>

set集合和陣列型別的屬性注入方式與list集合類似,區別在與list集合使用<list>節點,set集合使用<set>節點,陣列使用<array>節點。

9.為Map型別屬性賦值

將person類中的car屬性改為:

public class PersonMapCar {
	private String name;
	private int age;
	private Map<String,CarDemo> car;
<!--為Map型別屬性賦值  -->
<bean id="personMapCar" class="com.study.beans.PersonMapCar">
	<constructor-arg name="name" value="zhangshiwei"></constructor-arg>
	<constructor-arg name="age" value="23"></constructor-arg>
	<constructor-arg name="car">
		<map>
			<entry key="car1" value-ref="carDemo"/>
			<entry key="car2" value-ref="carDemo1"/>
			<entry key="car3" value-ref="carDemo2"/>
		</map>
	</constructor-arg>
</bean>
測試結果:
PersonMapCar [name=zhangshiwei, age=23, car={car1=CarDemo [brand=BMW, corp=HEFEI, price=0.0, maxSpeed=6900], 
car2=CarDemo [brand=<DGDG>, corp=822, price=0.0, maxSpeed=6900], car3=CarDemo [brand=Bmw, corp=null, price=822.0, maxSpeed=444]}]

10.配置Properties型別屬性值

Java檔案程式碼:

package com.study.beans;

import java.util.Properties;

public class DataSource {
	private Properties properties;

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	@Override
	public String toString() {
		return "DataSource [properties=" + properties + "]";
	}
	
}

xml檔案程式碼:
<!--配置Properties型別屬性值  -->
<bean id="dataSource" class="com.study.beans.DataSource">
	<property name="properties">
		<props>
			<prop key="user">root</prop>
			<prop key="password">root</prop>
			<prop key="jdbcUrl">jdbc:mysql:///test</prop>
			<prop key="driverClass">com.mysql.jdbc.Driver</prop>
		</props>
	</property>
</bean>
測試結果:
DataSource [properties={driverClass=com.mysql.jdbc.Driver, user=root, password=root, jdbcUrl=jdbc:mysql:///test}]

11.使用util名稱空間,配置獨立的集合Bean,可供其他Bean引用
<!-- 使用util名稱空間,配置獨立的集合Bean,可供其他Bean引用 ,需要先引入util名稱空間-->
<util:list id="car">
	<ref bean="carDemo"/>
	<ref bean="carDemo1"/>
	<ref bean="carDemo2"/>
</util:list>
<bean id="personListCar1" class="com.study.beans.PersonListCar">
	<constructor-arg name="name" value="zhangshiwei"></constructor-arg>
	<constructor-arg name="age" value="23"></constructor-arg>
	<constructor-arg name="cars" ref="car"></constructor-arg>
</bean>
引入的util名稱空間:
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

12.使用p名稱空間為Bean的屬性賦值
<!--使用p名稱空間為Bean的屬性賦值  ,需要先匯入p名稱空間-->
<bean id="personListCar2" class="com.study.beans.PersonListCar"
	p:name="ZhangShiWei" p:age="23" p:cars-ref="car"></bean>
引入的p名稱空間:
xmlns:p="http://www.springframework.org/schema/p"

13.使用c名稱空間注入構造引數的屬性

<!--使用c名稱空間注入構造引數的屬性 ,需要先匯入c名稱空間  -->
<bean id="personListCar3" class="com.study.beans.PersonListCar"
c:_0="ZHANGSHIwei" c:_1="23" c:_2-ref="car"></bean>

引入的c名稱空間:

xmlns:c="http://www.springframework.org/schema/c"