首页 > 其他 > 详细

Spring的依赖注入

时间:2014-02-15 16:17:30      阅读:343      评论:0      收藏:0      [点我收藏+]

1。Spring的核心技术包括依赖注入DI 和控制反转 IOC

  依赖注入就是各个bean之间相互依赖,所有容器的作用就是为各个bean注入依赖关系

  依赖注入包括setter注入和构造器注入。

  下面说说构造器注入

  (1):构造器注入包括三种形式,分别是通过类型type.通过类.还有通过索引

  通过类注入有时候会造成循环注入,也就是A类ref B类.B类ref A类。可能容易无法辨别先实例化谁

具体如下:

<beans>

<bean name="foo" class="x.y.Foo">

<constructor-arg>

<bean class="x.y.Bar">

</consructor-arg>

<constrtuctor-arg>

<bean class="x.y.Baz">

</constructor-arg>

</bean>


</beans>

(2)通过类型匹配

public ExampleBean(int years, String ultimateAnswer) {
        this.years = years;
        this.ultimateAnswer = ultimateAnswer;
    }

<beans>

<bean in="a" class="x.y.Foo">

<constructor-arg type="int" value="111"/>

<constructor-arg type="String" value="zz">

</bean>

</beans>

(3):通过索引匹配

<beans>

<bean id="a" class="x.y.Foo">

<constructor-arg index="0" value="1"/>

<constructor-arg index="1" value="zz"/>

</bean>

</beans>


2:Setter注入

<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>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>

<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

3:还有一个static工厂返回

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

<bean id="anotherExampleBean" class="examples.AnotherBean"/>

<bean id="yetAnotherBean" class="examples.YetAnotherBean"/》

4:idref元素

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

例子

<bean id="A" class="..."/>

<bean id="b" class="...">

<property name="targetName">

<idref bean="A">

</property>

</bean>

等于与

<bean id="A" class=".."/>

<bean id="b" class="....">

<property name="a" value="A">

</bean>

第一种是用idref会在一开始验证bean是否存在。而第2中只会在运行时验证

(5):延迟加载

<bean id="a" class=".." lazy-ini="true"/>

使用延迟加载lazy-INI会在application.XML初始化时延迟创建bean.但如果里面依赖了另一个没有设置验证加载的bean则这个设置延迟加载的bean也会被初始化

(6)自动专配autowire

autowire的方便之处在于可以消除或减少属性或构造器参数的设置,给配置文件减减肥

自动装配有4种形式分别

byName

根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。例如,在bean定义中将autowire设置为by name,而该bean包含master属性(同时提供setMaster(..)方法),Spring就会查找名为master的bean定义,并用它来装配给master属性。

byType

如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置dependency-check="objects"让Spring抛出异常。

constructor

byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。

autodetect

通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式。



Spring的依赖注入

原文:http://sw3458856.blog.51cto.com/6499064/1359143

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!