Spring如何实现自动依赖注入-------autowire

【问题描述】当我们在使用Spring的IOC功能的时候,Spring提供了集中注入方式:属性注入,构造函数注入和工厂方法注入,我们更多的时候是使用的属性注入,即set方法注入。使用set方法注入要求我们在写bean的配置文件的时候,需要我们手动设置properties。诸如:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  7.     <bean id="testBean" class="com.jack.TestBean" scope="prototype"/>  
  8.     <bean id="testAction" class="com.jack.TestAction" scope="prototype">  
  9.         <property name="testBean" ref="testBean"/>  
  10.     </bean>  
  11.   
  12. </beans>  
  13. 这样使用Spring起来相当麻烦。

    【问题分析以及解决办法】我们在想,会不会有那种自动注入的方法呢?就是不用我们在Spring配置文件里面写入

    1. <property name="testBean" ref="testBean"/>  
    之类的语句,用某种方法让Spring自己就知道注入哪种类型的对象。

    当当当当。。。。。Spring这么smart的开源框架,早就给大家想到了。。。。他就是autowire

    Spring的bean有一个autowire的属性,它可以为以下的6个值。

    1. 1、 No:即不启用自动装配。Autowire默认的值。  
    2.   
    3. 2、 byName:通过属性的名字的方式查找JavaBean依赖的对象并为其注入。比如说类Computer有个属性printer,指定其autowire属性为byName后,Spring IoC容器会在配置文件中查找id/name属性为printer的bean,然后使用Seter方法为其注入。  
    4.   
    5. 3、 byType:通过属性的类型查找JavaBean依赖的对象并为其注入。比如类Computer有个属性printer,类型为Printer,那么,指定其autowire属性为byType后,Spring IoC容器会查找Class属性为Printer的bean,使用Seter方法为其注入。  
    6.   
    7. 4、 constructor:通byType一样,也是通过类型查找依赖对象。与byType的区别在于它不是使用Seter方法注入,而是使用构造子注入。  
    8.   
    9. 5、 autodetect:在byType和constructor之间自动的选择注入方式。  
    10.   
    11. 6、 default:由上级标签<beans>的default-autowire属性确定  
    一般,我们在使用的时候都会用byName,这种也就是说,当我们定义bean的时候,在给bean取名的时候,约定俗成的讲bean的id设置成首字母小写的类名。在我上面的例子里面即是testBean和testAction,这样当我们在testAction里面要自动注入TestBean的时候。就要在里面写一个Set方法,当然set方法的命名也是有规范的,那就是要set+类名,这里即是setTestBean。
    1. package com.jack;  
    2. /** 
    3.  * @author Jack Zhang 
    4.  * @version vb1.0 
    5.  * @Email virgoboy2004@163.com 
    6.  * @Date 2012-5-1 
    7.  */  
    8. public class TestAction  
    9. {  
    10.     private TestBean testBean;  
    11.       
    12.     public void setTestBean(TestBean testBean)  
    13.     {  
    14.         this.testBean = testBean;  
    15.     }  
    16.       
    17.     public String execute()  
    18.     {  
    19.         testBean.getCode();  
    20.         return "json";  
    21.     }  
    22. }  
    然后再更改配置文件:
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans  
    3.     xmlns="http://www.springframework.org/schema/beans"  
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5.     xmlns:p="http://www.springframework.org/schema/p"  
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
    7.     <bean id="testBean" class="com.jack.TestBean" scope="prototype"/>  
    8.     <bean id="testAction" class="com.jack.TestAction" scope="prototype"autowire="byName"/>  
    9.   
    10. </beans>  

有的人可能还会嫌麻烦,因为这样,每一个bean都要加上这一句,很多余,哈哈哈哈,spring项目组也给大家想到了,那就是看如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.         default-autowire="byName"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  8.     <bean id="testBean" class="com.jack.TestBean" scope="prototype"/>  
  9.     <bean id="testAction" class="com.jack.TestAction" scope="prototype"/>  
  10.   
  11. </beans>  


很好的文章,最起码我读懂了,给大家分享。ps:http://blog.youkuaiyun.com/virgoboy2004/article/details/7525795

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值