javaEE學習 ----- spring環境搭建及基礎應用

一、基礎環境搭建


1,導包


 當前只是spring初始階段,只使用spring最核心的包及相關支持。spring關係如圖所示:


最終,導入項目的包結構如圖所示:




2,創建對象


public class Worker {
	// 姓名
	private String name;
	// 薪水
	private Integer salary;

	public void setName(String name) {
		this.name = name;
	}

	public void setSalary(Integer salary) {
		this.salary = salary;
	}

	@Override
	public String toString() {
		return "Worker [name=" + name + ", salary=" + salary + "]";
	}

}


3,書寫配置註冊對象到容器


創建xml文件註冊對象到容器,原則上命名和路徑沒有指定,推薦在src目錄下創建applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">

	<bean name="worker" class="com.future.domain.Worker"></bean>

</beans>


4,代碼測試


創建測試類,並書寫測試代碼:

public class TestDemo {
	/*
	 * 整體流程 
	 * 1,導包 
	 * 2,構建bean類 
	 * 3,將bean與spring關聯【配置】 
	 * 4,獲取bean對象並使用
	 */
	@Test
	public void test() {
		// 創建容器對象
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		//從spring中獲取對象
		Worker worker = (Worker) ac.getBean("worker");
		//使用對象
		System.out.print("worker:"+worker);
		//引出:構造方法的調用,屬性初始化
	}
}
展示結果如圖所示,表示存儲對象及對象調用成功。



二、創建對象的方式


1,通過空參構造方法創建對象


 如上面簡單實例,已經使用空參構造方法創建對象。爲使bean類更趨於完善,添加初始化及銷燬方法。

	// 添加初始化/銷燬方法
	public void init() {
		System.out.println("初始化方法!");
	}

	public void destory() {
		System.out.println("銷燬方法!");
	}


配置文件中做相應的修改,可以實現對bean類的初始化與銷燬。【applicationContext中修改】


	<bean name="worker1" class="com.future.domain.Worker" init-method="init"
		destroy-method="destory">
	</bean>

2,靜態工廠方法


(1)工廠類的實現

public class WorkerFactory {
	// 靜態工廠
	public static Worker createWorker1() {
		System.out.println("靜態工廠創建Worker");
		return new Worker();
	}

	// 實例工廠
	public Worker createWorker2() {
		System.out.println("實例工廠創建Worker");
		return new Worker();
	}
}
 (2)配置文件的修改

	<!-- 2:靜態工廠方法 -->
	<bean name="worker2" class="com.future.create.WorkerFactory"
		factory-method="createWorker1">
	</bean>


3,實例工廠方法


如2所展示,實例工廠方法實現如上,當前修改配置文件實現即可。

	<!-- 3:實例工廠方法 -->
	<bean name="worker3" 
	factory-bean="WorkerFactory" 
	factory-method="createWorker2">
	</bean>

	<bean name="WorkerFactory" 
	class="com.future.create.WorkerFactory">
	</bean>

三、屬性注入


爲以下功能的完善實現,爲基礎bean類添加bean屬性對象。並完善對應構造方法,包含空參,部分參數,部分參數調整位置,全參數構造方法。最後調整實現bean類如下:

public class Worker {
	// 姓名
	private String name;
	// 薪水
	private Integer salary;

	// 添加對象型成員變量
	private House house;
	

	public void setName(String name) {
		this.name = name;
	}

	public void setSalary(Integer salary) {
		this.salary = salary;
	}

	// 添加構造函數
	public Worker() {
		System.out.println("Worker空參構造方法");
	}

	// 添加初始化/銷燬方法
	public void init() {
		System.out.println("初始化方法!");
	}

	public void destory() {
		System.out.println("銷燬方法!");
	}

	// 對象型成員變量
	public House getHouse() {
		return house;
	}

	public void setHouse(House house) {
		this.house = house;
	}

	@Override
	public String toString() {
		return "Worker [name=" + name + ", salary=" + salary + ", house="
				+ house + "]";
	}

	// 創建帶參構造方法
	public Worker(String name, Integer salary) {
		System.out.println("帶參順序構造方法");
		this.name = name;
		this.salary = salary;
	}
	
	public Worker(Integer salary,String name) {
		System.out.println("帶參混亂順序構造方法");
		this.name = name;
		this.salary = salary;
	}
	
	public Worker(Integer salary,String name,House house) {
		System.out.println("全參構造方法");
		this.name = name;
		this.salary = salary;
		this.house = house;
	}
}
其中,bean類屬性的類對象實現如下:

public class House {
	//地標
	private String place; 
	//地段
	private String location;
	
	
	public String getPlace() {
		return place;
	}
	public void setPlace(String place) {
		this.place = place;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	
	@Override
	public String toString() {
		return "House [place=" + place + ", location=" + location + "]";
	}
}


1,通過set方法注入


	<!-- set方法注入 -->
	<bean name="worker1" class="com.future.domain.Worker">
		<property name="name" value="zhangsan"></property>
		<property name="salary" value="16000"></property>
		<!-- 對象型屬性,引用配置好對象 -->
		<property name="house" ref="house"></property>
	</bean>

	<bean name="house" class="com.future.domain.House">
		<property name="place" value="上地華聯"></property>
		<property name="location" value="上地創業大廈"></property>
	</bean>


2,通過構造方法注入


	<!-- 構造函數注入 -->
	<bean name="worker2" class="com.future.domain.Worker">
		<!-- name屬性: 構造函數的參數名 -->
		<!-- index屬性: 構造函數的參數索引 -->
		<!-- type屬性: 構造函數的參數類型 -->
		<constructor-arg name="name" index="1" type="java.lang.String"
			value="lisi"></constructor-arg>
		<constructor-arg name="salary" index="0"
			type="java.lang.Integer" value="999"></constructor-arg>
		<constructor-arg name="house" ref="house" index="2"></constructor-arg>
	</bean>


3,p空間名稱注入


引入p空間:

xmlns:p="http://www.springframework.org/schema/p"
	<!-- p名稱空間注入, 走set方法 
		1.導入P名稱空間 xmlns:p="http://www.springframework.org/schema/p" 
		2.使用p:屬性完成注入 |-值類型: p:屬性名="值" |-對象類型: p:屬性名-ref="bean名稱" -->

	<bean name="worker3" class="com.future.domain.Worker" p:name="jack"
		p:salary="20000" p:house-ref="house">
	</bean>


4,spel注入


	<!-- spel注入: spring Expression Language sping表達式語言 -->
	<bean name="worker4" class="com.future.domain.Worker">
		<property name="name" value="#{worker2.name}"></property>
		<property name="salary" value="#{worker3.salary}"></property>
		<property name="house" ref="house"></property>
	</bean>


5,複雜數據注入


(1)複雜數據的bean對象實現

//複雜數據類型注入
public class CollectionBean {
	//數組
	private String[] arr;
	//集合
	private List list;
	//key-value
	private Map map;
	//屬性
	private Properties prop;
	
	
	public String[] getArr() {
		return arr;
	}
	public void setArr(String[] arr) {
		this.arr = arr;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public Map getMap() {
		return map;
	}
	public void setMap(Map map) {
		this.map = map;
	}
	public Properties getProp() {
		return prop;
	}
	public void setProp(Properties prop) {
		this.prop = prop;
	}
	
	@Override
	public String toString() {
		return "CollectionBean [arr=" + Arrays.toString(arr) + ", list=" + list
				+ ", map=" + map + ", prop=" + prop + "]";
	}
}
(2)數據注入實現

<bean name="collectionBean" class="com.future.domain.CollectionBean">
		<!-- 數組 -->
		<property name="arr">
			<array>
				<value>hello</value>
				<value>world</value>
				<value>javaEE</value>
			</array>
		</property>
		<!-- list 集合 -->
		<property name="list">
			<list>
				<value>畫畫</value>
				<value>唱歌</value>
				<value>寫字</value>
				<value>冥想</value>
			</list>
		</property>

		<!-- key-value -->
		<property name="map">
			<map>
				<entry key="臉爲什麼黃了" value="防冷塗的蠟"></entry>
				<entry key="天王蓋地虎" value="寶塔鎮河妖"></entry>
			</map>
		</property>
		
		<!-- 屬性值 -->
		<property name="prop">
			<props>
				<prop key="用戶名">root</prop>
				<prop key="密碼">123456</prop>
				<prop key="驗證碼">天上掉下個林妹妹</prop>
			</props>
		</property>
	</bean>
整體數據注入測試類實現:

//測試注入屬性
public class TestInjection {

	// set方法注入
	@Test
	public void test1() {
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"/com/future/injection/applicationContext.xml");
		Worker worker1 = (Worker) ac.getBean("worker1");
		System.out.println("worker1:" + worker1);
	}

	// 構造方法注入
	@Test
	public void test2() {
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"/com/future/injection/applicationContext.xml");
		Worker worker2 = (Worker) ac.getBean("worker2");
		System.out.println("worker2:" + worker2);
	}

	// p名稱空間注入
	@Test
	public void test3() {
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"/com/future/injection/applicationContext.xml");
		Worker worker3 = (Worker) ac.getBean("worker3");
		System.out.println("worker3:" + worker3);
	}

	// spel注入
	@Test
	public void test4() {
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"/com/future/injection/applicationContext.xml");
		Worker worker4 = (Worker) ac.getBean("worker4");
		System.out.println("worker4:" + worker4);
	}

	// 複雜數據注入
	@Test
	public void test5() {
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"/com/future/injection/applicationContext.xml");
		CollectionBean collectionBean = (CollectionBean) ac
				.getBean("collectionBean");
		System.out.println("collectionBean:" + collectionBean);
	}

}
其中部分測試結果:





源碼傳送門




愛在左,情在右,走在生命的兩旁。隨時撒種,隨時開花,將這一路長徑點綴得花香瀰漫,使穿枝拂葉的行人,踏着荊棘,不覺得痛苦,有淚可落,卻不是淒涼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章