@Autowired注解可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。

注意:@Autowired默认是按照类型来注入的。

看下面的例子:例子是以对成员变量(field)为例进行的

  1. public class Person {
  2. private String name;
  3. private String address;
  4. private int age;
  5. public void setName(String name) {
  6. this.name = name;
  7. }
  8. public String getName() {
  9. return name;
  10. }
  11. public int getAge() {
  12. return age;
  13. }
  14. public void setAge(int age) {
  15. this.age = age;
  16. }
  17. public void setAddress(String address) {
  18. this.address = address;
  19. }
  20. public String getAddress() {
  21. return address;
  22. }
  23. @Override
  24. public String toString() {
  25. return "[name:"+getName()+
  26. ",address:" + getAddress()+
  27. ",age:" +getAge()+
  28. "]";
  29. }

另外一个Customer类有一个Person类型的成员,现用@Autowired注入:

  1. public class Customer
  2. {
  3. @Autowired
  4. private Person person;
  5. private int type;
  6. private String action;
  7. public String getAction() {
  8. return action;
  9. }
  10. public void setAction(String action) {
  11. this.action = action;
  12. }
  13. public int getType() {
  14. return type;
  15. }
  16. public void setType(int type) {
  17. this.type = type;
  18. }
  19. @Override
  20. public String toString() {
  21. return "[Type:"+getType()+
  22. ",action:"+getAction()+
  23. ","+"person:"+
  24. person.toString();
  25. }
  26. }

在配置文件中加入

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  8. <!--@Autowired-->
  9. <context:annotation-config/>
  10. <bean id="CustomerBean" class="com.mkyong.common.Customer">
  11. <property name="action" value="buy" />
  12. <property name="type" value="1" />
  13. </bean>
  14. <bean id="PersonBean" class="com.mkyong.common.Person">
  15. <property name="name" value="mkyong" />
  16. <property name="address" value="address ABC" />
  17. <property name="age" value="29" />
  18. </bean>
  19. </beans>

测试程序:

  1. public class Main
  2. {
  3. public static void main( String[] args )
  4. {
  5. ApplicationContext context =
  6. new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"});
  7. Customer customer = (Customer)context.getBean("CustomerBean");
  8. System.out.println(customer);
  9. }
  10. }

@Autowired默认情况下,总是认为field是可以成功注入的,如果没有成功注入(例如没有匹配的类型)则会跑出异常,如果要使得在没有成功注入的情况下不抛出异常,那么可以和required属性一起使用,将required的属性设置为false:

  1. public class Customer
  2. {
  3. **@Autowired(required=false)**
  4. private Person person;
  5. private int type;
  6. private String action;
  7. //getter and setter methods
  8. }

@Autowired默认是按照类型来匹配的,那么当容器中有两个相同类型的bean时怎样区分要注入是哪一个呢?这时候@Qualifier注解便起作用了:

  1. <bean id="person1" class="com.hzsunpeng.Person">
  2. <property name="name" value="name1"/>
  3. <property name="address" value="adderss1"/>
  4. <property name="age" value="23"/>
  5. </bean>
  6. <bean id="person2" class="com.hzsunpeng.Person">
  7. <property name="name" value="name2"/>
  8. <property name="address" value="adderss2"/>
  9. <property name="age" value="22"/>
  10. </bean>

这时候怎样决定要注入person1呢还是person2呢?

如果想注入person1,那么可以这样做:

  1. public class Customer
  2. {
  3. @Autowired
  4. @Qualifier("person1")
  5. private Person person;
  6. private int type;
  7. private String action;
  8. //setter and getter
  9. }

最后注意一个细节:如果使用了springMVC自动扫描组件(@Component或者是@Service等),在配置文件中加入了

  1. <context:component-scan base-package="***.***.***" />

那么配置文件中的

  1. <context:annotation-config/>

可以省略。

Spring@Autowired注解的更多相关文章

  1. Spring @Autowired 注解 学习资料

    Spring @Autowired 注解 学习资料 网址 Spring @Autowired 注解 https://wiki.jikexueyuan.com/project/spring/annota ...

  2. Spring @Autowired注解用在集合上面,可以保持接口的所有实现类

    CourseService课程接口有2个子类,HistroyCourseServiceImpl和MathsCourseServiceImpl public interface CourseServic ...

  3. Spring @Autowired 注解自动注入流程是怎么样?

    面试中碰到面试官问:"Spring 注解是如果工作的?",当前我一惊,完了这不触及到我的知识误区了吗?,还好我机智,灵机一动回了句:Spring 注解的工作流程倒还没有看到,但是我 ...

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

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

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

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

  6. Spring @Autowired注解在非Controller中注入为null

    问题描述 今天在写一个工具类,里面用了@Autowired注入了StringRedisTemplate以及RedisTemplate时,在template.opsForValue().set(key, ...

  7. Spring@Autowired注解与自动装配(转发)

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

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

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

  9. Spring @Autowired 注解不生效

    @Autowired默认不生效.为了生效,需要在xml配置:<context:annotation-config> 注解一<context:component-scan base-p ...

随机推荐

  1. Yarn执行流程

    在Yarn中,JobTracker被分为两部分:ResourceManager(RM)和ApplicationMaster(AM). MRv1主要由三部分组成:编程模型(API).数据处理引擎(Map ...

  2. Android的Fragment中onActivityResult不被调用

    1.检查该Fragment所属的Activity中,是否重写了onActivityResult方法. 2.检查Fragment中的startActivityForResult的调用方式. 请确保不要使 ...

  3. flask + mysql写的简单监控系统

    这里以监控内存使用率为例,写的一个简单demo性程序,具体操作根据51reboot提供的教程写如下. 一.建库建表 创建falcon数据库: mysql> create database fal ...

  4. ISD9160学习笔记05_ISD9160语音识别代码分析

    前言 语音识别是特别酷的功能,ISD9160的核心卖点就是这个语音识别,使用了Cybron VR 算法. 很好奇这颗10块钱以内的IC是如何实现人家百来块钱的方案.且听如下分析. 本文作者twowin ...

  5. 将百度编辑器ueditor用在easyui中

    又一个自己想深爱却一直被拖着的对象--百度编辑器(ueditor) 但终究逃不过再次把它"供奉"起来的宿命,这不今天又得好好研究一下它的使用方法,以免自己今后再次使用时的各种不便- ...

  6. MVC的初步认识理论

    说起来写博客可以说一个月没来啦,我们狠狠的放假一个月,想一想都奇怪.而是想一下以后的假期还会这样吗?或许这是作为学生的我们的最后一个长的假期啦,以后就要面对工作再也没有寒假暑假之分啦,在这一个月的时间 ...

  7. ORACLE常用函数汇总【转】

    PL/SQL单行函数和组函数详解 函数是一种有零个或多个参数并且有一个返回值的程序.在SQL中Oracle内建了一系列函数,这些函数都可被称为SQL或PL/SQL语句, 函数主要分为两大类: 单行函数 ...

  8. 《转载》Python并发编程之线程池/进程池--concurrent.futures模块

    本文转载自Python并发编程之线程池/进程池--concurrent.futures模块 一.关于concurrent.futures模块 Python标准库为我们提供了threading和mult ...

  9. 在MyEclipse(2015)中上传项目到github的步骤(很详细)

    (图文)在MyEclipse(2015)中上传项目到github的步骤(很详细) git|smartGit使用详解 SmartGit使用教程

  10. X64下的虚拟地址到物理地址的转换

    https://bbs.pediy.com/thread-203391.htm   早就知道传上来排版会全乱掉,把pdf直接传上来吧 x64结构体系寻址.pdf 发现安大的关于x86启用PAE下的虚拟 ...