Spring配置文件解析-依赖注入

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

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

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

[xhtml] view
plain
copyprint?

  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>

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

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

[xhtml] view
plain
copyprint?

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

<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="7500000"/>
<constructor-arg index="1" value="42"/>
</bean>
2.Setter注入
通过调用无参构造器或无参static工厂方法实例化bean之后,调用该bean的setter方法,即可实现基于setter的DI。
首先是一个用XML格式定义的Setter
DI例子:

[xhtml] view
plain
copyprint?

  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>

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

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

[xhtml] view
plain
copyprint?

  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>

<bean id="exampleBean" class="examples.ExampleBean"
factory-method="createInstance">
<constructor-arg ref="anotherExampleBean"/>
<constructor-arg ref="yetAnotherBean"/>
<constructor-arg value="1"/>
</bean>
<property/> 和<constructor-arg/>
元素中可以使用‘value‘ 属性.
也可以按照下面这种方式配置一个java.util.Properties实例:

[xhtml] view
plain
copyprint?

  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>

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

[xhtml] view
plain
copyprint?

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

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

[xhtml] view
plain
copyprint?

  1. <ref bean="someBean"/>

<ref bean="someBean"/>

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

[xhtml] view
plain
copyprint?

  1. <ref local="someBean"/>

<ref local="someBean"/>

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

[xhtml] view
plain
copyprint?

  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>

<!-- in the parent context -->
<bean id="accountService" class="com.foo.SimpleAccountService">
<!-- insert dependencies as required as here -->
</bean>
<!-- in the child (descendant) context -->
<bean id="accountService" <-- notice that the name of this bean is the same as the name of the ‘parent‘ bean
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref parent="accountService"/> <-- notice how we refer to the parent bean
</property>
<!-- insert other configuration and dependencies as required as here -->
</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的值。

[xhtml] view
plain
copyprint?

  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">[email protected]</prop>
  6. <prop key="support">[email protected]</prop>
  7. <prop key="development">[email protected]</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>

<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</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>an entry</value>
</key>
<value>just some string</value>
</entry>
<entry>
<key>
<value>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>

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

[xhtml] view
plain
copyprint?

  1. <beans>
  2. <bean id="parent" abstract="true" class="example.ComplexObject">
  3. <property name="adminEmails">
  4. <props>
  5. <prop key="administrator">[email protected]</prop>
  6. <prop key="support">[email protected]</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">[email protected]</prop>
  15. <prop key="support">[email protected]</prop>
  16. </props>
  17. </property>
  18. </bean>
  19. <beans>

<beans>
<bean id="parent" abstract="true" class="example.ComplexObject">
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.com</prop>
<prop key="support">support@example.com</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">sales@example.com</prop>
<prop key="support">support@example.co.uk</prop>
</props>
</property>
</bean>
<beans>
Nulls:
<null/>用于处理null值。Spring会把属性的空参数当作空字符串处理:

[xhtml] view
plain
copyprint?

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

<bean class="ExampleBean">
<property name="email"><null/></property>
</bean>
map中entry元素的简写形式为key/key-ref 和 value
/value-ref属性。
使用p名称空间配置属性:

[c-sharp] view
plain
copyprint?

  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. <bean name="classic" class="com.example.ExampleBean">
  7. <property name="email" value="[email protected]/>
  8. </bean>
  9. <bean name="p-namespace" class="com.example.ExampleBean"
  10. p:email="[email protected]"/>
  11. </beans>

<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-2.5.xsd">

<bean name="classic" class="com.example.ExampleBean">
<property name="email" value="foo@bar.com/>
</bean>

<bean name="p-namespace" class="com.example.ExampleBean"
p:email="foo@bar.com"/>
</beans>

若要引用其他Bean,如下:

[c-sharp] view
plain
copyprint?

  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. <bean name="john-classic" class="com.example.Person">
  7. <property name="name" value="John Doe"/>
  8. <property name="spouse" ref="jane"/>
  9. </bean>
  10. <bean name="john-modern"
  11. class="com.example.Person"
  12. p:name="John Doe"
  13. p:spouse-ref="jane"/>
  14. <bean name="jane" class="com.example.Person">
  15. <property name="name" value="Jane Doe"/>
  16. </bean>
  17. </beans>

<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-2.5.xsd">

<bean name="john-classic" class="com.example.Person">
<property name="name" value="John Doe"/>
<property name="spouse" ref="jane"/>
</bean>

<bean name="john-modern"
class="com.example.Person"
p:name="John Doe"
p:spouse-ref="jane"/>

<bean name="jane" class="com.example.Person">
<property name="name" value="Jane Doe"/>
</bean>
</beans>

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

[c-sharp] view
plain
copyprint?

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

<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
<property name="manager" ref="manager" />
</bean>

<bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />

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

[xhtml] view
plain
copyprint?

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

<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
在容器层次上通过在<beans/>元素上使用‘default-lazy-init‘属性来控制延迟初始化也是可能的。

[xhtml] view
plain
copyprint?

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

<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</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: 对协作者,原始类型及集合执行依赖检查

时间: 2024-12-24 14:20:41

Spring配置文件解析-依赖注入的相关文章

spring四种依赖注入方式

平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程序员实例化,而是通过spring容器帮我们new指定实例并且将实例注入到需要该对象的类中.依赖注入的另一种说法是"控制反转",通俗的理解是:平常我们new一个实例,这个实例的控制权是我们程序员,而控制反转是指new实例工作不由我们程序员来做而是交给spring容器来做. spring有多种

spring 四种依赖注入方式以及注解注入方式

平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程序员实例化, 而是通过spring容器帮我们new指定实例并且将实例注入到需要该对象的类中.依赖注入的另一种说法是“控制反转”,通俗的理解是:平常我们new一个实例,这个实例的控制权是我们程序员, 而控制反转是指new实例工作不由我们程序员来做而是交给spring容器来做. spring有多种依赖注

JavaEE开发之Spring中的依赖注入与AOP编程

一.快速创建Mava管理的Spring工程 因为本篇博客是讨论关于Spring的东西,所以我们就不创建WebApp的工程了.我们使用Spring来快速的创建一个Maven管理的工程.如下所示找到File->New->Maven Project选项来创建一个新的Maven Project,具体如下所示: 下方我们选择创建一个简单的Maven工程,跳过模板的选择.上篇博客我们在创建Maven工程时,是没有选择下方这个选项的,然后我们选择了一个WebApp的模板.而本篇博客,我们不需要WebApp的

spring四种依赖注入方式 (set,构造器,工厂,注解 )

平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程序员实例化,而是通过spring容器帮我们new指定实例并且将实例注入到需要该对象的类中.依赖注入的另一种说法是"控制反转",通俗的理解是:平常我们new一个实例,这个实例的控制权是我们程序员,而控制反转是指new实例工作不由我们程序员来做而是交给spring容器来做. spring有多种

SSH问题:系统启动时,spring配置文件解析失败,报”cvc-elt.1: 找不到元素 &#39;beans&#39; 的声明“异常

现象:spring加载配置文件applicationContext.xml出错,抛出nested exception is og.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 47; cvc-elt.1: 找不到元素 'beans' 的声明r的异常信息. 造成该异常原因有两种: 第一,配置文件头部配置的xsd版本信息不正确,造成解析时出错.spring头部xsd或dtd校验文件的查找分两步,第一先从本地jar包中找,如果找到则用

系统启动时,spring配置文件解析失败,报”cvc-elt.1: 找不到元素 &#39;beans&#39; 的声明“异常

现象:spring加载配置文件applicationContext.xml出错,抛出nested exception is og.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 47; cvc-elt.1: 找不到元素 'beans' 的声明r的异常信息. 造成该异常原因有两种: 第一,配置文件头部配置的xsd版本信息不正确,造成解析时出错.spring头部xsd或dtd校验文件的查找分两步,第一先从本地jar包中找,如果找到则用

spring学习笔记-依赖注入

学习过程访问的地址,按顺序理解: [email protected]注解与自动装配 spring四种依赖注入方式 Spring注解注入 <context:component-scan>使用说明 详细:Spring零配置通过注解实现Bean依赖注入总结

简单解析依赖注入(控制反转)在Spring中的应用

IoC——Inversion of Control  控制反转DI——Dependency Injection   依赖注入 大家都知道,依赖注入是Spring中非常重要的一种设计模式.可能很多初学者对这种看起来高深的东西有一种抗拒,这里就 简单介绍一下到底什么是依赖注入,Spring到底又到底是如何运用依赖注入的. 依赖关系:在A组件中要引用B组件的对象,则称A依赖于B依赖关系会造成各组件之间的硬编码,为解决依赖关系,一般的解决方法如下: 1.A组件先创建B组件,在调用B组件方法2.A组件先通

.NET Core实战项目之CMS 第三章 入门篇-源码解析配置文件及依赖注入

作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/9998021.html 写在前面 上篇文章我给大家讲解了ASP.NET Core的概念及为什么使用它,接着带着你一步一步的配置了.NET Core的开发环境并创建了一个ASP.NET Core的mvc项目,同时又通过一个实战教你如何在页面显示一个Content的列表.不知道你有没有跟着敲下代码,千万不要做眼高手低的人哦.这篇文章我们就会设计一些复杂的概念了,因为要对ASP.NET Core的启动及运行原