javascript
【Spring学习】ring的core模块应用详解
Core模塊主要的功能是實(shí)現(xiàn)了控制反轉(zhuǎn)與依賴注入、Bean配置以及加載。Core模塊中有Beans、BeanFactory、BeanDefinitions、ApplicationContext等概念。Beans為spring里的各種對象,一般要配置在spring的配置文件中;BeanFactory為創(chuàng)建Beans的Factory,spring通過BeanFactory加載各種Beans;BeanDefinition為Bean在配置文件中的定義,一般要定義id和class;ApplicationContext代表配置文件。
BeanFactory
BeanFactory是實(shí)例化、配置、管理眾多bean的容器。BeanFactory可以用接口org,springframework.beans.factory.BeanFactory表示。BeanFactory有多種實(shí)現(xiàn),其中最為常用的是org,springframework.beans.factory.xml.XmlBeanFactory,能加載XML格式的配置文件。在Web程序中用戶不需要實(shí)例化Beanfactory,Web程序加載的時(shí)候會自動實(shí)例化BeanFactory,并加載所有的Beans,將各個(gè)Bean設(shè)置到Servlet、Struts的Action中或者h(yuǎn)ibernate資源中。在Java程序中,需要從BeanFactory中獲取Bean,因此需要實(shí)例化BeanFactory,例如,加載ClassPath下的配置文件:
ClassPathResource cpr= new ClassPathResource("applicationContext.xml"); XmlBeanFactory factory = new XmlBeanFactory (cpr); Iservice service= factory.getBean("service"); …… factory.destroySingletons();或者使用文件流加載任意位置的配置文件:
InputStream in = new FileInputStream("D:\\ApplicationContext.xml"); XmlBeanFactory factory = new XmlBeanFactory (in);或者用ClassPathXmlApplicationContext加載多個(gè)配置文件(以字符串形式傳入):
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( new String [] {"applicationContext1.xml","applicationContext2.xml"} ); BeanFactory factory = (BeanFactory) appContext; //ApplicationContext繼承自BeanFactory接口配置Bean
工廠模式
如果一個(gè)bean不能通過new直接實(shí)例化,而是通過工廠類的某個(gè)方法創(chuàng)建的,需要把<bean>的class屬性配置為工廠類(或者吧factory-bean屬性配置為工廠類對象)
<bean id="example1" class = "examples.MyBeanFactory" method="createInstance" /> <!--等價(jià)于下面的配置--> <bean id="example12" factory-bean = "examples.MyBeanFactory" method="createInstance" />構(gòu)造函數(shù)
如果Bean的構(gòu)造函數(shù)帶有參數(shù),需要指定構(gòu)造函數(shù)的參數(shù)
<bean id = "example" class=" examples.ExampleBean"> <constructor-args><ref bean="BeanId1"/></constructor-args> <constructor-args><ref bean="BeanId2"/></constructor-args> <constructor-args><value>1</value></constructor-args> </bean>參數(shù)的先后順序,要與構(gòu)造函數(shù)參數(shù)的順序相同。
單態(tài)模式singleton
Bean可以定義是否為單態(tài)模式,在非單態(tài)模式下,每次請求該Bean都會生成一個(gè)新的對象,像數(shù)據(jù)源等一般配置為單態(tài)模式。spring默認(rèn)為單態(tài)模式,如果要使用非單態(tài)模式(prototype原型模式),需要把singleton屬性設(shè)置為false:
<bean id="exampleBean" class="examples.ExamleBean" singleton="false"/>property屬性
<property name="examProperty" value="pValue" />等價(jià)于
<property name="examProperty"> <value>pValue</value> </property>注意:
<property name="password"> <value></value> </property>會將password設(shè)置為”“,而不是null,如果想設(shè)置為null應(yīng)該為
<property name="password"> <null/> </property>設(shè)置對象屬性
spring配置文件的Bean之間可以相互引用,引用時(shí)用<ref>標(biāo)簽配合Bean的id屬性使用,也可以使用內(nèi)部配置:
<bean id="dao" class = "com.lmb.DaoImpl"></bean> <bean id="serviceImpl" class="com.lmb.serviceImpl"> <property name="dao"> <ref bean="dao"/> </property> </bean> //ref的bean屬性為目標(biāo)<bean>的id屬性等價(jià)于內(nèi)部配置
<property name="dao"> <bean class="com.lmb.DaoImpl"/> </property>除了使用<ref>的bean屬性,還可以使用local、parent,它們與bean屬性的作用是一樣的,但是,local只能使用本配置文件中的bean,parent只能使用父配置文件中的bean。
list屬性
<property name="propName"> <list> <value>string、integer、double等類型數(shù)據(jù)</value> <ref bean="dataSource"/> </list> </property>set屬性
<property name="propName"> <set> <value>string、integer、double等類型數(shù)據(jù)</value> <ref bean="dataSource"/> </set> </property>map屬性
<property name="propName"> <map> <entry key="key1"> <value>string、integer、double等類型數(shù)據(jù)</value> </entry> <entry key-ref="key2"> <ref bean="dataSource"/> </entry> </map> </property>props屬性
<property name="propName"> <props> <prop key="url">http://localhost:8080/lmb</prop> <prop key="name">lmb</prop> </ props > </property>destroy-method和init-method屬性
destroy-method屬性配置關(guān)閉方法,如果有配置,在丟棄Java對象時(shí)會調(diào)用該方法,某些數(shù)據(jù)源、SessionFactory對象都需要用destroy-method配置關(guān)閉方法。Spring在注銷這些資源時(shí)會調(diào)用下面的close方法:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> …… </bean>有些對象在實(shí)例化之后需要執(zhí)行某些初始化代碼,但是這些代碼不能寫進(jìn)構(gòu)造函數(shù),這時(shí)候可以把初始化代碼寫進(jìn)某個(gè)方法中,并用<init-method>指定該方法
<bean id="c" class="com.clf.SpringExample" init-method="init">depends-on屬性
Spring會默認(rèn)按照配置文件里的Bean順序地實(shí)例化Bean,但是有時(shí)候?qū)嵗疉對象之前需要先實(shí)例化后面的B對象,這時(shí)候可以使用depends-on屬性強(qiáng)制先實(shí)例化B對象:
<bean id="a" clas="com.lmb.A" depends-on="b"></bean> <bean id="b" clas="com.lmb.B"></bean>idref與ref的區(qū)別
<idref>與<ref>的作用是一樣的,都是配置Java對象的,不同的是,<idref>只有bean與local屬性,沒有parent屬性
Spring加載XML配置文件時(shí),會檢查<idref>配置的Bean在不在,而<ref>只會在第一次調(diào)用時(shí)才會檢查,換句話說,如果Bean不存在,<idref>在啟動程序時(shí)就會拋出錯(cuò)誤,而<ref>只會在運(yùn)行中拋出錯(cuò)誤。
autowire
可以通過Bean的autowire屬性設(shè)置自動裝配規(guī)則。使用autowire后不需要再用<property name="" value="" />顯式地設(shè)置該Bean的屬性、依賴關(guān)系,Spring會根據(jù)反射,自動尋找符合條件的屬性,設(shè)置到該Bean上,autowire屬性定義的不是需要自動裝配的屬性名,而是自動裝配的規(guī)則,一旦配置,所有的屬性都會遵循autowire定義的規(guī)則:
No:即不啟用自動裝配。Autowire默認(rèn)的值。
byName:通過屬性的名字的方式查找JavaBean依賴的對象并為其注入。比如說類Computer有個(gè)屬性printer,指定其autowire屬性為byName后,Spring IoC容器會在配置文件中查找id/name屬性為printer的bean,然后使用Setter方法為其注入。
byType:通過屬性的類型查找JavaBean依賴的對象并為其注入。比如類Computer有個(gè)屬性printer,類型為Printer,那么,指定其autowire屬性為byType后,Spring IoC容器會查找Class屬性為Printer的bean,使用Setter方法為其注入。
constructor:通byType一樣,也是通過類型查找依賴對象。與byType的區(qū)別在于它不是使用Setter方法注入,而是使用構(gòu)造子注入。
autodetect:在byType和constructor之間自動的選擇注入方式。
default:由上級標(biāo)簽<beans>的default-autowire屬性確定。
dependency-check
有時(shí)候某些Bean的屬性配置有錯(cuò)誤,這種錯(cuò)誤在程序啟動的時(shí)候不會有任何異常,會一直潛伏到Spring調(diào)用該Bean時(shí)才會被發(fā)現(xiàn)。依賴檢查能夠檢查屬性是否被設(shè)置,如果配置了依賴檢查,程序啟動是會進(jìn)行配置校驗(yàn),以便及時(shí)地發(fā)現(xiàn)錯(cuò)誤。但是需要注意的是,依賴檢查是很生硬的,例如設(shè)置為object,將會檢查所有的Java對象屬性,只要有一個(gè)屬性沒有設(shè)置,就會拋出異常。屬性列表如下:
no或default:不做任何檢查,默認(rèn)
simple:僅檢查基本類型、集合屬性
object:僅檢查Java對象屬性
all:檢查所有屬性
Bean的高級特性
spring程序中,Java bean一般與spring是非耦合的,不會依賴于spring類庫。這也是spring的優(yōu)點(diǎn)。但有時(shí)候Java bean需要知道自己在spring框架中的一些屬性。spring提供了一些接口,實(shí)例化Java bean對象后spring會調(diào)用接口的方法。
BeanNameAware接口
BeanNameAware接口幫助Bean知道自己在配置文件中的id。
import org.springframework.beans.factory.BeanNameAware; public class BeanNameTest implements BeanNameAware{ private String beanName; //Spring會調(diào)用該方法 public void setBeanName(String beanName){ this.beanName = beanName; } }BeanFactoryAware接口
BeanFactoryAware接口幫助Bean知道哪個(gè)BeanFactory實(shí)例化了自己,BeanFactoryAware接口中有一個(gè)回調(diào)方法setBeanFactory,初始化該對象后,會回調(diào)該方法,將BeanFactory傳進(jìn)來。用法同BeanNameAware。
public interface BeanFactoryAware{ void setBeanFactory(BeanFactory beanFactory) throws BeanException; }如果Java bean實(shí)現(xiàn)了BeanFacoryAware接口,就能夠獲得BeanFactory對象。BeanFactory還有下面幾個(gè)常用方法:
boolean containsBean(String)判定指定名稱的Bean是否存在
Object getBean(String)返回指定名稱,如果沒有該Bean會拋出異常
Object getBean(String,Class)返回指定名稱的Bean,并轉(zhuǎn)化為指定的類對象
boolean isSingleton(String)判斷指定名稱的Bean是否被配置為單態(tài)模式
String[] getAliases(String)返回指定名稱的Bean的別名
InitializingBean接口
InitializingBean接口執(zhí)行初始化方法。InitializingBean接口會在Bean實(shí)例化后、所有屬性被設(shè)置后調(diào)用初始化方法。但是使用該接口會與Spring代碼發(fā)生耦合,因此不推薦使用,Spring推薦使用init-method配置。有關(guān)spring的InitializingBean的afterPropertiesSet方法和init-method區(qū)別和應(yīng)用參看本人博客。
public interface InitializingBean{ public void afterPropertiesSet(); //初始化時(shí)調(diào)用此方法 }DisposableBean接口
DisposableBean接口會在Bean對象丟棄時(shí)調(diào)用銷毀方法
public interface DisposableBean{ public void destroy(); //銷毀時(shí)調(diào)用此方法 }屬性覆蓋器
對于一些參數(shù),更實(shí)用更簡單的方法是使用properties配置,而不是配置在Spring的配置文件中。
PropertyPlaceholderConfigurer屬性覆蓋器
PropertyPlaceholderConfigurer允許把XML配置的某些參數(shù)配置到properties文件中。這在數(shù)據(jù)庫配置中很常用,配置時(shí)需要配置一個(gè)PropertyPlaceholderConfigurer對象,指定properties文件的位置,然后把替換的屬性用形如${jdbc.url}的字符串替代:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc. username}"/> <property name="password" value="${jdbc. password}"/> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"> </bean> jdbc.propertiesjdbc.driverClassName= com.mysql.jdbc.Driver jdbc.url =jdbc:mysql://localhost:3306/lmb?characterEncoding=UTF-8 jdbc.username =admin jdbc.password =admin總結(jié):core模塊是spring最核心、最基本的模塊,實(shí)現(xiàn)加載配置文件,管理,初始化bean等功能。spring配置文件中可以配置各種各樣構(gòu)造函數(shù)的類,不過最常見還是使用不帶參數(shù)的構(gòu)造函數(shù)。
總結(jié)
以上是生活随笔為你收集整理的【Spring学习】ring的core模块应用详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java集合学习系列】HashMap实
- 下一篇: 【SpringBoot】在普通类中获取s