初學者易上手的SSH-spring 01控制反轉(IOC),ssh-springioc
這章開始學習SSH中最後的一個架構spring。Spring是一個開放原始碼的設計層面架構,他解決的是商務邏輯層和其他各層的松耦合問題,因此它將面向介面的編程思想貫穿整個系統應用。
首先就來學習一下IOC,它的好處就是降低了耦合,主要是通過bean之間的依賴關係儘可能地抓換為關聯關係。
那麼來基於maven項目來配置一個spring吧。
首先可以下載一個spring的外掛程式:可在eclipse->Help->Eclipse Marketplace中搜尋spring,然後找到spring tools進行下載即可,其它下載方式就不過多介紹了。
第一步:maven項目中的pom.xml中匯入spring的依賴:spring-context,我以4.3.10版本為例
<!-- spring-context 依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.10.RELEASE</version> </dependency>
第二步:在src/main/resources檔案夾下建立一個xml,取名為applicationContext.xml。在裡面加上spring表標頭檔,如下
<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"></beans>標籤的解釋:xmlns=XML Name spacexmlns: 關於初始化bean的格式檔案地址xmlns:xsi: 輔助初始化beanxsi:context: 關於spring上下文,包括載入資源檔xmlns:tx 事務標籤的約束xmlns:aop aop(面向切面)標籤的約束xsi:schemaLocation: 用於聲明了目標名稱空間的模式文檔
第三步:建立一個人的類,給上name,age屬性,封裝構造,tostring即可。
第四步:開始配置一個bean,來實現IOC。在applicationContext.xml,配置如下:
<bean id="person" class="com.entity.Person"></bean>
//id為這個bean的名稱自己定義就好,class為要配置類的全限定路徑
//當然也可以使用name屬性來代替id屬性,name屬性可以使用特殊符號來定義名稱
第五步:再建立一個類,用main方法或者用junit的@Test來進行測試,測試代碼如下
// 延時載入(懶載入) 返回的對象只有一個 單列模式 // 負載檔案 BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); 擷取bean對象 Person p = (Person) factory.getBean("person"); System.out.println(p);
// 及時載入 一個bean能夠建立多個對象 ApplicationContext aContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 擷取bean對象 Person p = (Person) aContext.getBean("person"); System.out.println(p);
這樣一個簡單的bean就配置完了。
我們可以在bean後面加上一個屬性scope以及init-method屬性。
scope屬性值:
singleton(預設值)
在每個Spring Ioc容器中一個bean定義對應一個對象執行個體(單例模式)
prototype
一個bean定義對應多個對象執行個體.
request
在一次HTTP請求中,一個bean定義對應一個執行個體;即每次HTTP請求將會有各自的bean執行個體,它們依據某個bean定義建立而成。該範圍僅在基於web的Spring ApplicationContext情形下有效.
session
在一個HTTP Session中,一個bean定義對應一個執行個體。該範圍僅在基於web的Spring ApplicationContext情形下有效。
global session
在一個全域的HTTP Session中,一個bean定義對應一個執行個體。典型情況下,僅在使用portlet context的時候有效。該範圍僅在基於web的Spring ApplicationContext情形下有效.
init-method屬性的意思是這個bean初始化的時候調用哪個方法,屬性值為bean對應這個類中的方法名即可。
接下來看下注入方式:
首先bean標籤裡面添加property標籤,name對應屬性名稱,value對應屬性值
1.比如給這個對象的屬性設定預設值
<bean id="person" class="com.entity.Person"><!--屬性設定預設值 --> <property name="name" value="哈哈"></property> <property name="age" value="11"></property></bean>
2.注入一個集合(set,list),以list為例
在persion類中加上一個lsit<string>的屬性,封裝
注入:
<bean id="person" class="com.entity.Person">//ls為list<string>的變數名 <property name="ls"> <list> <value>Google</value> <value>呵呵</value> <value>嘻嘻</value> </list> </property></bean>
3.構造注入
//構造注入時,實體類中需存在相應的構造方法
<bean id="person" class="com.entity.Person"> <constructor-arg name="name" value="1"></constructor-arg> <constructor-arg name="age" value="18"></constructor-arg></bean>
4.注入對象
建立實體類card,給上cid,cname兩個屬性,封裝構造。
在applicationContext.xml中增加一個bean
<bean id="card" class="com.entity.Card" > </bean>
card類中加上persion對象屬性,封裝。
然後在card的bean中注入
<bean id="card" class="com.entity.Card" > //name 物件變數名 <property name="person">//ref對應屬性類在bean的標籤ID <ref bean="person" />
//idref元素的功能與<value>類似,只是idref多了驗證的功能
<!-- <idref bean="person" /> --> </property> </bean>
測試我這裡就沒試了。除了上面這幾種注入,還可以百度看看靜態工廠的方法注入以及執行個體工廠的方法注入。
本章結束!