Spring@Autowired注解
@Autowired注解可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。
注意:@Autowired默认是按照类型来注入的。
看下面的例子:例子是以对成员变量(field)为例进行的
public class Person {
private String name;
private String address;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
return "[name:"+getName()+
",address:" + getAddress()+
",age:" +getAge()+
"]";
}
另外一个Customer类有一个Person类型的成员,现用@Autowired注入:
public class Customer
{
@Autowired
private Person person;
private int type;
private String action;
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String toString() {
return "[Type:"+getType()+
",action:"+getAction()+
","+"person:"+
person.toString();
}
}
在配置文件中加入
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--@Autowired-->
<context:annotation-config/>
<bean id="CustomerBean" class="com.mkyong.common.Customer">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>
<bean id="PersonBean" class="com.mkyong.common.Person">
<property name="name" value="mkyong" />
<property name="address" value="address ABC" />
<property name="age" value="29" />
</bean>
</beans>
测试程序:
public class Main
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"});
Customer customer = (Customer)context.getBean("CustomerBean");
System.out.println(customer);
}
}
@Autowired默认情况下,总是认为field是可以成功注入的,如果没有成功注入(例如没有匹配的类型)则会跑出异常,如果要使得在没有成功注入的情况下不抛出异常,那么可以和required属性一起使用,将required的属性设置为false:
public class Customer
{
**@Autowired(required=false)**
private Person person;
private int type;
private String action;
//getter and setter methods
}
@Autowired默认是按照类型来匹配的,那么当容器中有两个相同类型的bean时怎样区分要注入是哪一个呢?这时候@Qualifier注解便起作用了:
<bean id="person1" class="com.hzsunpeng.Person">
<property name="name" value="name1"/>
<property name="address" value="adderss1"/>
<property name="age" value="23"/>
</bean>
<bean id="person2" class="com.hzsunpeng.Person">
<property name="name" value="name2"/>
<property name="address" value="adderss2"/>
<property name="age" value="22"/>
</bean>
这时候怎样决定要注入person1呢还是person2呢?
如果想注入person1,那么可以这样做:
public class Customer
{
@Autowired
@Qualifier("person1")
private Person person;
private int type;
private String action;
//setter and getter
}
最后注意一个细节:如果使用了springMVC自动扫描组件(@Component或者是@Service等),在配置文件中加入了
<context:component-scan base-package="***.***.***" />
那么配置文件中的
<context:annotation-config/>
可以省略。
Spring@Autowired注解的更多相关文章
- Spring @Autowired 注解 学习资料
Spring @Autowired 注解 学习资料 网址 Spring @Autowired 注解 https://wiki.jikexueyuan.com/project/spring/annota ...
- Spring @Autowired注解用在集合上面,可以保持接口的所有实现类
CourseService课程接口有2个子类,HistroyCourseServiceImpl和MathsCourseServiceImpl public interface CourseServic ...
- Spring @Autowired 注解自动注入流程是怎么样?
面试中碰到面试官问:"Spring 注解是如果工作的?",当前我一惊,完了这不触及到我的知识误区了吗?,还好我机智,灵机一动回了句:Spring 注解的工作流程倒还没有看到,但是我 ...
- Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- 【转】Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- Spring @Autowired注解在非Controller中注入为null
问题描述 今天在写一个工具类,里面用了@Autowired注入了StringRedisTemplate以及RedisTemplate时,在template.opsForValue().set(key, ...
- Spring@Autowired注解与自动装配(转发)
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- [转] Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- Spring @Autowired 注解不生效
@Autowired默认不生效.为了生效,需要在xml配置:<context:annotation-config> 注解一<context:component-scan base-p ...
随机推荐
- Yarn执行流程
在Yarn中,JobTracker被分为两部分:ResourceManager(RM)和ApplicationMaster(AM). MRv1主要由三部分组成:编程模型(API).数据处理引擎(Map ...
- Android的Fragment中onActivityResult不被调用
1.检查该Fragment所属的Activity中,是否重写了onActivityResult方法. 2.检查Fragment中的startActivityForResult的调用方式. 请确保不要使 ...
- flask + mysql写的简单监控系统
这里以监控内存使用率为例,写的一个简单demo性程序,具体操作根据51reboot提供的教程写如下. 一.建库建表 创建falcon数据库: mysql> create database fal ...
- ISD9160学习笔记05_ISD9160语音识别代码分析
前言 语音识别是特别酷的功能,ISD9160的核心卖点就是这个语音识别,使用了Cybron VR 算法. 很好奇这颗10块钱以内的IC是如何实现人家百来块钱的方案.且听如下分析. 本文作者twowin ...
- 将百度编辑器ueditor用在easyui中
又一个自己想深爱却一直被拖着的对象--百度编辑器(ueditor) 但终究逃不过再次把它"供奉"起来的宿命,这不今天又得好好研究一下它的使用方法,以免自己今后再次使用时的各种不便- ...
- MVC的初步认识理论
说起来写博客可以说一个月没来啦,我们狠狠的放假一个月,想一想都奇怪.而是想一下以后的假期还会这样吗?或许这是作为学生的我们的最后一个长的假期啦,以后就要面对工作再也没有寒假暑假之分啦,在这一个月的时间 ...
- ORACLE常用函数汇总【转】
PL/SQL单行函数和组函数详解 函数是一种有零个或多个参数并且有一个返回值的程序.在SQL中Oracle内建了一系列函数,这些函数都可被称为SQL或PL/SQL语句, 函数主要分为两大类: 单行函数 ...
- 《转载》Python并发编程之线程池/进程池--concurrent.futures模块
本文转载自Python并发编程之线程池/进程池--concurrent.futures模块 一.关于concurrent.futures模块 Python标准库为我们提供了threading和mult ...
- 在MyEclipse(2015)中上传项目到github的步骤(很详细)
(图文)在MyEclipse(2015)中上传项目到github的步骤(很详细) git|smartGit使用详解 SmartGit使用教程
- X64下的虚拟地址到物理地址的转换
https://bbs.pediy.com/thread-203391.htm 早就知道传上来排版会全乱掉,把pdf直接传上来吧 x64结构体系寻址.pdf 发现安大的关于x86启用PAE下的虚拟 ...