1. 程式人生 > >在IOC中裝配Bean

在IOC中裝配Bean

ioc bean

一、基於XML的配置

采用Schema格式

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"//默認命名空間,用於bean的定義
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"//xsi標準命名空間,用於為每個文檔指定相對應的Schema樣式文件
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:util="http://www.springframework.org/schema/util"
   xmlns:aop="http://www.springframework.org/schema/aop" //自定義的一種命名空間 
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
   
   <bean id="car" class="com.smart.fb.Car"
         p:brand="紅旗CA72" p:maxSpeed="200" p:price="20000.00"/>
   ......
   </beans>

Schema在文檔根節點中通過xmlns對文檔所引用的命名空間進行聲明。文檔後面的元素可通過命名空間別名加以區分。<aop:config/>


二、Bean基本配置

<bean id="car" class="com.smart.fb.Car">

id為這個Bean的名稱,通過容器的getBean("car")即可獲取對應的Bean,class定義了bean的實現類。

id的命名必須以字母開始,不能以逗號和空格結尾。但是name屬性沒有限制。

spring不允許有兩個id相同的bean,但是可以name相同。返回是返回最後一個。


三、依賴註入

  1. 屬性註入:要求bean提供一個默認的構造函數,並有相應的Setter方法。spring是先調用Bean的默認構造函數實例化Bean對象,然後通過反射調用set方法註入屬性值。但是spring只會檢查是否有對於的set方法,而有沒有該屬性不關註。

    在xml中

<bean id="boss" class="com.smart.attr.Boss">
   <property name="car" ref="car" />
   <property name="name" value="Tom" />
   <property name="age" value="45" />
</bean>

命名規範:xxx屬性對於setXxx()方法。變量前兩個字母要麽都大寫要麽都小寫。


2.構造函數註入

<!--構造函數註入:type -->
<bean id="car1" class="com.smart.ditype.Car">
   <constructor-arg type="java.lang.String">
      <value>紅旗CA72</value>
   </constructor-arg>
   <constructor-arg type="double">
      <value>20000</value>
   </constructor-arg>
</bean>

<!--
   構造函數註入:index <bean id="car2" class="com.smart.ditype.Car">
   <constructor-arg index="0" value="紅旗CA72" /> <constructor-arg
   index="1" value="中國一汽" /> <constructor-arg index="2" value="20000" />
   </bean>
-->

<!--構造函數註入:type&index -->
<bean id="car3" class="com.smart.ditype.Car">
   <constructor-arg index="0" type="java.lang.String">
      <value>紅旗CA72</value>
   </constructor-arg>
   <constructor-arg index="1" type="java.lang.String">
      <value>中國一汽</value>
   </constructor-arg>
   <constructor-arg index="2" type="int">
      <value>200</value>
   </constructor-arg>
</bean>
<bean id="car4" class="com.smart.ditype.Car">
   <constructor-arg index="0">
      <value>紅旗CA72</value>
   </constructor-arg>
   <constructor-arg index="1">
      <value>中國一汽</value>
   </constructor-arg>
   <constructor-arg index="2" type="int">
      <value>200</value>
   </constructor-arg>
</bean>

<!--構造函數註入:自動識別入參類型 -->
<bean id="boss1" class="com.smart.ditype.Boss">
   <constructor-arg>
      <value>John</value>
   </constructor-arg>
   <constructor-arg>
      <ref bean="car" />
   </constructor-arg>
   <constructor-arg>
      <ref bean="office" />
   </constructor-arg>
</bean>
<bean id="office" class="com.smart.ditype.Office" />


四、註入參數

  1. 字面值:基本數據類型及其封裝類、String類都可以采用字面值註入。通過value=""或者<value>方式來註入。<![CDATA[]]>作用是讓XML解析器把[]內的字符串當成普通文本對待。一般情況下XML會忽略標簽內部字符串的前後空格,但是在spring中不會。


  2. 引用其他bean:通過ref元素。<property name="car" ref="car"></property>或者<ref bean="car"/> bean是同一容器或者父容器中的bean,local只能引用同一配置文件中的bean,parent引用父容器的bean


  3. 內部bean:類似內部類,沒有名字不能被外部引用。



  4. null值:<value></value>這樣會被解析成空字符串。<value><null/></value>這樣才是空。


  5. 集合類型屬性

<list>
   <value>看報</value>
   <value>賽車</value>
   <value>高爾夫</value>
   </list>
   
<set>
   <value>看報</value>
   <value>賽車</value>
   <value>高爾夫</value>
</set>

<map>
   <entry >
      <key>
         <value>AM</value>
      </key>
      <value>會見客戶</value>
   </entry>
   <entry>
      <key>
         <value>PM</value>
      </key>
      <value>公司內部會議</value>
   </entry>
</map>

<props>
   <prop key="jobMail">[email protected]</prop>
   <prop key="lifeMail">[email protected]</prop>
</props>

<bean id="parentBoss" abstract="true"
   class="com.smart.attr.Boss">
   <property name="favorites">
      <set>
         <value>看報</value>
         <value>賽車</value>
         <value>高爾夫</value>
      </set>
   </property>
</bean>
<bean id="childBoss" parent="parentBoss">
   <property name="favorites">
      <set merge="true">
         <value>爬山</value>
         <value>遊泳</value>
      </set>
   </property>
</bean>

子bean的favorites最後有5個元素。merge="true"是和父bean的同名進行集合屬性合並。


五、bean之間的關系

1.繼承:子bean繼承父bean。重復的會覆蓋。

1)父bean:abstract="true"

2)子bean:parent="parbean"

   <!-- 父子<bean> -->
<bean id="abstractCar" class="com.smart.tagdepend.Car"
      p:brand="紅旗CA72" p:price="2000.00" p:color="黑色"
      abstract="true"/>
      
<bean id="car3" parent="abstractCar">
   <property name="color" value="紅色"/>
</bean>
<bean id="car4" parent="abstractCar" >
   <property name="color" value="白色"/>
</bean>

2.引用


六、整合多個配置文件

通過<import>將多個配置文件引到一個文件中,進行配置文件集成。

<import resource="classpath:com/.../bean.xml"/>


七、Bean的作用域

  1. siglenton作用域:默認情況下,spring的ApplicationContext容器在啟動時,會自動實例化所有的sigleton的bean並緩存在容器中。如果不希望提前實例化,可以通過lazy-init="true"來等到使用是才實例化。

  2. prototype作用域:scope="prototype"指定非單例作用域的Bean。

  3. web環境下:在低版本的web,可以使用http請求過濾器進行配置,高版本可以使用http請求監聽器來進行配置。

    1)reqest作用域:每次http請求就會調用。

    2)session:橫跨整個Session,Session中所有http請求共享一個bean。

    3)glabalSession

  4. <bean ..><aop:scoped-proxy/></bean>則註入的是動態代理對象。


八、基於註解的配置

  1. 使用註解定義Bean:@[email protected]:對DAO實現類的標註。


  2. 掃描註解定義的Bean:

    第一步:聲明context命名空間

xmlns:context="http://www.springframework.org/schema/context"

第二部:使用component-scan的base-package屬性指定一個需掃描的包。

<context:component-scan base-package="com.smart.anno">
    <context:include-filter type="aspectj" expression="com.smart.anno.*Plugin+"/>
    <context:include-filter type="aspectj" expression="com.smart.anno.MyComponent"/>
    <context:exclude-filter type="aspectj" expression="com.smart..*Controller+"/>
</context:component-scan>

user-default-filters屬性默認是掃描@Repository@Service @Controlle@Component 除非設為false。


3.自動裝配Bean

  1. @Autowired默認按類型匹配方式來找bean,找不到的時候會報異常,[email protected](reqired=false)[email protected]稱。

  2. 對類方法進行標註:[email protected]名稱。

  3. 延遲註入:@Lazy在屬性及目標Bean類上同時標註。

  4. @Resource註解要求提供一個Bean的名稱。二者都可以寫在字段












本文出自 “赤霄” 博客,請務必保留此出處http://cnslp.blog.51cto.com/11387491/1932793

在IOC中裝配Bean