天天看點

Spring 2.0技術手冊 -1

要引用已經設定的bean執行個體,可以使用​

​<ref>​

​ 标簽

依賴注入(Dependency Injection,簡稱DI)

使用Spring IDE 可視化bean檔案的節點摘要

Bean graph

Bean 消息,事件章

Bean基本管理

Application是基于BeanFactory而建立的。

最常使用的3個,我使用過其中一個

ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationContext.xml”);

ApplicationContext可以讀取多個 Bean定義檔案

ApplicationContext ac = new ClassPathXmlApplicationContext(new String [] {“applicationContext.xml”,”applicationContext2.xml”});

也可以使用檔案的通配符 *

import 也可以導入bean定義檔案, 必須在 标簽之前

Bean的識别名稱 和 别名 ,也就是一個執行個體可以起多個名字

下面是兩種别名的方式, alias 和 name屬性

id="helloworld" class="com.kpk.learnSpring.hellobean" name="hell003,hell004">
        <property name="helloWorld"><value>fupeng2016</value></property>    
    </bean>
    <alias name="helloworld" alias="hell001"/>
    <alias name="helloworld" alias="hell002"/>      

Bean的執行個體化

1,預設構造函數,沒有參數

id="helloworld" class="com.kpk.learnSpring.hellobean"      

factory-bean=””

factory-method=”“

Bean的 scope

取得執行個體預設是 singleton,一個bean名稱隻維持一個執行個體。

使用了scope參數,将bean設定為 每一個取bean都産生一個新的執行個體

<bean id="floppy" class="com.kpk.learnSpring.FloppyWriter"  scope="prototype">      

使用BeanFactory 在 getBean() 方法調用時,才會執行個體化對象

而ApplicatoinContext會将所有bean執行個體化。除非有lazy-init

bean如果定義的太多,也可以使用繼承

<bean id="BaseCustomerMalaysia" class="com.mkyong.common.Customer" abstract="true">
        <property name="country" value="Malaysia" />
</bean>

    <bean id="CustomerBean" parent="BaseCustomerMalaysia">
       <property name="action" value="buy" />
       <property name="type" value="1"      

Bean的依賴設定

Type3 IoC

有參數的構造函數

<bean id="testTpl" class="org.springframework.data.solr.core.SolrTemplate">
        <constructor-arg index="0" ref="solrServer1"
        <constructor-arg index="1" value="simu"
     </bean>      

如果多個構造方法,參數相同時候,可以在配置檔案中指定去按照哪個函數去構造。

可以使用value标簽,或者value屬性

如果将某個屬性設定為null ,可以使用null标簽。

使用ref标簽,可以直接隻用某個已經定義的Bean執行個體,這時必須在同一個設定檔案中。 且可以指定local屬性

class屬性

depend-on屬性,可以在執行個體化本bean之前,先去執行個體化,depend-on裡指定的屬性。

自動綁定

autowire=”byType”

autowire=”byName”

autowire=”constructor”

autowire=”autodetect”