歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring之Core模塊

Spring之Core模塊

日期:2017/3/1 9:31:14   编辑:Linux編程

Core模塊主要的功能是實現了控制反轉與依賴注入、Bean配置以及加載。Core模塊中有Beans、BeanFactory、BeanDefinitions、ApplicationContext等概念

BeanFactory

BeanFactory是實例化、配置、管理眾多bean的容器

在Web程序中用戶不需要實例化Beanfactory,Web程序加載的時候會自動實例化BeanFactory,並加載所欲的Beans,將各個Bean設置到Servlet、Struts的Action中或者Hibernate資源中

在Java桌面程序中,需要從BeanFactory中獲取Bean,因此需要實例化BeanFactory,例如,加載ClassPath下的配置文件:

ClassPathResource res = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory (res);
Iservice service= factory.getBean("service");
……
factory.destroySingletons();

或者使用文件流加載任意位置的配置文件

InputStream in = new FileInputStream("C:\\ApplicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory (in);

或者用ClassPathXmlApplicationContext加載多個配置文件(以字符串形式傳入)

ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
new String [] {"applicationContext.xml","applicationContext-part2.xml"}
);

BeanFactory factory = (BeanFactory) appContext;
//ApplicationContext繼承自BeanFactory接口

--------------------------------------分割線 --------------------------------------

Spring中如何配置Hibernate事務 http://www.linuxidc.com/Linux/2013-12/93681.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

基於 Spring 設計並實現 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm

Spring-3.2.4 + Quartz-2.2.0集成實例 http://www.linuxidc.com/Linux/2013-10/91524.htm

使用 Spring 進行單元測試 http://www.linuxidc.com/Linux/2013-09/89913.htm

運用Spring注解實現Netty服務器端UDP應用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm

Spring 3.x 企業應用開發實戰 PDF完整高清掃描版+源代碼 http://www.linuxidc.com/Linux/2013-10/91357.htm

--------------------------------------分割線 --------------------------------------

配置Bean

工廠模式

如果一個bean不能通過new直接實例化,而是通過工廠類的某個方法創建的,需要把<bean>的class屬性配置為工廠類(或者吧factory-bean屬性配置為工廠類對象)

<bean id="examBean" class = "examples.MyBeanFactory" method="createInstance" />
<!--等價於下面的配置-->
<bean id="examBean2" factory-bean = "examples.MyBeanFactory" method="createInstance" />

構造函數

如果Bean的構造函數帶有參數,需要指定構造函數的參數

<bean id = "examBean" class=" examples.ExampleBean">
<constructor-args><ref bean="anotherBeanId"/></constructor-args>
<constructor-args><ref bean="anotherBeanId2"/></constructor-args>
<constructor-args><value>1</value></constructor-args>
</bean>

參數又先後順序,要與構造函數參數的順序相同

單態模式

Bean可以定義是否為單態模式,在非單態模式下,每次請求該Bean都會生成一個新的對象

像數據源等一般配置為單態模式

<bean id="exampleBean" class="examples.ExamleBean" singleton="false"/>

property屬性

destroy-method屬性配置關閉方法,如果有配置,在丟棄Java對象時會調用該方法,某些數據源、SessionFactory對象都需要用destroy-method配置關閉方法

<property name="examProperty" value="pValue" />

等價於

<property name="examProperty">
<value>pValue</value>
</property>

注意:

<property name="password">
<value></value>
</property>

會將password設置為"",而不是null,如果想設置為null應該為

<property name="password">
<null/>
</property>

<ref>屬性

Spring配置文件的Bean之間可以相互引用,引用時用<ref>標簽配合Bean的id屬性使用

也可以使用內部配置

<bean id="dao" class = "com.clf.DaoImpl"></bean>
<bean id="serviceImpl" class="com.clf. serviceImpl">
<property name="dao">
<ref bean="dao"/>
</property>
</bean>

等價於內部配置

<property name="dao">
<bean class="com.clf.DaoImpl"/>
</property>

除了使用<ref>的bean屬性,還可以使用local、parent,它們與bean屬性的作用是一樣的,但是,local只能使用本配置文件中的bean,parent只能使用父配置文件中的bean

<list>屬性

<property name="propName">
<list>
<value>String、Integer、Double等類型數據</value>
<ref bean="dataSource"/>
</list>
</property>

<set>屬性

<property name="propName">
<set>
<value>String、Integer、Double等類型數據</value>
<ref bean="dataSource"/>
</set>
</property>

<map>屬性

<property name="propName">
<map>
<entry key="key1">
<value>String、Integer、Double等類型數據</value>
</entry>
<entry key-ref="key2">
<ref bean="dataSource"/>
</entry>
</map>
</property>

<props>屬性

<property name="propName">
<props>
<prop key="url">http://localhost:8080/clf</prop>
<prop key="name">clf</prop>
</ props >
</property>

<destroy-method>和<init-method>屬性

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
……
</bean>

Spring在注銷這些資源時會調用close方法

有些對象在實例化之後需要執行某些初始化代碼,但是這些代碼不能寫進構造函數,這時候可以把初始化代碼寫進某個方法中,並用<init-method>指定該方法

<bean id="c" class="com.clf.SpringExample" init-method="init">

depends-on屬性

Spring會默認按照配置文件裡的Bean順序地實例化Bean,但是有時候實例化A對象之前需要先實例化後面的B對象,這時候可以使用depends-on屬性強制先實例化B對象

<bean id="a" clas="com.clf.A" depends-on="b"></bean>
<bean id="b" clas="com.clf.B"></bean>

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2015-03/114675p2.htm

Copyright © Linux教程網 All Rights Reserved