Spring 中的set注入与autowire注入

原文:http://blog.csdn.net/virgoboy2004/article/details/7525795

 

一、问题描述

当我们在使用Spring的IOC功能的时候,Spring提供了集中注入方式:属性注入,构造函数注入和工厂方法注入。

我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方法。

public class TestAction  
{  
    private TestBean testBean;  
      
    public void setTestBean(TestBean testBean)  
    {  
        this.testBean = testBean;  
    }  
      
    public String execute()  
    {  
        testBean.getCode();  
        return "json";  
    }  
}  

我们更多的时候是使用的属性注入,即set方法注入(对于需要注入的属性必须要有setter方法)。使用set方法注入要求我们在写bean的配置文件的时候,需要我们手动设置properties。

诸如:

<?xml version="1.0" encoding="UTF-8"?>  
<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-3.0.xsd">  
    <bean id="testBean" class="com.jack.TestBean" scope="prototype"/>  
    <bean id="testAction" class="com.jack.TestAction" scope="prototype">  
        <property name="testBean" ref="testBean"/>  
    </bean>  
  
</beans>  

这样使用Spring起来相当麻烦。但是在大型项目中还是推荐用set注入,不用自动注入和注解,这样在xml就能知道他们之间的依赖关系,便于维护和管理!!!

 

二、问题分析以及解决办法

我们在想,会不会有那种自动注入的方法呢?就是不用我们在Spring配置文件里面写入

<property name="testBean" ref="testBean"/>  

之类的语句,用某种方法让Spring自己就知道注入哪种类型的对象。

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

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

1、 No:即不启用自动装配。Autowire默认的值。  

2、 byName:通过属性的名字的方式查找JavaBean依赖的对象并为其注入。比如说类Computer有个属性printer,指定其autowire属性为byName后,Spring IoC容器会在配置文件中查找id/name属性为printer的bean,然后使用Seter方法为其注入。  

3、 byType:通过属性的类型查找JavaBean依赖的对象并为其注入。比如类Computer有个属性printer,类型为Printer,那么,指定其autowire属性为byType后,Spring IoC容器会查找Class属性为Printer的bean,使用Seter方法为其注入。  

4、 constructor:通byType一样,也是通过类型查找依赖对象。与byType的区别在于它不是使用Seter方法注入,而是使用构造子注入。  

5、 autodetect:在byType和constructor之间自动的选择注入方式。  

6、 default:由上级标签<beans>的default-autowire属性确定  

一般,我们在使用的时候都会用byName,这种也就是说,当我们定义bean的时候,在给bean取名的时候,约定俗成的讲bean的id设置成首字母小写的类名。

在我上面的例子里面即是testBean和testAction,这样当我们在testAction里面要自动注入TestBean的时候。就要在里面写一个Set方法,当然set方法的命名也是有规范的,那就是要set+类名,这里即是setTestBean。 

public class TestAction  
{  
    private TestBean testBean;  
      
    public void setTestBean(TestBean testBean)  
    {  
        this.testBean = testBean;  
    }  
      
    public String execute()  
    {  
        testBean.getCode();  
        return "json";  
    }  
}  

然后再更改配置文件:

<?xml version="1.0" encoding="UTF-8"?>  
<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-3.0.xsd">  
    <bean id="testBean" class="com.jack.TestBean" scope="prototype"/>  
    <bean id="testAction" class="com.jack.TestAction" scope="prototype"autowire="byName"/>  
  
</beans>  

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

<?xml version="1.0" encoding="UTF-8"?>  
<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"  
        default-autowire="byName"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
    <bean id="testBean" class="com.jack.TestBean" scope="prototype"/>  
    <bean id="testAction" class="com.jack.TestAction" scope="prototype"/>  
  
</beans>  

Okay,问题完美解决。。。。。。。。。

 

三、Spring @Autowired注解

 

1、配置文件的方法

我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方法。

2、@Autowired 的使用

Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。

要实现我们要精简程序的目的。需要这样来处理: 

1. 在applicationContext.xml中加入: 

<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->     
  <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  

Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。   


2. 修改在原来注入spirng容器中的bean的方法

在域变量上加上标签@Autowired,并且去掉 相应的get 和set方法

public class TestAction  
{
    @Autowired
    private TestBean testBean;  
      
    public String execute()  
    {  
        testBean.getCode();  
        return "json";  
    }  
}  

3. 在applicatonContext.xml中 把原来 引用的<porpery >标签也去掉。

<?xml version="1.0" encoding="UTF-8"?>  
<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-3.0.xsd">  
    <bean id="testBean" class="com.jack.TestBean" scope="prototype"/>  
    <bean id="testAction" class="com.jack.TestAction" scope="prototype"/>  
  
</beans>  

这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。   
  
按照上面的配置,Spring 将直接采用 Java 反射机制对 TestAction中的 testBean 这个私有成员变量进行自动注入。所以对成员变量使用 @Autowired 后,您大可将它们的 setter 方法(setTestBean)从 testAction 中删除。   

3、@Autowired 作用在Setter和构造函数上

@Autowired除了用在私有属性上,还可以用在Setter方法和构造方法中


当然,您也可以通过 @Autowired 对方法或构造函数进行标注,如果构造函数有两个入参,分别是 bean1 和 bean2,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 CountryService (Bean1 bean1 ,Bean2 bean2) 的入参来创建 CountryService Bean。

public class TestAction  
{
    
    private TestBean testBean;  

    @Autowired
    public TestAction(TestBean testBean) {
        this.testBean = testBean
    }
      
    public String execute()  
    {  
        testBean.getCode();  
        return "json";  
    }  
}  

由于 Boss() 构造函数有个入参,@Autowired 将寻找和它类型匹配的 Bean,将它们作为 构造函数 的入参来创建 TestAction。  

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring,接口注入有两种方式:byType和byName。 通过byType方式进行接口注入时,Spring容器会根据bean定义的类来查找与依赖类型相同的bean,并将其注入到另一个bean。这个过程需要借助setter注入来完成,因此被注入的bean必须存在相应的set方法。然而,当XML文件存在多个相同类型但名称不同的实例bean时,byType模式可能会导致注入失败,因为Spring容器无法确定应该注入哪个bean实例。在这种情况下,可以通过设置autowire-candidate属性为false来过滤不需要注入的实例bean,以帮助Spring容器确定注入哪个bean实例。\[1\] 另一种方式是byName方式。通过byName方式进行接口注入时,Spring容器会根据bean的名称来查找与依赖类型相同的bean,并将其注入到另一个bean。在XML文件,可以使用@Autowired注解的@Qualifier("名称")来指定要注入的bean的名称,以帮助Spring容器确定注入哪个bean实例。\[3\] 需要注意的是,无论是使用byType还是byName方式进行接口注入,被注入的bean必须存在默认的空参构造方法,否则Spring无法实例化对象,导致报错。如果在类定义了带有参数的构造方法,一定要同时提供一个空参构造方法。\[2\] #### 引用[.reference_title] - *1* *3* [spring注入方式](https://blog.csdn.net/shangliangren/article/details/123932809)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Spring常用的三种注入方式](https://blog.csdn.net/weixin_30553777/article/details/99878397)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值