最基礎(chǔ)的包中有兩個是org.springframework.beans 和org.springframework.context .這兩個包提供了spring依賴注射功能的基礎(chǔ)。
BeanFactory提供了高級的配置機制用于管理beans。
ApplicationContext接口建立在BeanFactory接口之上(BeanFactory的子類),又添加了其它的功能,比如:更容易與Spring AOP功能結(jié)合,信息資源處理,事件傳播,建立ApplicationContext和父Contexts的機制,應(yīng)用層特殊的context(如WebApplicationContext)。
ApplicationContext是BeanFactory的擴(kuò)展,所有BeanFactory的功能都能在ApplicationContext中實現(xiàn)。在建立的項目處于j2ee環(huán)境的時候最好選擇ApplicationContext;當(dāng)你對內(nèi)存的占有很關(guān)心的時候(如一個applet會關(guān)心每KB大小)可以選擇BeanFactory(占內(nèi)存少)。
BeanFactory 是為了訪問Spring bean container,可以把它看成事實上的container.一般簡單的BeanFactory的實現(xiàn)是org.springframework.beans.factory.xml.XmlBeanFactory。
創(chuàng)建 BeanFatory的3個辦法如下
1。Resource res = new FileSystemResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
2。ClassPathResource res = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
3。ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
// of course, an ApplicationContext is just a BeanFactory
BeanFactory factory = (BeanFactory) appContext;
(注:public interface Resourceextends InputStreamSource,
public interface InputStreamSource)
多數(shù)用法中,用戶不需要編寫代碼初始化BeanFactory 或 ApplicationContext,因為Spring框架會自動完成。例如:在web層提供了代碼自動裝載
ApplicationContext,作為正常的j2eeWEB應(yīng)用程序開始的一部分。
配置bean的XML幾種格式如下:
(1)。通過構(gòu)造函數(shù)創(chuàng)建bean,bean可以是javabean 也可以不是,也不用實現(xiàn)別的接口。
<bean id="exampleBean" class="examples.ExampleBean"/>
<bean name="anotherExample" class="examples.ExampleBeanTwo"/>
如果你需要實現(xiàn)IOC機制,你的bean需要一個默認(rèn)的空參數(shù)構(gòu)造函數(shù)。
(2)。通過靜態(tài)工廠函數(shù)創(chuàng)建bean.
<bean id="exampleBean" class="examples.ExampleBean2"
factory-method="createInstance"/>
createInstance是examples.ExampleBean2的靜態(tài)工廠方法,用于生成exampleBean。
(3).通過實例工廠方法創(chuàng)建bean.
<!-- The factory bean, which contains a method called createInstance -->
<bean id="myFactoryBean" class="...">
...
</bean>
<!-- The bean to be created via the factory bean -->
<bean id="exampleBean" factory-bean="myFactoryBean" factory-method="createInstance"/>
To singleton or not to singleton
<bean id="exampleBean"
class="examples.ExampleBean" singleton="false"/>
<bean name="yetAnotherExample"
class="examples.ExampleBeanTwo" singleton="true"/>
幾種依賴注射格式
(1)setter注射
<bean id="exampleBean" class="examples.ExampleBean">
<property name="beanOne"><ref bean="anotherExampleBean"/></property>
<property name="beanTwo"><ref bean="yetAnotherBean"/></property>
<property name="integerProperty"><value>1</value></property>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i) {
this.i = i;
}
}
(2)構(gòu)造函數(shù)注射
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
<constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
<constructor-arg type="int"><value>1</value></constructor-arg>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public ExampleBean(AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
this.beanOne = anotherBean;
this.beanTwo = yetAnotherBean;
this.i = i;
}
}
(3)靜態(tài)工廠注射
<bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance">
<constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
<constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
<constructor-arg><value>1</value></constructor-arg>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {
...
// a private constructor
private ExampleBean(...) {
...
}
// a static factory method
// the arguments to this method can be considered the dependencies of the bean that
// is returned, regardless of how those arguments are actually used.
public static ExampleBean createInstance(AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
ExampleBean eb = new ExampleBean(...);
// some other operations
...
return eb;
}
}
構(gòu)造函數(shù)配置的另一種格式
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0"><value>7500000</value></constructor-arg>
<constructor-arg index="1"><value>42</value></constructor-arg>
</bean>
可以在配置文件中加入驗證數(shù)據(jù)的類型 如下
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" type="int"><value>7500000</value></constructor-arg>
<constructor-arg index="1" type="java.lang.String"><value>42</value></constructor-arg>
</bean>
一般屬性配置格式
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- results in a setDriverClassName(String) call -->
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/mydb</value>
</property>
<property name="username">
<value>root</value>
</property>
</bean>
注意:<value></value> 默認(rèn)為空字符串,
屬性null的格式如下
<bean class="ExampleBean">
<property name="email"><null/></property>
</bean>
屬性collection的格式如下
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setPeople(java.util.Properties) call -->
<property name="people">
<props>
<prop key="HarryPotter">The magic property</prop>
<prop key="JerrySeinfeld">The funny property</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource"/>
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry>
<key><value>yup an entry</value></key>
<value>just some string</value>
</entry>
<entry>
<key><value>yup a ref</value></key>
<ref bean="myDataSource"/>
</entry>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource"/>
</set>
</property>
</bean>
3.3.3.4. Inner bean definitions via nested bean elements
<bean id="outer" class="...">
<!-- Instead of using a reference to target, just use an inner bean -->
<property name="target">
<bean class="com.mycompany.PersonImpl">//嵌套的bean不需要id
<property name="name"><value>Tony</value></property>
<property name="age"><value>51</value></property>
</bean>
</property>
</bean>
復(fù)合屬性定義
<bean id="foo" class="foo.Bar">
<property name="fred.bob.sammy" value="123"/> 屬性fred下有屬性bob,bob屬性下有sammy 把sammy置為123。
</bean>
3.3.4. Method Injection(方法注射)
<!-- a stateful bean deployed as a prototype (non-singleton) -->
<bean id="singleShotHelper class="..." singleton="false">
</bean>
<!-- myBean uses singleShotHelper -->
<bean id="myBean" class="...">
<lookup-method name="createSingleShotHelper" bean="singleShotHelper"/>
<property>
...
</property>
</bean>
我的理解:myBean 中必須實現(xiàn) protected [abstract] SingleShotHelper createSingleShotHelper();
singleShotHelper中必須實現(xiàn) protected SingleShotHelper createSingleShotHelper();
運行時 singleShotHelper的createSingleShotHelper會覆蓋myBean的createSingleShotHelper。//未驗證
3.3.4.2. Arbitrary method replacement(任意的方法替換)
public class MyValueCalculator {
public String computeValue(String input) {
... some real code
}
... some other methods
}
進(jìn)行替換的類必須實現(xiàn)接口org.springframework.beans.factory.support.MethodReplacer
public class ReplacementComputeValue implements MethodReplacer {
public Object reimplement(Object o, Method m, Object[] args) throws Throwable {
// get the input value, work with it, and return a computed result
String input = (String) args[0];
...
return ...;
}
}
<bean id="myValueCalculator class="x.y.z.MyValueCalculator">
<!-- arbitrary method replacement -->
<replaced-method name="computeValue" replacer="replacementComputeValue">
<arg-type>String</arg-type> 這里表示方法接受的參數(shù),有幾個列幾個。
</replaced-method> 這樣myValueCalculator中的computeValue方法就被替換了
</bean>
<bean id="replacementComputeValue" class="a.b.c.ReplaceMentComputeValue"/>
3.4.1生命周期
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
public class ExampleBean {
public void init() {
// do some initialization work
}
}
Is exactly the same as:
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements InitializingBean {
public void afterPropertiesSet() {
// do some initialization work
}
}
<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
public class ExampleBean {
public void cleanup() {
// do some destruction work (like closing connection)
}
}
Is exactly the same as:
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements DisposableBean {
public void destroy() {
// do some destruction work
}
}
===============================================
3.7. 用BeanPostProcessors自定義beans
ConfigurableBeanFactory bf = new .....; // create BeanFactory
... // now register some beans
// now register any needed BeanPostProcessors
MyBeanPostProcessor pp = new MyBeanPostProcessor();
bf.addBeanPostProcessor(pp);
// now start using the factory
3.8. 用BeanFactoryPostProcessors 自定義bean factories
XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
// create placeholderconfigurer to bring in some property
// values from a Properties file
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("jdbc.properties"));
// now actually do the replacement
cfg.postProcessBeanFactory(factory);
(也可以聲明式定義
<!-- property placeholder post-processor -->
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="jdbc.properties"/>
</bean>
)
由上
以下這個處于beanfactory容器里的數(shù)據(jù)源就按jdbc.properties里的配置了
<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>
其中jdbc.properties如下
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
3.8.2. The PropertyOverrideConfigurer
3.19. Creating an ApplicationContext from a web application
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>(the listener cannot be used in Servlet 2.2 compatible
containers 但是在Servlet2.4后都普遍采用它)
<!-- OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
-->