重視java編程思想去學(xué)習(xí)spring框架。Spring輕量級(jí)容器。作用于整個(gè)三層架構(gòu)(struts2作用于web.hibrenate作用于dao層是重量級(jí))
struts2作用于web層。hibernate作用于刀層勤家。spring作用于整個(gè)三層架構(gòu)。
三層架構(gòu);
- IOC 控制反轉(zhuǎn)(依賴(lài)注入)
- AOP 面向切面編程 事務(wù)
搭建Spring環(huán)境
所需要的資源Spring.這里是我們所需要的整個(gè)資源锅必。我們一般開(kāi)發(fā)中只需其中的某些包就可以,所以我們一般導(dǎo)入自己需要的包惕艳,在這里我將常用的包能解決一般開(kāi)發(fā)問(wèn)題所需要的包提取在jar文件中搞隐。這里我們導(dǎo)入jar下面的所有包。為了在寫(xiě)配置文件中有代碼提示远搪,我們還需要導(dǎo)入兩個(gè).xsd文件劣纲。在schema目錄下。我們將這兩個(gè)東西也導(dǎo)入到項(xiàng)目中谁鳍,在xml catalog中進(jìn)行關(guān)聯(lián)癞季,注意關(guān)聯(lián)的時(shí)候Key type選擇URI,KEY值我們是在配置文件中尋找那兩個(gè)KEY倘潜。如:
http://www.springframework.org/schema/beans
將上面步驟整理好后绷柒,我們先新建一個(gè)包在新建一個(gè)類(lèi)Student。給定相應(yīng)的屬性后涮因,我們還需要添加get/set方法废睦。給出了有參構(gòu)造函數(shù),必須在添加無(wú)參構(gòu)造函數(shù)养泡。
-
在寫(xiě)一個(gè)配置文件嗜湃。文件可以起任意名字。bean.xml;
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> </beans>
這個(gè)文件的頭部聲明可以在下載的spring文檔中的示例找到.注意這里面用到了倆個(gè)schema文件(.xsd文件),就是spring-beans-3.2.xsd和spring-context-3.2.xsd,這倆個(gè)文件在下載的spring文檔中也是可以找到的.我們?cè)贓clipse中把xml文件和schema文件關(guān)聯(lián)(注意上課的演示步驟)后,在這個(gè)xml中就可以有智能提示了瓤荔。還有一點(diǎn)需要注意,將來(lái)我們這xml中再引入另外的schema文件后在配置的時(shí)候就可以智能提示出新的配置內(nèi)容,spring是又很多模塊組成的,我們需要什么功能的配置標(biāo)簽就需要引入對(duì)應(yīng)的schema文件,從而獲得智能提示,同時(shí)也能幫我們驗(yàn)證配置的正確性
-
在這里我們?cè)?lt;bean>添加子標(biāo)簽配置bean類(lèi)净蚤。
<bean name="student" class="com.zts.ioctest.Student" scope="prototype"> <!--這里的name屬性的值就是,setName,去掉set输硝,首字母小寫(xiě) --> <property name="name" value="tom"> </property> </bean>
-
測(cè)試(我們?cè)贛ain方法中測(cè)試)
//beanfactory 功能簡(jiǎn)陋一般不用 //所以我們一般拿 ApplicationContext ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml"); Student bean = (Student) ac.getBean("student"); String name = bean.getName(); System.out.println(name); System.out.println(bean);
Spring詳細(xì)分析
軟件應(yīng)用分層架構(gòu)
標(biāo)準(zhǔn)三層架構(gòu):
- 數(shù)據(jù)訪問(wèn)層:主要是對(duì)原始數(shù)據(jù)(數(shù)據(jù)庫(kù)或者文本文件等存放數(shù)據(jù)的形式)的操作層,而不是指原始數(shù)據(jù),也就是說(shuō),是對(duì)數(shù)據(jù)的操作,而不是數(shù)據(jù)庫(kù),具體為業(yè)務(wù)邏輯層或表示層提供數(shù)據(jù)服務(wù).
- 業(yè)務(wù)邏輯層:主要是針對(duì)具體的問(wèn)題的操作,也可以理解成對(duì)數(shù)據(jù)層的操作,對(duì)數(shù)據(jù)業(yè)務(wù)邏輯處理,如果說(shuō)數(shù)據(jù)層是積木,那邏輯層就是對(duì)這些積木的搭建今瀑。具體的講主要負(fù)責(zé)對(duì)數(shù)據(jù)層的操作。也就是說(shuō)把一些數(shù)據(jù)層的操作進(jìn)行組合点把。
- 表示層:主要表示W(wǎng)EB方式,如果邏輯層相當(dāng)強(qiáng)大和完善,無(wú)論表現(xiàn)層如何定義和更改,邏輯層都能完善地提供服務(wù)橘荠。主要對(duì)用戶(hù)的請(qǐng)求接受,以及數(shù)據(jù)的返回,為客戶(hù)端提供應(yīng)用程序的訪問(wèn)。
優(yōu)點(diǎn):
- 開(kāi)發(fā)人員可以只關(guān)注整個(gè)結(jié)構(gòu)中的其中某一層郎逃;
- 可以很容易的用新的實(shí)現(xiàn)來(lái)替換原有層次的實(shí)現(xiàn)哥童;
- 可以降低層與層之間的依賴(lài);
- 有利于標(biāo)準(zhǔn)化褒翰;
- 利于各層邏輯的復(fù)用贮懈。
- 結(jié)構(gòu)更加的明確
- 在后期維護(hù)的時(shí)候,極大地降低了維護(hù)成本和維護(hù)時(shí)間
缺點(diǎn):
- :降低了系統(tǒng)的性能匀泊。這是不言而喻的。如果不采用分層式結(jié)構(gòu),很多業(yè)務(wù)可以直接造訪數(shù)據(jù)庫(kù),以此獲取相應(yīng)的數(shù)據(jù),如今卻必須通過(guò)中間層來(lái)完成朵你。
- 有時(shí)會(huì)導(dǎo)致級(jí)聯(lián)的修改各聘。這種修改尤其體現(xiàn)在自上而下的方向。如果在表示層中需要增加一個(gè)功能,為保證其設(shè)計(jì)符合分層式結(jié)構(gòu),可能需要在相應(yīng)的業(yè)務(wù)邏輯層和數(shù)據(jù)訪問(wèn)層中都增加相應(yīng)的代碼抡医。
- 增加了開(kāi)發(fā)成本躲因。
IOC注入
set方式注入(必須依靠set方法)
可以注入的內(nèi)容有:
- 基本類(lèi)型(8中基本類(lèi)型+字符串)的裝配
- 對(duì)象類(lèi)型的裝配
- 集合的裝配
基本類(lèi)型的裝配
方式: 配置元素<value/>
例子:
public class HelloBean {
private String name;
private int age;
public String sayHello(){
return "hello "+name +",your age is" + age;
}
.............
}
配置文件set.xml
<bean id="helloBean" class="ioc.HelloBean">
<property name="name">
<value>tom</value>
</property>
<property name="age" value="20">
</property>
</bean>
<!--
id是Bean的唯一標(biāo)識(shí),要求在整個(gè)配置文件中要唯一,也可使用name屬性,bean標(biāo)簽里面的id和name屬性都可以用來(lái)標(biāo)識(shí)這個(gè)配置的對(duì)象,但是id會(huì)幫我們檢查給對(duì)象起的名字是否規(guī)范(名字不能重復(fù)、不能用數(shù)字開(kāi)頭忌傻、不能有空格等等),如果檢查出來(lái)了那么就會(huì)報(bào)錯(cuò)大脉。name屬性不會(huì)幫檢查這些東西。
-->
<!--property 對(duì)于所有用set方式來(lái)注入的必須改標(biāo)簽-->
<!--value 是對(duì)以基本類(lèi)型,都用value(標(biāo)簽/屬性)來(lái)注入,可以實(shí)現(xiàn)自動(dòng)的數(shù)據(jù)類(lèi)型轉(zhuǎn)換-->
測(cè)試類(lèi):
main:
ApplicationContext ac =
new ClassPathXmlApplicationContext("set.xml");
//獲取容器的一個(gè)實(shí)例
HelloBean hb = (HelloBean) ac.getBean("helloBean");
System.out.println(hb.sayHello());
對(duì)象類(lèi)型的裝配
<ref local=" "/> 用于涉及的對(duì)象的id在當(dāng)前配置文件中(用于在本配置文件中配置了的bean的引入同ref="..")
<ref bean=" "/> 用于涉及的對(duì)象的id不在本配置文件中
(用于引用不在本配置文件中配置的bean)-
使用property的ref屬性引用
public class OtherBean { private String str1; public String getStr1() { return str1; } public void setStr1(String str1) { this.str1 = str1; } public String toString(){ return "OtherBean "+str1; } } public class SomeBean { private OtherBean ob; public void printInfo(){ System.out.println("someBean "+ob); } public OtherBean getOb() { return ob; } public void setOb(OtherBean ob) { this.ob = ob; } }
配置applicationContext.xml
<bean id="someBean" class="ioc.SomeBean">
<property name="ob">
<ref bean="otherBean" />
</property>
</bean>
配置other.xml文件
<bean id="otherBean" class="ioc.OtherBean">
<property name="str1">
<value>string1</value>
</property>
</bean>
測(cè)試類(lèi):
main:
String[] path = {"ioc/applicationContext.xml","ioc/other.xml"};
ApplicationContext ac = new ClassPathXmlApplicationContext(path);
SomeBean sb = (SomeBean) ac.getBean("someBean");
sb.printInfo();
集合的裝配
方式:配置元素<list> <set> <map> <props>
public class SomeBean {
private List listProperty;
private Set setProperty;
private Map mapProperty;
private Properties<String,String> property;
public List getListProperty() {
return listProperty;
}
public void setListProperty(List listProperty) {
this.listProperty = listProperty;
}
public Set getSetProperty() {
return setProperty;
}
public void setSetProperty(Set setProperty) {
this.setProperty = setProperty;
}
public Map getMapProperty() {
return mapProperty;
}
public void setMapProperty(Map mapProperty) {
this.mapProperty = mapProperty;
}
public Properties getProperty() {
return property;
}
public void setProperty(Properties property) {
this.property = property;
}
public void printInfo(){
System.out.println("listProperty");
System.out.println(listProperty);
System.out.println("setProperty");
System.out.println(setProperty);
Set set = mapProperty.entrySet();
Iterator it = set.iterator();
while(it.hasNext()){
Map.Entry entry = (Entry) it.next();
System.out.println("Key " +entry.getKey() );
System.out.println("value "+entry.getValue());
}
System.out.println("props: ");
Set set2 = property.entrySet();
Iterator it2 = set2.iterator();
while(it2.hasNext()){
Map.Entry entry= (Entry) it2.next();
System.out.println("key "+entry.getKey());
System.out.println("value "+entry.getValue());
}
}
}
applcationContext.xml的寫(xiě)法:
<bean id="someBean" class="ioc.SomeBean">
<property name="listProperty">
<list>
<value>list1</value>
<value>list1</value>
<value>list3</value>
</list>
</property>
<property name="setProperty">
<set>
<value>set1</value>
<value>set1</value>
<value>set3</value>
</set>
</property>
<property name="mapProperty">
<map>
<entry key="key1">
<value>value1</value>
</entry>
<entry key="key2">
<value>value2</value>
</entry>
</map>
</property>
<property name="property">
<props>
<prop key="key1">prop1</prop>
<prop key="key2">prop2</prop>
<prop key="key3">prop3</prop>
</props>
</property>
</bean>
測(cè)試類(lèi):
main:
String path = "ioc/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(path);
SomeBean sb = (SomeBean) ac.getBean("someBean");
sb.printInfo();
基于構(gòu)造器注入
方式: 配置<constructor-arg>元素
在Bean中不用寫(xiě)set方法,但是要有相應(yīng)的構(gòu)造器
構(gòu)造器注入有倆種形式 一個(gè)是根據(jù)參數(shù)類(lèi)型 一個(gè)是根據(jù)參數(shù)位置的下標(biāo)
<constructor-arg type="int" value="">
<constructor-arg index="0" value="">
例如:
<bean name="student" class="com.briup.bean.Student">
<constructor-arg type="int" value="25">
</constructor-arg>
<constructor-arg type="java.lang.String" value="tom">
</constructor-arg>
<constructor-arg type="long" value="100">
</constructor-arg>
</bean>
或者:
<bean name="student" class="com.briup.bean.Student">
<constructor-arg index="2">
<value>30</value>
</constructor-arg>
<constructor-arg index="0">
<value>200</value>
</constructor-arg>
<constructor-arg index="1">
<value>lily</value>
</constructor-arg>
</bean>
自動(dòng)注入 :容器依照一些規(guī)則去裝配bean中的一個(gè)屬性
注意:自動(dòng)裝配只對(duì)[對(duì)象類(lèi)型]起作用,對(duì)基本類(lèi)型不起作用.
第一種情況:
在beans標(biāo)簽中配置裝載方式:default-autowire="byName"
default-autowire="byName"
在根元素beans中加入這個(gè)屬性,那么下面所有的bean都會(huì)
使用byName的方式進(jìn)行自動(dòng)注入,如果在下面的某一個(gè)bean
里面想使用其他的方式進(jìn)行注入,可以用autowire=""屬性進(jìn)行
說(shuō)明,或者某一個(gè)bean不想使用任何自動(dòng)注入就使用autowire="no"
第二種情況:
-
在bean標(biāo)簽中指定配置方式
autowire="byName":
spring容器會(huì)到當(dāng)前的類(lèi)中找property的名字,然后
再根據(jù)這個(gè)名字去spring容器中找有沒(méi)有和這個(gè)property
名字相同的對(duì)象,有的話,就把這個(gè)對(duì)象當(dāng)做參數(shù)放到
setXxxx這個(gè)方法里面注入進(jìn)來(lái).
注意:了解property指的類(lèi)中的什么東西水孩。autowire="byType":
spring容器會(huì)根據(jù)當(dāng)前類(lèi)中的set方法里面參數(shù)的類(lèi)型,
去容器中找相匹配的對(duì)象,如果沒(méi)找到就算了,如果找到
一個(gè)就注入進(jìn)來(lái),如果找到多個(gè),那么就會(huì)報(bào)錯(cuò)了.
autoWrite="constructor"
根據(jù)構(gòu)造器的參數(shù)類(lèi)型去匹配
繼承:并不是oo的繼承關(guān)系
bean的定義的繼承,指bean的配置可去繼承
true 抽象化 代碼中不能getBean獲取其對(duì)象
abstract =
false 默認(rèn)
parent = "父類(lèi)bean的id/name"
例子:
<bean name="student" class="com.briup.bean.Student">
<property name="name">
<value>zhangsan</value>
</property>
</bean>
<!--
abstract="true" 表示當(dāng)前的配置是一個(gè)抽象的配置,
這時(shí)候我們?cè)诖a中就不能通過(guò)這個(gè)bean的名字teacher來(lái)
獲得相應(yīng)的對(duì)象了(和java中的抽象類(lèi)不能直接new對(duì)象的道理一樣)
但是我們可以在寫(xiě)一個(gè)配置去繼承這個(gè)抽象的配置,當(dāng)然即使當(dāng)前
這個(gè)配置不是抽象的,也能夠被繼承(和java中繼承一樣)
-->
<bean name="teacher" class="com.briup.bean.Teacher" abstract="true">
<property name="student" ref="student"></property>
</bean>
<!--
parent="teacher" 表示當(dāng)前配置是繼承了另外一個(gè)名字叫
teacher的bean的配置,配置和配置的繼承像java中的類(lèi)和類(lèi)
直接的繼承一樣,子類(lèi)會(huì)把父類(lèi)中的對(duì)象繼承過(guò)來(lái).
當(dāng)然在子配置里面依然是可以覆蓋父配置中已經(jīng)寫(xiě)的配置信息.
-->
<bean name="t" parent="teacher">
<property name="id">
<value>11</value>
</property>
<property name="name">
<value>TeacherWang</value>
</property>
</bean>
bean對(duì)象的生命周期
生命周期執(zhí)行的過(guò)程如下:
- spring對(duì)bean進(jìn)行實(shí)例化,默認(rèn)bean是單例
- 2)spring對(duì)bean進(jìn)行依賴(lài)注入
- 3)如果bean實(shí)現(xiàn)了BeanNameAware接口,spring將bean的id傳給setBeanName()方法
- 如果bean實(shí)現(xiàn)了BeanFactoryAware接口,spring將調(diào)用setBeanFactory方法,將BeanFactory實(shí)例傳進(jìn)來(lái)
- 如果bean實(shí)現(xiàn)了ApplicationContextAware()接口,spring將調(diào)用setApplicationContext()方法將應(yīng)用上下文的引用傳入
- 如果bean實(shí)現(xiàn)了BeanPostProcessor接口,spring將調(diào)用它們的postProcessBeforeInitialization接口方法
- 如果bean實(shí)現(xiàn)了InitializingBean接口,spring將調(diào)用它們的afterPropertiesSet接口方法,類(lèi)似的如果bean使用了init-method屬性聲明了初始化方法,改方法也會(huì)被調(diào)用
- 如果bean實(shí)現(xiàn)了BeanPostProcessor接口,spring將調(diào)用它們的postProcessAfterInitialization接口方法
- 此時(shí)bean已經(jīng)準(zhǔn)備就緒,可以被應(yīng)用程序使用了,他們將一直駐留在應(yīng)用上下文中,直到該應(yīng)用上下文被銷(xiāo)毀
- 若bean實(shí)現(xiàn)了DisposableBean接口,spring將調(diào)用它的distroy()接口方法镰矿。同樣的,如果bean使用了destroy-method屬性聲明了銷(xiāo)毀方法,則該方法被調(diào)用
其實(shí)很多時(shí)候我們并不會(huì)真的去實(shí)現(xiàn)上面說(shuō)描述的那些接口,那么下面我們就除去那些接口針對(duì)bean的單例和非單例來(lái)描述下bean的生命周期:
-
單例管理的對(duì)象:
- 默認(rèn)情況下,spring在讀取xml文件的時(shí)候,就會(huì)創(chuàng)建對(duì)象。
- 在創(chuàng)建的對(duì)象的時(shí)候(先調(diào)用構(gòu)造器),會(huì)去調(diào)用init-method=".."
屬性值中所指定的方法. - 對(duì)象在被銷(xiāo)毀的時(shí)候,會(huì)調(diào)用destroy-method="..."屬性值中
所指定的方法.(例如調(diào)用container.destroy()方法的時(shí)候) - lazy-init="true",可以讓這個(gè)對(duì)象在第一次被訪問(wèn)的時(shí)候創(chuàng)建
-
非單例管理的對(duì)象:
- spring讀取xml文件的時(shí)候,不會(huì)創(chuàng)建對(duì)象.
- 在每一次訪問(wèn)這個(gè)對(duì)象的時(shí)候,spring容器都會(huì)創(chuàng)建這個(gè)對(duì)象,并且
調(diào)用init-method=".."屬性值中所指定的方法. - 對(duì)象銷(xiāo)毀的時(shí)候,spring容器不會(huì)幫我們調(diào)用任何方法,
因?yàn)槭欠菃卫?這個(gè)類(lèi)型的對(duì)象有很多個(gè),spring容器一旦把
這個(gè)對(duì)象交給你之后,就不再管理這個(gè)對(duì)象了.