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 {@Testpublic 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控制台打印: 

转载于:https://www.cnblogs.com/telwanggs/p/6913253.html

(转)@Autowire注解与自动装配相关推荐

  1. @Autowire注解与自动装配

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

  2. Spring基于注解的自动装配

    Spring基于注解的自动装配 基于XML的自动装配是在配置文件的bean里设置autowire属性,有byType,byName的方式.而基于注解的自动装配同样是这样只不过我们直接在成员变量上直接标 ...

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

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

  4. 万字长文,图文并茂的给你讲清SpringBoot注解,自动装配原理!

    你知道的越多,不知道的就越多,业余的像一棵小草! 你来,我们一起精进!你不来,我和你的竞争对手一起精进! 编辑:业余草 cnblogs.com/jing99/p/11504113.html 推荐:ht ...

  5. Spring注解驱动开发学习总结8:自动装配注解@Autowire、@Resource、@Inject

    Spring注解驱动开发学习总结8:自动装配注解@Autowire.@Resource.@Inject 1.自动装配@Autowire.@Resource.@Inject 1.1 构建bookDao ...

  6. Spring(三)——HelloSpring、IOC创建对象的方式、属性注入、自动装配、使用注解开发

    文章目录 1. 简介 2. IOC理论推导 3. HelloSpring 4. IOC创建对象的方式 4.1 使用无参构造创建对象(默认) 4.2 使用有参构造创建对象 5. Spring配置 5.1 ...

  7. Spring自动装配(autowire)篇

    什么是自动装配 自动装配帮我们省去了 property标签配置操作,Spring会自动根据 属性名称,类型,构造器来进行自动注入. 例如不进行自动装配配置如下: <bean id="u ...

  8. Spring—自动装配与注解自动装配

    目录 自动装配 Spring支持5种自动装配模式 演示自动装配 1. no 默认装配方式 2.使用 byName 自动装配 3.使用 byType 自动装配 4.使用 constructor 自动装配 ...

  9. [Spring实战系列](13)使用注解自动装配

    1. 简介 从Spring2.5开始,我们就可以使用注解的自动装配方式装配Spring Bean的属性.使用注解自动装配方式与在XML中使用autowire属性自动装配没有太大区别.那为啥还要研发出这 ...

最新文章

  1. Ubuntu Linux root password - default password
  2. Apple Watch开发快速入门教程
  3. 为什么一般要定义析构函数为虚析构函数
  4. 『原创』网站测试计划模板
  5. 安装CentOs 5.5后无法显中文(中文乱码)
  6. python基础——字典
  7. 日期减三个月oracle_ORACLE 日期加减操作
  8. 我的世界服务器linux加mod,在Linux下搭建带MOD 我的世界(Minecraft)服务器
  9. 滨州学院计算机自荐考试题型,滨州学院期末考试试卷标准样式.doc
  10. 使用Chrome Frame插件解决IE浏览器兼容问题
  11. Spring Cloud教程合集
  12. node_modules中的.cache文件夹的占用空间越来越大
  13. 2021-2027全球与中国抽屉拉手市场情况与未来趋势研究报告
  14. Xcode13.3 13.2以及Flutter新版本的稳定性问题
  15. 微信小游戏Laya引擎声音Bug的解决方案
  16. 2023年天津仁爱学院高职升本科专业考试报考须知
  17. 第29章基于锁的并发数据结构
  18. java计算乘地铁费用_Java_地铁购票系统
  19. p标签换行导致的问题
  20. 如何选择Java培训机构

热门文章

  1. Hadoop之内存问题
  2. java文本区水平对齐方式,如何将文本居中在水平StackLayout中?
  3. STM32网络之中断
  4. python布尔类型运算_python基础之布尔运算、集合
  5. linux下ip协议(V4)的实现(三)
  6. Linux输入子系统:事件的编码 -- event-codes.txt
  7. 从程序员到项目经理(七):程序员加油站 -- 完美主义也是一种错
  8. 谷粒商城集群篇爬坑笔记--Gitee拉取项目报错、项目target文件不存在(部分项目不全)、SonarQube报错
  9. 仓鼠大厦java下载_仓鼠大厦食盐宫殿 世界古怪酒店TOP10(组图)
  10. 【小项目】学生信息登记系统