附上示例程序的github地址:https://github.com/bjtudujunlin/SpringDataExample
1、 Spring配置方式
Spring常用的配置方式有兩種,基於XML的配置和基於Java的配置。XML配置是早期Spring採用的一種方式,後來出現了強大的基於Java的配置,從此逃離了大量XML文件的編寫工作,但是鑑於已經存在那麼多基於XML的Spring配置,所以理解如何在Spring中使用XML還是很重要的。所以這裏對兩種方式都進行了介紹。
2、 XML配置
XML配置可以結合《Spring框架系列(一)-整體架構》中IOC的例子進行理解。
A、 首先需要創建XML的配置規範
在使用XML時,需要在配置文件的頂部聲明多個XML模式(XSD)文件,這些文件定義了配置Spring的XML元素,也就是告訴系統怎麼去解析XML文件中的元素標籤,如果缺少了對應的XSD文件,系統會提示“無法找到元素 'XXX'的聲明”。
下面來看看具體例子。
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> </beans> |
在示例中,beans是整個配置文件的根節點,內部可以包含一個或者多個bean節點。
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
顧名思義,xmlns指的就是xml配置的命名空間,上面這兩個是Spring最基本的命名空間定義,所有的xml配置中,這兩個是必不可少的。
xmlns:aop定義了啓用AOP功能時的命名空間,xmlns:context定義了啓用自動掃描或者註解裝配時的命名空間,另外還有xmlns:tx,這裏沒有列出來,定義了啓用事務時的命令空間。
xsi:schemaLocation屬性定義了上述命名空間配套的xsd文件裝載路徑,該屬性必須是偶數行,奇數行表示命名空間,相鄰偶數行表示xsd文件裝載路徑,例如
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
在創建XML配置規範的時候,需要根據具體需要來添加命名空間和xsd文件。
B、 聲明Bean
聲明一個Bean很簡單,如下所示,添加bean元素,指定class屬性就行,該屬性是全限定的類名。
<bean class="iscas.springstudy.Person"></bean> |
因爲沒有明確給定ID,所以這個bean將會根據全限定類名來進行命名。在本例中,bean的ID將會是“iscas.springstudy.Person#0
”。其中,“#0
”是一個計數的形式,用來區分相同類型的其他bean。如果你聲明瞭另外一個Person,並且沒有明確進行標識,那麼它自動得到的ID將會是“iscas.springstudy.Person #1
”。爲了便於記憶,用戶往往需要自己設定bean的id,這可以通過id屬性來指定,如下:
<bean id="person" class="iscas.springstudy.Person"> |
需要注意的是,在沒有特殊處理的情況下,Spring配置的Bean默認爲單例模式,就是說整個context只有一個id爲“xx”的bean,通過getBean方法獲取到的都是這一個bean。
C、 藉助構造器注入初始化bean
聲明瞭Bean,緊接着我們需要利用構造函數初始化這個bean,這也是可以配置的。Person類的構造函數定義如下:
public Person(Region region,String discribe) |
所以我們需要注入兩個構造參數,內容如下
<bean id="person" class="iscas.springstudy.Person"> <constructor-arg ref="regiona"></constructor-arg> <constructor-arg value="a funny man"></constructor-arg> </bean> |
通過constructor-arg屬性可以完成構造函數配置,按照構造函數參數從左到右的順序,ref標籤表示參數爲指定id的bean,value標籤表示參數值是一個字符串。
D、設置屬性
構造函數有了,我們還需要爲Bean配置屬性,內容如下
<bean id="person" class="iscas.springstudy.Person"> <constructor-arg ref="regiona"></constructor-arg> <constructor-arg value="a funny man"></constructor-arg> <property name="name" value="liming"></property> <property name="age" value="19"></property> </bean> |
Property定義了屬性,name爲屬性名,value爲屬性的值,在例子中這兩個屬性都是字符串。在實際應用中,屬性值可以是int,也可以是List,基本覆蓋常用的數據結構。
E、 使用
配置完成後,就可以將xml文件加載到上下文中進行使用。加載方式如下,通過ClassPathXmlApplicationContext加載classpath路徑下的xml文件,得到context上下文對象,然後根據id獲取到bean。
ApplicationContext application = new ClassPathXmlApplicationContext("spring-config.xml"); Person person = (Person) application.getBean("person"); |
3、 Java配置
再來看看基於java的配置,這種配置方式更加簡單,不再需要XML文件,看看下面的例子,我們新創建一個類JavaConfigPerson,類中有兩個方法person和regiona。和普通類有區別的地方在於多了@Configuration和@Bean兩個註解,@Configuration就相當於xml中步驟A裏面定義的配置規範,是不是一下少了好多行。@Bean就相當於xml中聲明Bean。類中的方法名regiona、person就相當於xml配置中的id,只是它們也可以當作普通方法來使用。
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class JavaConfigPerson { @Bean public Person person (){ Person person = new Person(regiona (),"a funny man"); person.setName("liming"); person.setAge("19"); return person; } @Bean public Region regiona (){ Region region = new Region(); region.setProvince("beijing"); region.setCity("haidian"); return region; } } |
配置完成後,怎麼使用呢,利用AnnotationConfigApplicationContext類來實例化context上下文對象,實例化完成後操作跟xml配置完全相同,例子如下:
ApplicationContext ctx = new AnnotationConfigApplicationContext(JavaConfigPerson.class); Person person = (Person) ctx.getBean("getPerson"); |
4、 混合配置
基於XML配置一般是一些老的系統,爲了將新添加的基於java配置的系統與基於xml配置的系統進行集成,需要添加再配置文件中添加context:component-scan標籤,掃描指定路徑下基於java 的配置,添加到context上下文中。
<context:component-scan base-package="iscas.springstudy.aop" /> |
在上面的配置中,系統會自動掃描包iscas.springstudy.aop下面包含@Configuration註解的類。
至此完成了基於xml和java的混合配置,加載使按照基於xml配置生成context上下文即可。