Spring4入門&常用配置

 

1.下載Spring的壓縮文件

解壓dist:

/docs中是Spring的文檔,lib中有Spring的各種功能的jar包包含文檔jar包和源文件jar包。

由圖可知:Spring的核心包括beans、core、context、spEL。環境配置:在/libs中找到對應的jar包放到項目的/lib中

還需要導入日誌的包,在依賴庫中找:

 

Spring叫做EE開發的一站式框架。一站式框架有EE開發的每一層解決方案。

     WEB層:SpringMVC

     Service層:Spring的Bean管理,Spring聲明式事務

     Dao層:Spring的JDBC模板,Spring的ORM模板

 

 

Spring  IOC入門

         IOC(Inversion of Control):將對象的創建權交給Spring管理。

傳統的設計在需要對業務擴展的時候不得不修改低層源代碼,因爲面向接口編程接口和實現類有耦合。使用工廠模式可以解除接口和實現類的耦合,但是接口和工廠有耦合。 

使用工廠+反射+配置文件   可以實現文件的解耦和。(IOC底層實現)

創建配置文件:Spring默認文件名採用applicationContext.xml命名;

applicationContext.xml配置文件書寫:引入約束:spring-framework-4.2.4.RELEASE/docs/spring-framework-reference/html/xsd-configuration.html  找個the  beans schema將<beans>標籤複製。  在beans標籤中配置。


基礎IOC測試:

1.創建需要被控制的對象:

public class Student {

	private String name ;
	private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	public void annoce() {
		System.out.println("Student被調用了");
	}
}

2.在applicationContext.xml中配置bean 

<?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">
    <bean name="stu" class="com.aloha.spring.test1.Student">
    </bean>
</beans>

3.創建測試類

	@Test
	public void Test1(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Student student = (Student) context.getBean("stu");
		student.annoce();
	}

測試結果:

IOC和DI

IOC將對象的控制權反轉給Spring。

DI依賴注入,前提是得有IOC環境。Spring管理這個類的時候將類依賴的屬性注入(設置)進來。

 

面向對象:

          1.依賴:如果B類裏創建了A類的對象,那麼說B類對A類有依賴關係

          2.繼承:

          3.聚合:鬆散聚合和緊密聚合

<!-- 對於Student中的屬性name採用依賴注入的方式 -->
    <bean name="stu" class="com.aloha.spring.test1.Student">
    	<property name="name" value="aloha"></property>
    </bean>

這時就把name的值設置爲“aloha”了。

 

Spring的工廠類

BeanFactory:老版本工廠類 調用getBean()的時候,纔會生成類的實例
ApplicationContext:新版本工廠類(現在使用) 加載配置文件的時候就會將Spring管理的類都實例化

 

可以看到ApplicationContext間接繼承了BeanFactory。

ApplicationContext有兩個繼承者FileSystemXmlApplicationContext和ClassPathXmlApplicationContext。

 

Spring配置

<bean>標籤的屬性:

屬性名 特點
id 唯一約束,不能出現特殊字符
name 理論不唯一,實際開發要唯一,可以出現特殊字符(老版本中常用)
init-method 隨便寫值,值即是類中的方法名
destroy-method 單例工廠關閉了纔會執行小鬼
scope bean作用範圍的配置

Bean作用範圍配置(scope屬性):

singleton(默認):單例創建對象。

prototype:多例創建對象。常用於Struts2整合Spring,因爲Action是多例。

request:web項目中,Spring創建類後將對象存入request域中。

session:web項目中,Spring創建類後將對象存入session域中。

globalsession:web項目中,porlet環境下使用。作用於所有子網。

 

Spring屬性注入方式(DI依賴注入)

給bean中屬性設置值有三種方式:

①有參構造方法方式

在java類中提供需要的有參構造,在Spring配置文件ApplicationContext中加入如下內容:

<bean name="給類命名" class="類的全路徑">
    <constructor-arg name="類的屬性1" value="屬性的值1"/>
    <constructor-arg name="類的屬性2" value="屬性的值2"/>
    ......
</bean>

②get方法方式

            在java類中提供屬性的get方法,在Spring配置文件ApplicationContext中加入如下內容:

<bean name="給類命名" class="類的全路徑">
    <property name="類的屬性1" value="屬性的值1"/>
    <property name="類的屬性2" value="屬性的值2"/>
    <property name="對象屬性" ref="寫對象類的id或name"/>
    ......
</bean>

③接口注入方式

空。

 

p名稱空間屬性注入:

首先在<beans ...>中加入屬性xml名稱空間   xmlns:p="http://www.springframework.org/schema/p"

<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:p="http://www.springframework.org/schema/p"
       ......>

p名稱空間引入成功。

p名稱空間直接寫在<bean>標籤的屬性位置  

普通屬性 對象屬性
p:屬性名 = "值" p:屬性名-ref = "值"
    <!-- p名稱空間注入方式 -->
    <bean name="pstu" class="com.aloha.spring.test1.Student" p:name="ns_aloha" p:car-ref="car"></bean>

SpEl屬性 ——Spring Exception Language

語法:#{SpEL}

需要注意:在SpEL中的<property>標籤中沒有ref屬性,這裏的對象值依然使用value屬性。

SpEL處可以填基本類型、引用類型、對象.屬性、對象.方法......

數組、集合屬性注入

是數組還是集合取決於java類中定義的屬性是哪種類型,容器類型可以是數組,list,set,map。

每一個標籤裏裝的是數組/集合中的一個(對)值。

<list>
    <value></value>普通數組/集合填入value標籤中
    <ref><ref>對象數組填入ref標籤
</list>

map集合這樣配置: (還有一種複雜方式沒有列出)

<map>
    <entry key="鍵名" value="值名" />
    <entry key="鍵名" value="值名" />
    .......
</map>

分模塊開發

①分模塊就是每部分使用自己的配置文件,ClassPathXmlApplicationContext(String ...)這是一個可變參數的方法; 這個方法可以一次加載多個配置文件,在傳附件IOC的時候引入所有有關配置文件:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext1.xml","applicationContext2.xml");

②在一個配置文件中導入多個配置文件:

<import resource = "applicationContext.xml" />

resource屬性的值是需要導入xml文件的相對路徑。

 

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