重讀 Spring 3 開發手冊 總結 一

1. ApplicationContext基本配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
           
    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>
    <import resource="/resources/themeSource.xml"/>
    <import resource="../resources/configFile.xml"/>
    <import resource="classpath:../configFile.xml"/>
    
  <bean id="..." class="...">
    <!-- collaborators and configuration for this bean go here -->
  </bean>

  <bean id="..." class="...">
    <!-- collaborators and configuration for this bean go here -->
  </bean>
  <!-- more bean definitions go here -->
</beans>


2. 獲取Container容器的實例

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
//or
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml");
//or
ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:conf/appContext.xml");


3. bean 實例化
1> 工廠靜態方法實例化:
配置:

<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
 <!-- inject any dependencies required by this locator bean -->
</bean>
<!-- the bean to be created via the factory bean -->
<bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/>

類:

public class DefaultServiceLocator {
 private static ClientService clientService = new ClientServiceImpl();
 private DefaultServiceLocator() {}

 public ClientService createClientServiceInstance() {
    return clientService;
 }
}


2> 構造函數實例化:

1) 依賴關係:

package x.y;
public class Foo {
 public Foo(Bar bar, Baz baz) {
 // ...
 }
}
<beans>
 <bean id="foo" class="x.y.Foo">
 <constructor-arg ref="bar"/>
 <constructor-arg ref="baz"/>
 </bean>
 <bean id="bar" class="x.y.Bar"/>
 <bean id="baz" class="x.y.Baz"/>
</beans>


2)構造函數賦值:
package examples;
public class ExampleBean {
 private int years;
 private String ultimateAnswer;
 public ExampleBean(int years, String ultimateAnswer) {
 this.years = years;
 this.ultimateAnswer = ultimateAnswer;
 }
}

按類型
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>

按序列
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="7500000"/>
<constructor-arg index="1" value="42"/>
</bean>

按名稱
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg name="years" value="7500000"/>
<constructor-arg name="ultimateanswer" value="42"/>
</bean>


4 屬性賦值:

<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"/>
		<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
		<property name="username" value="root"/>
		<property name="password" value="masterkaoli"/>
	</bean>
或:
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close"
      p:driverClassName="com.mysql.jdbc.Driver"
      p:url="jdbc:mysql://localhost:3306/mydb"
      p:username="root"
      p:password="masterkaoli"/>
或:
<bean id="mappings"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	 <!-- typed as a java.util.Properties -->
	 <property name="properties">
		<value>
		   jdbc.driver.className=com.mysql.jdbc.Driver
		   jdbc.url=jdbc:mysql://localhost:3306/mydb
		</value>
	 </property>
	</bean>



集合類型的屬性賦值:
<bean id="moreComplexObject" class="example.ComplexObject">
	<!-- results in a setAdminEmails(java.util.Properties) call -->
	<property name="adminEmails">
	  <props>
		  <prop key="administrator">[email protected]</prop>
		  <prop key="support">[email protected]</prop>
		  <prop key="development">[email protected]</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="an entry" value="just some string"/>
		  <entry key ="a ref" value-ref="myDataSource"/>
	  </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>



The value of a map key or value, or a set value, can also again be any of the following elements:
bean | ref | idref | list | set | map | props | value | null


集合類型的屬性賦值及合併繼承
<beans>
	<bean id="parent" abstract="true" class="example.ComplexObject">
	  <property name="adminEmails">
		  <props>
			  <prop key="administrator">[email protected]</prop>
			  <prop key="support">[email protected]</prop>
		  </props>
	  </property>
	</bean>
	<bean id="child" parent="parent">
	  <property name="adminEmails">
		  <!-- the merge is specified on the *child* collection definition -->
		  <props merge="true">
			  <prop key="sales">[email protected]</prop>
			  <prop key="support">[email protected]</prop>
		  </props>
	  </property>
	</bean>
	<beans>



child.getAdminEmails()結果爲
=========================================
[email protected]
[email protected]
[email protected]

=========================================


Null值和空值(Null and empty string values)
<bean class="ExampleBean">
     <property name="email" value=""/>
</bean>
等同於 exampleBean.setEmail("");

<bean class="ExampleBean">
  <property name="email"><null/></property>
</bean>
等同於exampleBean.setEmail(null).

屬性賦值的簡寫形式: p:namespace
<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 name="classic" class="com.example.ExampleBean">
 <property name="email" value="[email protected]"/>
 <property name="user" ref="aUser"/>
 </bean>

 <bean name="p-namespace" class="com.example.ExampleBean" p:email="[email protected]"/>
 <bean name="p-namespace-ref" class="com.example.ExampleBean" p:email="[email protected]" p:user-ref="aUser"/>
 
 <bean name="aUser" class="com.example.User">
 <property name="userName" value="tiger"/>
 <property name="age" value="24"/>
 </bean>
</beans>

構造函數的簡寫形式: c:namespace
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/c"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">
 
 <bean id="bar" class="x.y.Bar"/>
 <bean id="baz" class="x.y.Baz"/>


 <-- 'traditional' declaration -->      
 <bean id="foo" class="x.y.Foo">
 <constructor-arg ref="bar"/>
 <constructor-arg ref="baz"/>
 <constructor-arg value="[email protected]"/>
 </bean>
 <-- 'c-namespace' declaration -->
 <bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="[email protected]">
</beans>


5 依賴
有多個依賴時可用逗號,空格,分號分割

<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
      <property name="manager" ref="manager" />
</bean>
<!--在beanOne實例化或消亡之前會先實例化或消亡manager和accountDao-->
<bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />
有依賴關係的bean,不但在實例化時存在依賴關係,在對象消亡時也同樣具有依賴關係。即在實例化或消亡時會先實例化或消亡被依賴對象。


6.Lazy-initialized 延時實例化
A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.
延時實例化的bean,會在被訪問的時候才進行實例化,而不是在容器啓動是進行pre-instantiation

<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>

However, when a lazy-initialized bean is a dependency of a singleton bean that is not lazy-initialized, the ApplicationContext 

creates the lazy-initialized bean at startup, because it must satisfy the singleton's dependencies. 
The lazy-initialized bean is injected into a singleton bean elsewhere that is not lazy-initialized.
當一個singleton 的bean依賴於Lazy-initialized的bean時,Lazy-initialized將會被實例化。
Lazy-initialized同樣可以作用於容器級別:

<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</beans>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章