@Autowired:自动装配,不用在bean里写<property>属性来指定所依赖的属性

1
2
3
4
@Autowired
public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
}

Autowired:表示spring会在容器里找跟setUserDao(UserDao userDao)这里面的参数相同类型的bean,然后给注入进来。

例如:原来用XML方式的写法

1
2
3
4
5
<bean id="userDaoImpl" class="com.fz.annotation.dao.impl.UserDaoImpl">
</bean>
<bean id="userService" class="com.fz.xml.service.UserService">
    <property name="userDao" ref="userDaoImpl"></property>
</bean>

这里我们需要写一个<property name="userDao" ref="userDaoImpl"></property>来给bean注入属性。使用了annotation之后呢,就可以不用这样写

了。直接去掉<property>属性

1
2
3
4
<bean id="userDaoImpl" class="com.fz.annotation.dao.impl.UserDaoImpl">
</bean>
<bean id="userService" class="com.fz.xml.service.UserService">  
</bean>

然后在userService里的userDao的set方法上加入@Autowired即可

现在测试:控制台打印出userDaoImpl里的User add.....信息,表示userDao已经获取到

1
2
3
4
5
6
@Test
public void getProperties(){
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext_annotation.xml");
    UserService userService = (UserService) ctx.getBean("userService");
    userService.userAdd();
}

通过测试,可以看出。@Autowired是根据set方法里的bean的类型来去容器里找相应类型的bean,也就是使用了byType

假如现在我们配置两个相同类型的userDaoImpl

1
2
3
4
5
6
<bean id="userDaoImpl" class="com.fz.annotation.dao.impl.UserDaoImpl">
</bean>
<bean id="userDaoImpl1" class="com.fz.annotation.dao.impl.UserDaoImpl">
</bean>
<bean id="userService" class="com.fz.annotation.service.UserService">
</bean>

此时测试的时候将会报错:expected single matching bean but found 2。

但是,假如实际开发中我们就需要两个类型相同的bean呢?可以加上@Qualifier("userDaoImpl")

1
2
3
4
@Autowired
public void setUserDao(@Qualifier("userDaoImpl")UserDao userDao) {
    this.userDao = userDao;
}

@Qualifier("userDaoImpl"):表示使用名字为userDaoImpl的bean来进行注入,现在测试的话同样没问题。

也可以写在Autowired之后

@Autowired

@Qualifier("userDaoImpl")

public void setUserDao(UserDao userDao)

总结:

1、@Autowired可以放在属性上,任意方法上,建议放在属性的set方法上。

2、@Autowired默认是按byType来查找bean的,如果找到多个同一类型的bean,则会报错

3、如果想按byName来查找bean,那就@Autowired和@Qualifier结合使用:@Qualifier("userDaoImpl"):表示使用名字为userDaoImpl的bean来进行注入


SpringAnnotation注解之@Autowired的更多相关文章

  1. 注解:@Autowired

    Spring的bean对象自动装配注解,@Autowired有三种使用方式,下面分别介绍! 1. 作用于类成员变量上,即在定义类的成员变量的时候,至于其上方. public class Mobile{ ...

  2. Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解

    本文介绍了使用Spring注解注入属性的方法.使用注解以前,注入属性通过类以及配置文件来实现.现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifier,@Pos ...

  3. Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的

    本文介绍了使用spring注解注入属性的方法. 使用注解以前,注入属性通过类以及配置文件来实现.现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifier,@Po ...

  4. 从头认识Spring-2.3 注解装配-@autowired(3)-通过构造器方法注入

    这一章节我们来讨论一下注解装配的@autowired是如何通过set方法或者其它方法注入? 1.domain 蛋糕类:(不变) package com.raylee.my_new_spring.my_ ...

  5. Java 各级注解及@Autowired注入为null解决办法

    1.@controller 控制器 用于标注控制层,相当于struts中的action层. 2.@service 服务层 用于标注服务层,主要用来进行业务的逻辑处理. 3.@repository DA ...

  6. Spring注解之@Autowired

    前言 说起Spring的@Autowired注解,想必大家已经熟悉的不能再熟悉了.本文就针对此最常用的注解,梳理一下它的功能和原理,争取从源码的角度将此注解讲通,如有写的不准确的地方,欢迎各位园友拍砖 ...

  7. Spring注解之@Autowired、@Qualifier、@Resource、@Value

    前言 @Autowired.@Qualifier.@Resource.@Value四个注解都是用于注入数据的,他们的作用就和在xml配置文件中的bean标签中写一个标签的作用是一样的!本篇中特别要讲解 ...

  8. @Resource注解和@Autowired注解

    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11770982.html 1. @Resource 类来源: javax(Java扩展包) 类全 ...

  9. spring 注解@Resource @Autowired区别

    1.@Autowired寻找类的时候默认是ByType,也就是通过类的类型来寻找类.不过,也可以通过借助@Qualifier("name")来指定寻找的类名 @Autowired ...

随机推荐

  1. 获取 config文件的节点值

    System.Configuration.ConfigurationManager.AppSettings["followTemplate"];

  2. Linux CentOS6环境下MySQL5.1升级至MySQL5.5版本过程

    转载地址:http://www.laozuo.org/6145.html 老左今天有在帮朋友的博客搬迁到另外一台VPS主机环境,其环境采用的是LLSMP架构的,原先的服务器采用的是LNMP网站环境,其 ...

  3. yum安装redis phpredis扩展

    转载地址:http://blog.csdn.net/musicrabbit/article/details/9729941 redis和php-redis在官方源上是没有的,需要安装其他的源,其他源的 ...

  4. 阿里云 Linux 启用465端口发送邮件

    阿里云 Linux 启用465端口发送邮件 环境:阿里云 Linux Centos 7.4 x64 注:阿里云默认禁用25邮件端口,需要启动465端口加密进行邮件发送. 注:确保邮箱开启SMTP服务, ...

  5. Eclipse FindBugs插件

    在线安装: Update Site:http://findbugs.cs.umd.edu/eclipse 本地安装: 1.首先从findbugs网站下载插件:http://findbugs.sourc ...

  6. mysql类似递归的一种操作进行层级查询

    select device_id,device_type,COUNT(1) count from ( select t1.device_id,t1.device_type,DATE_SUB(t1.re ...

  7. C#代码实现 Excel表格与Object互相转换,Excel表格导入数据库(.NET2.0 .NET4.0)

    前些天在工作上遇到这个需求,在GitHub找到一个开源代码可以用,Fork了一个版本,整理一下发出来. ①.Net项目中使用Nuget安装一个 NPOI 包    https://github.com ...

  8. word2vector 使用方法 计算语义相似度

    参考:http://techblog.youdao.com/?p=915#LinkTarget_699word2vector是一个把词转换成词向量的一个程序,能够把词映射到K维向量空间,甚至词与词之间 ...

  9. 编译libmemcached

    php的扩展memcache,不支持cas,所以我们要装memcached扩展,memcached扩展是基于libmemcached,所以要先安装libmemcached 一.下载软件 1.libme ...

  10. Hystrix的正确理解方式

    hystrix-logo-tagline-640.png 什么是熔断器 熔断器,原本是电路中在电器发生短路时的防止电路过载的开关装置,它切断发生短路的电路,从而防止因电路过载导致的发热起火等灾难的发生 ...