zoukankan      html  css  js  c++  java
  • Spring配置文件解析-依赖注入

    转:http://blog.csdn.net/zzh87615/article/details/5915658

    ————————————————————————————————————-

    1.构造器注入
    基于构造器的DI通过调用带参数的构造器来实现,每个参数代表着一个依赖。此外,还可通过给stattic工厂方法传参数来构造bean。
    构造器参数解析根据参数类型进行匹配,如果bean的构造器参数类型定义非常明确,那么在bean被实例化的时候,bean定义中构造器参数的定义顺序就是这些参数的顺序,依次进行匹配,否则可以根据构造器参数类型匹配,如下:

    1. <bean id="exampleBean" class="examples.ExampleBean">  
    2.   <constructor-arg type="int" value="7500000"/>  
    3.   <constructor-arg type="java.lang.String" value="42"/>  
    4. </bean>  

    还可以通过index属性来显式指定构造参数的索引,比如下面的例子:

    1. <bean id="exampleBean" class="examples.ExampleBean">  
    2.   <constructor-arg index="0" value="7500000"/>  
    3.   <constructor-arg index="1" value="42"/>  
    4. </bean>  


    2.Setter注入
    通过调用无参构造器或无参static工厂方法实例化bean之后,调用该bean的setter方法,即可实现基于setter的DI。
    首先是一个用XML格式定义的Setter DI例子:

    1. <bean id="exampleBean" class="examples.ExampleBean">  
    2.   <!-- setter injection using the nested <ref/> element -->  
    3.   <property name="beanOne"><ref bean="anotherExampleBean"/></property>  
    4.   <!-- setter injection using the neater 'ref' attribute -->  
    5.   <property name="beanTwo" ref="yetAnotherBean"/>  
    6.   <property name="integerProperty" value="1"/>  
    7. </bean>  

    在xml bean定义中指定的构造器参数将被用来作为传递给类ExampleBean构造器的参数。现在来研究一个替代构造器的方法,采用static工厂方法返回对象实例:

    1. <bean id="exampleBean" class="examples.ExampleBean"  
    2.       factory-method="createInstance">  
    3.   <constructor-arg ref="anotherExampleBean"/>  
    4.   <constructor-arg ref="yetAnotherBean"/>  
    5.   <constructor-arg value="1"/>   
    6. </bean>  


    <property/> 和<constructor-arg/> 元素中可以使用'value' 属性.
    也可以按照下面这种方式配置一个java.util.Properties实例:

    1. <bean id="mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">             
    2.    <!-- typed as a java.util.Properties -->  
    3.    <property name="properties">  
    4.       <value>  
    5.          jdbc.driver.className=com.mysql.jdbc.Driver  
    6.          jdbc.url=jdbc:mysql://localhost:3306/mydb  
    7.       </value>  
    8.    </property>  
    9. </bean>  


    idref元素用来将容器内其它bean的id传给<constructor-arg/> 或 <property/>元素,同时提供错误验证功能。

    1. <bean id="theTargetBean" class="..."/>  
    2. <bean id="theClientBean" class="...">  
    3.     <property name="targetName">  
    4.         <idref bean="theTargetBean" />  
    5.     </property>  
    6. </bean>  


    如果被引用的bean在同一XML文件内,且bean名字就是bean id,那么可以使用local属性,此属性允许XML解析器在解析XML文件时对引用的bean进行验证。
    引用其它的bean:
    第一种形式也是最常见的形式是通过使用<ref/>标记指定bean属性的目标bean,通过该标签可以引用同一容器或父容器内的任何bean(无论是否在同一XML文件中)。XML 'bean'元素的值既可以是指定bean的id值也可以是其name值。

    1. <ref bean="someBean"/>  

    第二种形式是使用ref的local属性指定目标bean,它可以利用XML解析器来验证所引用的bean是否存在同一文件中。local属性值必须是目标bean的id属性值。如果在同一配置文件中没有找到引用的bean,XML解析器将抛出一个例外。如果目标bean是在同一文件内,使用local方式就是最好的选择(为了尽早地发现错误)。

    1. <ref local="someBean"/>  

    第三种方式是通过使用ref的parent属性来引用当前容器的父容器中的bean。parent属性值既可以是目标bean的id值,也可以是name属性值。而且目标bean必须在当前容器的父容器中。使用parent属性的主要用途是为了用某个与父容器中的bean同名的代理来包装父容器中的一个bean(例如,子上下文中的一个bean定义覆盖了他的父bean)。

    1. <!-- in the parent context -->  
    2. <bean id="accountService" class="com.foo.SimpleAccountService">  
    3.     <!-- insert dependencies as required as here -->  
    4. </bean>  
    5. <!-- in the child (descendant) context -->  
    6. <bean id="accountService"  <-- notice that the name of this bean is the same as the name of the 'parent' bean  
    7.       class="org.springframework.aop.framework.ProxyFactoryBean">  
    8.       <property name="target">  
    9.           <ref parent="accountService"/>  <-- notice how we refer to the parent bean  
    10.       </property>  
    11.     <!-- insert other configuration and dependencies as required as here -->  
    12. </bean>  

    内部bean:
    所谓的内部bean(innerbean)是指在一个bean的<property/>或<constructor-arg/>元素中使用<bean/>元素定义的bean。内部bean定义不需要有id或name属性,
    即使指定id 或 name属性值也将会被容器忽略。
    集合:
    通过<list/>、<set/>、<map/>及<props/>元素可以定义和设置与Java Collection类型对应List、Set、Map及Properties的值。

    1. <bean id="moreComplexObject" class="example.ComplexObject">  
    2.   <!-- results in a setAdminEmails(java.util.Properties) call -->  
    3.   <property name="adminEmails">  
    4.     <props>  
    5.         <prop key="administrator">administrator@example.org</prop>  
    6.         <prop key="support">support@example.org</prop>  
    7.         <prop key="development">development@example.org</prop>  
    8.     </props>  
    9.   </property>  
    10.   <!-- results in a setSomeList(java.util.List) call -->  
    11.   <property name="someList">  
    12.     <list>  
    13.         <value>a list element followed by a reference</value>  
    14.         <ref bean="myDataSource" />  
    15.     </list>  
    16.   </property>  
    17.   <!-- results in a setSomeMap(java.util.Map) call -->  
    18.   <property name="someMap">  
    19.     <map>  
    20.         <entry>  
    21.             <key>  
    22.                 <value>an entry</value>  
    23.             </key>  
    24.             <value>just some string</value>  
    25.         </entry>  
    26.         <entry>  
    27.             <key>  
    28.                 <value>a ref</value>  
    29.             </key>  
    30.             <ref bean="myDataSource" />  
    31.         </entry>  
    32.     </map>  
    33.   </property>  
    34.   <!-- results in a setSomeSet(java.util.Set) call -->  
    35.   <property name="someSet">  
    36.     <set>  
    37.         <value>just some string</value>  
    38.         <ref bean="myDataSource" />  
    39.     </set>  
    40.   </property>  
    41. </bean>  

    集合的合并:
    SpringIoC容器将支持集合的合并。这样我们可以定义parent-style和child-style的<list/>、<map/>、<set/>或<props/>元素,子集合的值从其父集合继承和覆盖
    而来;也就是说,父子集合元素合并后的值就是子集合中的最终结果,而且子集合中的元素值将覆盖父集全中对应的值。

    1. <beans>  
    2. <bean id="parent" abstract="true" class="example.ComplexObject">  
    3.     <property name="adminEmails">  
    4.         <props>  
    5.             <prop key="administrator">administrator@example.com</prop>  
    6.             <prop key="support">support@example.com</prop>  
    7.         </props>  
    8.     </property>  
    9. </bean>  
    10. <bean id="child" parent="parent">  
    11.     <property name="adminEmails">  
    12.         <!-- the merge is specified on the *child* collection definition -->  
    13.         <props merge="true">  
    14.             <prop key="sales">sales@example.com</prop>  
    15.             <prop key="support">support@example.co.uk</prop>  
    16.         </props>  
    17.     </property>  
    18. </bean>  
    19. <beans>  


    Nulls:
    <null/>用于处理null值。Spring会把属性的空参数当作空字符串处理:

    1. <bean class="ExampleBean">  
    2.   <property name="email"><null/></property>  
    3. </bean>  


    map中entry元素的简写形式为key/key-ref 和 value /value-ref属性。
    使用p名称空间配置属性:

    1. <beans xmlns="http://www.springframework.org/schema/beans"  
    2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xmlns:p="http://www.springframework.org/schema/p"  
    4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
    5.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">   
    6.       
    7.     <bean name="classic" class="com.example.ExampleBean">  
    8.         <property name="email" value="foo@bar.com/>  
    9.     </bean>  
    10.       
    11.     <bean name="p-namespace" class="com.example.ExampleBean"  
    12.           p:email="foo@bar.com"/>  
    13. </beans>  

    若要引用其他Bean,如下:

    1. <beans xmlns="http://www.springframework.org/schema/beans"  
    2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xmlns:p="http://www.springframework.org/schema/p"  
    4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
    5.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">   
    6.       
    7.     <bean name="john-classic" class="com.example.Person">  
    8.         <property name="name" value="John Doe"/>  
    9.         <property name="spouse" ref="jane"/>  
    10.     </bean>  
    11.   
    12.     <bean name="john-modern"   
    13.         class="com.example.Person"  
    14.         p:name="John Doe"  
    15.         p:spouse-ref="jane"/>  
    16.   
    17.     <bean name="jane" class="com.example.Person">  
    18.         <property name="name" value="Jane Doe"/>  
    19.     </bean>  
    20. </beans>  

    使用depends-on:
    depends-on属性可以用于当前bean初始化之前显式地强制一个或多个bean被初始化。
    若需要表达对多个bean的依赖,可以在'depends-on'中将指定的多个bean名字用分隔符进行分隔,分隔符可以是逗号、空格及分号等。

    1. <bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">  
    2.   <property name="manager" ref="manager" />  
    3. </bean>  
    4.   
    5. <bean id="manager" class="ManagerBean" />  
    6. <bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />  

    延迟初始化bean:
    在XML配置文件中,延迟初始化将通过<bean/>元素中的lazy-init属性来进行控制。

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


    在容器层次上通过在<beans/>元素上使用'default-lazy-init'属性来控制延迟初始化也是可能的。

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


    自动装配(autowire)协作者:
    Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系。autowire一共有五种类型:no、byName(根据属性名自动装配)、
    byType(如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配)、constructor(与byType的方式类似,不同之处在于它应用于构造器参数)、
    autodetect(通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配)
    依赖检查:
    Spring除了能对容器中bean的依赖设置进行检查外,还可以检查bean定义中实际属性值的设置,当然也包括采用自动装配方式设置属性值的检查。
    依赖检查默认为not,它有几种不同的使用模式,在xml配置文件中,可以在bean定义中为dependency-check属性使用以下几种值:
     none: 没有依赖检查,如果bean的属性没有值的话可以不用设置。
     simple: 对于原始类型及集合(除协作者外的一切东西)执行依赖检查
     object: 仅对协作者执行依赖检查
     all: 对协作者,原始类型及集合执行依赖检查

  • 相关阅读:
    JavaWeb学习总结(二)——Tomcat服务器学习和使用(一)
    JavaWeb学习总结(一)——JavaWeb开发入门
    javaweb学习总结(四)——Http协议
    Eclipse中应用的调试
    Java Web快速入门——全十讲
    Spring 系列: Spring 框架简介
    分布式环境中三种Session管理方法的使用场景及优缺点
    Cookie/Session机制详解
    HTTP 协议详解
    Webx3学习笔记(2)——基本流程
  • 原文地址:https://www.cnblogs.com/kaikailele/p/3959414.html
Copyright © 2011-2022 走看看