Spring注解配置(1)——@Autowired
@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。在使用@Autowired之前,我们对一个bean配置起属性时,是这样用的
- <property name="属性名" value=" 属性值"/>
通过这种方式来,配置比较繁琐,而且代码比较多。在Spring 2.5 引入了 @Autowired 注释
下面用案例来具体说明
UserRepository.java
- 1 package com.proc.bean.repository;
- 2
- 3 public interface UserRepository {
- 4
- 5 void save();
- 6 }
这里定义了一个UserRepository接口,其中定义了一个save方法
UserRepositoryImps.java

- 1 package com.proc.bean.repository;
- 2
- 3 import org.springframework.stereotype.Repository;
- 4
- 5 @Repository("userRepository")
- 6 public class UserRepositoryImps implements UserRepository{
- 7
- 8 @Override
- 9 public void save() {
- 10 System.out.println("UserRepositoryImps save");
- 11 }
- 12 }

定义一个UserRepository接口的实现类,并实现save方法,在这里指定了该bean在IoC中标识符名称为userRepository
UserService.java

- 1 package com.proc.bean.service;
- 2
- 3 import org.springframework.beans.factory.annotation.Autowired;
- 4 import org.springframework.stereotype.Service;
- 5
- 6 import com.proc.bean.repository.UserRepository;
- 7
- 8 @Service
- 9 public class UserService {
- 10
- 11 @Autowired
- 12 private UserRepository userRepository;
- 13
- 14 public void save(){
- 15 userRepository.save();
- 16 }
- 17 }

这里需要一个UserRepository类型的属性,通过@Autowired自动装配方式,从IoC容器中去查找到,并返回给该属性
applicationContext.xml配置
- <context:component-scan base-package="com.proc.bean" />
测试代码:
- 1 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
- 2
- 3 UserService userService=(UserService) ctx.getBean("userService");
- 4 userService.save();
输出结果:UserRepositoryImps save
那么使用@Autowired的原理是什么?
其实在启动spring IoC时,容器自动装载了一个AutowiredAnnotationBeanPostProcessor后置处理器,当容器扫描到@Autowied、@Resource或@Inject时,就会在IoC容器自动查找需要的bean,并装配给该对象的属性
- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
注意事项:
在使用@Autowired时,首先在容器中查询对应类型的bean
如果查询结果刚好为一个,就将该bean装配给@Autowired指定的数据
如果查询的结果不止一个,那么@Autowired会根据名称来查找。
如果查询的结果为空,那么会抛出异常。解决方法时,使用required=false
举例说明:
在上面例子中,我们在定一个类来实现UserRepository接口

- package com.proc.bean.repository;
- import org.springframework.stereotype.Repository;
- @Repository
- public class UserJdbcImps implements UserRepository {
- @Override
- public void save() {
- System.out.println("UserJdbcImps save");
- }
- }

这时在启动容器后,在容器中有两个UserRepository类型的实例,一个名称为userRepository,另一个为userJdbcImps。在UserService中
- @Autowired
- private UserRepository userRepository;
输出结果:UserRepositoryImps save
这里由于查询到有两个该类型的实例,那么采用名称匹配方式,在容器中查找名称为userRepository的实例,并自动装配给该参数。
如果这里想要装载userJdbcImps的实例,除了将字段userRepository名称改成userJdbcImps外,可以提供了一个@Qualifier标记,来指定需要装配bean的名称,代码这样写
- 1 @Autowired
- 2 @Qualifier("userJdbcImps")
- 3 private UserRepository userRepository;
输出结果:UserJdbcImps save
Spring注解配置(1)——@Autowired的更多相关文章
- 关于Spring注解配置的步骤
今天分享一下 关于Spring注解配置的流程 1 导包:如下图所示 2 书写User和Car类 代码如下 package cn.lijun.bean; public class Car { priv ...
- Spring注解配置和xml配置优缺点比较
Spring注解配置和xml配置优缺点比较 编辑 在昨天发布的文章<spring boot基于注解方式配置datasource>一文中凯哥简单的对xml配置和注解配置进行了比较.然后朋 ...
- 【Spring实战】Spring注解配置工作原理源码解析
一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...
- Spring注解配置Aop
之前学习了SpringAop的基本原理.http://www.cnblogs.com/expiator/p/7977975.html 现在尝试使用注解来配置SpringAop. Aop,面向切面编程. ...
- 5.spring:注解配置 Bean
在classpath中扫描组件 组键扫描:能够从classpath下自动扫描,侦测和实例化具有特定注解的组件 特定的组件包括: ->@Componment:基于注解,标识一个受Spring管理的 ...
- 【转】【Spring实战】Spring注解配置工作原理源码解析
一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...
- Spring 注解配置Bean
一.使用注解配置Bean 1.注解 在类定义.方法定义.成员变量定义前使用.其简化<bean>标签,功能同<bean>标签.格式为: @注解标记名. 2.组件扫描 Spring ...
- spring注解配置实例
在spring中使用注解配置前需要先在配置文件指定需要扫描的包. 通过注解的方式依赖注入,可以不用创建set方法,也不用在xml文件中申明注入关系. 实例结构如下: 整个流程是: 先创建好数据库的表对 ...
- spring注解配置启动过程
最近看起spring源码,突然想知道没有web.xml的配置,spring是怎么通过一个继承于AbstractAnnotationConfigDispatcherServletInitializer的 ...
随机推荐
- Codeforces Round #411 A. Fake NP
A. Fake NP time limit per test 1 second memory limit per test 256 megabytes Tavak and Seyyed a ...
- 139.00.007 Git学习-Cheat Sheet
@(139 - Environment Settings | 环境配置) Git虽然极其强大,命令繁多,但常用的就那么十来个,掌握好这十几个常用命令,你已经可以得心应手地使用Git了. 友情附赠国外网 ...
- 十五、css3 Filter--滤镜
如何实现下图的效果-—这里就用到了滤镜 给灰色弹框这个标签元素加“伪类”如下: #nearStoreContent .popChoose li:before { 1. z-index:; 2. pos ...
- [翻译] Macros with a Variable Number of Arguments - GCC
可变参数宏(Variadic Macro) 在1999年的ISO C标准中,可以声明一个像函数一样接受可变参数的宏.定义这种宏的语法与函数的定义相似.这是一个例子: #define debug(for ...
- linux 软连接和硬链接示意图
创建软连接 ln -s 1.txt 1-softlink.txt 创建硬链接 ln 1.txt 1-hardlink.txt
- incast.tcl
# Basic Incast Simulation # Check Args if {$argc != 5} { puts "Usage: ns incast <srv_num> ...
- UnicodeDecodeError: 'utf8' codec can't decode byte in position invalid start byte
在scrapy项目中,由于编码问题,下载的网页中中文都是utf-8编码,在Pipeline.py中方法process_item将结果保存到数据库中时,提示UnicodeDecodeError: 'ut ...
- 【Leetcode】【Easy】Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 浅析tnsping
首先,先弄清楚tnsping是什么: Oracle Net 工具(命令)tnsping,是一个OSI会话层的工具,测试数据库服务的命令,用来决定是否一个Oracle Net 网络服务(service) ...
- Oracle三种链接方式的区别
1 nested loops join--我们用设置statistics_level=all的方式来观察如下表连接语句的执行计划: --T2表被访问100次(驱动表访问1次,被驱动表访问100次)-- ...