1.Core模塊的功能實現的反向控制與依賴注入、Bean配置與加載。Core模塊中有Beans、BeanFactory、BeanDefinitions、ApplicationContext等幾個重要概念。
2.Beans爲Spring裏的各種對象,一般都要配置在Spring的配置文件中;BeanFactory是爲創建Bean的Factory;Spring通過BeanFactory加載各種Bean;BeanDifinition爲Bean在配置文件中的定義,一般要定義id和class;ApplicationContext代表配置文件。
BeanFactory工廠
a)首先來說下BeanFactory:
BeanFactory是實例化、配置、管理衆多Bean的容器。BeanFactory根據配置實例化Bean對象,並設置相互的依賴性。
BeanFactory可用接口org.springframework.factory.BeanFactory表示。BeanFactory有多種實現,最常用的爲XmlBeanFactory,XmlBeanFactory能夠加載XML格式的配置文件。
在Web應用程序中,我們不需要實例化BeanFactory,Web程序加載時會自動實例化BeanFactory,並加載所有的Bean;但是在Java桌面程序中,需要從BeanFactory中獲取Bean,所以要實例化BeanFactory。
//獲取配置資源並取得對象工廠
XmlBeanFactory factory = new XmlBeanFactory( new ClassPathResource("applicationContext.xml"));
//獲取Bean
IService hello =(IService) factory.getBean("proxy");
hello.service("HelloBean");
factory.destroySingletons();
參數applicationContext.xml默認在classpath下面,如果需要加載多個配置文件的話,可以用ClassPathXmlApplicationContext加載多個配置文件
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String []{"...","..."});
BeanFactory factory1 = appContext;
b)XmlBeanFactory配置格式:在xmlBeanFactory中,配置文件的根節點爲<beans>,裏面定義了多個一個或者多個<bean>子節點,每個<bean>代表一個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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 配置攔截器對象 -->
<bean id="methodBeforeAdviceImpl" class="com.spring.impl.MethodBeforeAdviceImpl"></bean>
<bean id="theAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice">
<ref local="methodBeforeAdviceImpl"/>
</property>
<property name="mappedName" value="*"></property><!-- 攔截所有的方法 -->
</bean>
<bean id="daoImpl" class="com.spring.impl.DaoImpl"></bean>
<bean id="serviceImpl" class="com.spring.service.ServiceImpl">
<property name="idao" ref="daoImpl"></property>
</bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interceptorNames" value="theAdvisor"></property>
<property name="target">
<ref local="serviceImpl"/>
</property>
</bean>
</beans>
配置JavaBean
a)基本配置<bean>:通常需要定義id與class屬性,class屬性是必須的,如果有多個bean,id也是必須的
b)工廠模式factory-method:如果一個Bean不能直接被實例化,而是通過工廠類的某個方法創建的,需要吧<bean>的class屬性設置爲工廠類(或者吧factory-bean的屬性設置爲工廠類),factory-method屬性設置爲產生實例的方法:
c)構造函數<construct-tag>:如果Java Bean的構造函數帶有參數,需要指定構造函數的參數,<construct-tag>指定構造參數所用的值
d).單態模式singleton:Bean可以定義爲單態模式,Spring默認爲單例模式,如果想使用非單例模式(稱爲Protootype模式),需要把singleton設置爲false
<bean id="test1" class="路徑" singleton=“false” >
e).配置屬性<property>及設置對象屬性<ref>:
bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interceptorNames" value="theAdvisor"></property>
<property name="target">
<ref local="serviceImpl"/>
</property>
</bean>
注意:如果property的屬性沒有設置值,默認是“”,而不是Null,如果要設置爲null,可以使用<null/>
<password>
<value></value><!--默認是“”-->
</password>
<password>
<null/><!--設置爲null-->
</password>
f)配置list
<bean id="test1" class="">
<property name="someList">
<list>
<value>String /Integer/Double/Boolean等類型對象</value><!--普通類型可以直接用字符串 -->
<ref bean="xxx"/><!-- Java對象 可以用ref,也可以用bean重新定義-->
</list>
</property>
</bean>
g)配置set屬性(配置同上,只需將list節點換爲set)
h).配置map屬性:<entry>配置map裏的元素,key指定索引,alue指定值。如果value爲Java對象,則使用<ref>指定,也可以通過<bean>定義,如果key爲Java對象,使用key-ref屬性
<bean id="test" class="....">
<map>
<entry key="...">
<value>好孩子</value>
</entry>
<entry key-ref="....">
<ref bean="...."/>
</entry>
</map>
</bean>
i).配置Properties屬性<props>:<props>配置一個Properties對象,<prop>配置一條屬性,屬性key配置索引:
<property>
<props>
<prop key="url">http://....</prop>
<prop key="name">xxx</prop>
</props>
</property>
j)<idref>與ref的區別:<idref>與<ref>的作用是一樣,都是配置Java對象的。<idref>的用法與<ref>的用法基本相同,不同的是<idref>只有Bean的local屬性,沒有parent屬性,例如:
<idref local="...."></idref>
Spring加載xml配置文件時,會檢查<idref>配置的Bean存在不存在,而<ref>只會在第一次調用時纔會檢查。換句話說,如果Bean不存在,<idref>啓動程序的時候就拋出錯誤,而<ref>只會在運行中拋出錯誤。
k)設置destroy-method方法:飄過
l).設置depends-on依賴對象:Spring會默認按照配置文件裏的Bean的設置的先後順序實例化Bean,但是有時候在實例化A對象之前需要先實例化後面的B對象。這時候可以使用depends-on,強制先實例化B對象:
<bean id="a" class="...." depends-on="b"></bean>
<bean id="b" class="...."></bean>
m)。初始化方法init-method
n)還有屬性的自動裝配autoWire,依賴檢查dependency,以及Bean的一些高級屬性就不詳細列舉了