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

來源:互聯網
上載者:User

一、基礎環境搭建


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;}@Overridepublic 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對象並使用 */@Testpublic 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,靜態Factory 方法


(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:靜態Factory 方法 --><bean name="worker2" class="com.future.create.WorkerFactory"factory-method="createWorker1"></bean>


3,執行個體Factory 方法


如2所展示,執行個體Factory 方法實現如上,當前修改設定檔實現即可。

<!-- 3:執行個體Factory 方法 --><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;}@Overridepublic 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;}@Overridepublic 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-valueprivate 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;}@Overridepublic 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方法注入@Testpublic void test1() {ApplicationContext ac = new ClassPathXmlApplicationContext("/com/future/injection/applicationContext.xml");Worker worker1 = (Worker) ac.getBean("worker1");System.out.println("worker1:" + worker1);}// 構造方法注入@Testpublic void test2() {ApplicationContext ac = new ClassPathXmlApplicationContext("/com/future/injection/applicationContext.xml");Worker worker2 = (Worker) ac.getBean("worker2");System.out.println("worker2:" + worker2);}// p名稱空間注入@Testpublic void test3() {ApplicationContext ac = new ClassPathXmlApplicationContext("/com/future/injection/applicationContext.xml");Worker worker3 = (Worker) ac.getBean("worker3");System.out.println("worker3:" + worker3);}// spel注入@Testpublic void test4() {ApplicationContext ac = new ClassPathXmlApplicationContext("/com/future/injection/applicationContext.xml");Worker worker4 = (Worker) ac.getBean("worker4");System.out.println("worker4:" + worker4);}// 複雜資料注入@Testpublic void test5() {ApplicationContext ac = new ClassPathXmlApplicationContext("/com/future/injection/applicationContext.xml");CollectionBean collectionBean = (CollectionBean) ac.getBean("collectionBean");System.out.println("collectionBean:" + collectionBean);}}
其中部分測試結果:





源碼傳送門




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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.