http://blog.csdn.net/yerenyuan_pku/article/details/52860713

前面我们已经学会使用@Resource注解注入属性,并且我们还编码剖析了@Resource注解的实现原理。现在我们来学习使用@Autowire注解注入属性,本文是建立在编码剖析@Resource注解的实现原理的案例基础上的。

用@Autowire注解完成属性装配

@Autowire注解和@Resource一样,同样也可以标注在字段或属性的setter方法上,但它默认按类型装配。 
我们将@Autowire注解标注在字段上,如将PersonServiceBean类的代码修改为:

public class PersonServiceBean implements PersonService {
@Autowired private PersonDao personDao; public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
} @Override
public void save() {
personDao.add();
}
}
  • 1

接着将SpringTest类的代码改为:

public class SpringTest {

    @Test
public void instanceSpring() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService) ctx.getBean("personService");
personService.save();
ctx.close();
} }
  • 1

测试instanceSpring()方法,可发现Eclipse控制台打印: 

如果我们想使用按名称装配,可以结合@Qualifier注解一起使用,如将PersonServiceBean类的代码修改为:

public class PersonServiceBean implements PersonService {
@Autowired @Qualifier("personDaoxxxx") private PersonDao personDao; public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
} @Override
public void save() {
personDao.add();
}
}
  • 1

再次测试instanceSpring()方法,可发现Eclipse控制台打印: 

注意:@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它的required属性为false。如:

@Autowired(required=true) @Qualifier("personDaoxxxx") private PersonDao personDao;  
  • 1
  • 1

required=true代表字段personDao必须要注入值,也即是说在Spring容器中根据类型找不到对应的bean,那就会报异常;required=false意味着在Spring容器中根据类型找不到对应的的bean,就会把该字段设为null。

依赖注入——自动装配依赖对象

对于自动装配,大家了解一下就可以了,实在不推荐大家使用。例子:

<bean id="..." class="..." autowire="byType"/>

autowire属性取值如下:

  • byType:按类型装配,可以根据属性的类型,在容器中寻找跟该类型匹配的bean。如果发现多个,那么将会抛出异常。如果没有找到,即属性值为null。
  • byName:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有找到,即属性值为null。
  • constructor与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。
  • autodetect:通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式

现在我们重点关注byType和byName这两个属性值。为了试验,我们将PersonServiceBean类的代码改为:

public class PersonServiceBean implements PersonService {
private PersonDao personDao; public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
} @Override
public void save() {
personDao.add();
}
}
  • 1

此时要想自动装配依赖对象,试着将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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:annotation-config/> <bean id="personDaoxxxx" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" autowire="byType"></bean>
</beans>
  • 1

此时测试SpringTest类的instanceSpring()方法,可看到Eclipse控制台打印: 

若将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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:annotation-config/> <bean id="personDaoxxxx" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" autowire="byName"></bean>
</beans>
  • 1

再次测试SpringTest类的instanceSpring()方法,发现报空指针异常,明显就是没有为属性注入值,所以我们将Spring的配置文件中的

<bean id="personDaoxxxx" class="cn.itcast.dao.impl.PersonDaoBean"></bean>

修改为:

<bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean>

此时再次测试SpringTest类的instanceSpring()方法,可发现Eclipse控制台打印: 

 
 

(转)@Autowire注解与自动装配的更多相关文章

  1. @Resource注解完成自动装配

    @Resource注解是通过名字来自动装配的.在spring中自动装配的模式如果是通过名字来自动装配那么必须保证bean的名字和pojo 的属性名一直. 下面是详细代码:说明了@Resource注解是 ...

  2. Spring学习记录(十一)---使用注解和自动装配

    Spring支持用注解配置Bean,更简便. 上面的组件,是根据实际情况配的.比如写的一个类,是做业务处理的,那就用注解@Service表示服务层组件,以此类推.将整体分成不同部分. 要在xml加入c ...

  3. Spring中@Autowired注解与自动装配

    1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...

  4. Spring2.5学习3.3_@Autowire注解实现手动装配

    @Autowired默认按类型装配,假设我在personDao 字段上加了@Autowired注解,那么就会默认取personDao 字段的类型在Spring容器中寻找与这个类型匹配的bean,寻找到 ...

  5. Spring - bean的autowire属性(自动装配)

    当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式.但是对于大型项目,假设有一个bean A被多个bean引用注 ...

  6. 使用Spring的JavaConfig 和 @Autowired注解与自动装配

    1 JavaConfig  配置方法 之前我们都是在xml文件中定义bean的,比如: 1 2 3 4 5 6 7 8 <beans xmlns="http://www.springf ...

  7. springboot自动装配(1)---@SpringBootApplication注解怎么自动装配各种组件

    1.对于springboot个人认为它就是整合了各种组件,然后提供对应的自动装配和启动器(starter) 2.@SpringBootApplication注解其实就是组合注解,通过它找到自动装配的注 ...

  8. Spring@Autowired注解与自动装配

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

  9. 【转】Spring@Autowired注解与自动装配

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

随机推荐

  1. 区间DP中的环形DP

    vijos1312 链接:www.vijos.org/p/1312 题目分析:经典的环形DP(区间DP) 环形DP,首先解环过程,把数组复制一遍,n个数变成2n个数,从而实现解环 dp[i][j]表示 ...

  2. MongoDB之shard_副本集和分片部署

    机器角色分配和拓扑环境如下: -------------------配置副本集s1-------------------------------1.创建目录在s1h1上创建如下目录[root@node ...

  3. Bootstrap-CL:按钮下拉菜单

    ylbtech-Bootstrap-CL:按钮下拉菜单 1.返回顶部 1. Bootstrap 按钮下拉菜单 本章将讲解如何使用 Bootstrap class 向按钮添加下拉菜单.如需向按钮添加下拉 ...

  4. margin -------总结(block inline 可置换元素)

    margin在块元素.内联元素中的区别 block元素(块元素)大致有:P|H1|H2|H3|H4|H5|H6|UL|OL|PRE| DL | DIV | NOSCRIPT | BLOCKQUOTE ...

  5. 解决Excel打开UTF-8编码CSV文件乱码的问题

    打开 Excel,执行“数据”->“自文本”,选择 CSV 文件,出现文本导入向导,选择“分隔符号”,下一步,勾选“逗号”,去掉“ Tab 键”,下一步,完成,在“导入数据”对话框里,直接点确定 ...

  6. html行内要素与块级要素

    行内要素:在一行里,不可设置width和height,不能上下外铺(margin) span 块状要素,标准的 div

  7. kettle系列-我的开源kettle调度、管理平台[kettle-manager]介绍

    kettle管理工具 专门为kettle这款优秀的ETL工具开发的web端调度.管理工具. 新版本 项目简介 kettle作为非常优秀的开源ETL工具得到了非常广泛的使用,一般的使用的都是使用客户端操 ...

  8. Adore

    (非公共题目) 问题描述 小 w 偶然间⻅到了一个 DAG.这个 DAG 有 m 层,第一层只有一个源点,最后一层只有一个汇点,剩下的每一层都有 k 个节点. 现在小 w 每次可以取反第 i(1 &l ...

  9. (2)javascript的基本语法、数据结构、变量

    本篇学习资料主要讲解javascript的基本语法.数据结构.变量      无论是传统的编程语言,还是脚本语言,都具有数据类型.常量和变量.运算符.表达式.注释语句.流程控制语句等基本元素构成,这些 ...

  10. 浅谈Floyd的三种用法 By cellur925

    Floyd大家可能第一时间想到的是他求多源最短路的n³算法.其实它还有另外两种算法的嘛qwq.写一发总结好了qwq. 一.多源最短路 放段代码跑,注意枚举顺序,用邻接矩阵存图.本质是一种动规. 复杂度 ...