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 ...
随机推荐
- GSAP JS基础教程--TweenLite操作元素的相关属性
今天来学习用TweenLite操作元素的各种属性,以Div为例,其他元素的操作也是一样的,只是可能一些元素有它们的特殊属性,就可能不同罢了. 代码里用详细注释,我就不再重复啦,大家看代码就可以啦! ...
- MongoDB(四)-- 主从配置
一.前言 虽然MongoDB官方已经不建议使用主从模式了,但是 熟悉下 也是有用的,替代方案是采用副本集的模式.slave默认情况下是不支持读写的,但是master会把数据同步到slave,不支持客户 ...
- mybatis 之 parameterType="String" resultType="java.util.HashMap">
public ServiceMessage<Map<String, String>> getGoodsStockNo( List<Map<String, Strin ...
- 《转》Robot Framework 的安装配置和简单的实例介绍
Robot Framework 介绍 Robot Framework 是一款基于 Python 的功能自动化测试框架.它具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进 ...
- 浅谈千万级PV/IP规模高性能高并发网站架构
高并发访问的核心原则其实就一句话“把所有的用户访问请求都尽量往前推”. 如果把来访用户比作来犯的"敌人",我们一定要把他们挡在800里地以外,即不能让他们的请求一下打到我们的指挥部 ...
- Android Studio 无法预览xml布局视图的解决办法
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/lvyoujt/article/details/73283762 提示:failed to load ...
- Clojure 学习入门(14)—— 循环控制
Clojure 基于函数的流程控制 repeatedly 字面意思为重复函数.一般的用法如下: #(rand-int 11)) 8 2 6 6) 重复产生5次随机数.rand-int 11表示0至 ...
- Delphi 中DataSnap技术网摘
Delphi2010中DataSnap技术网摘 一.为DataSnap系统服务程序添加描述 这几天一直在研究Delphi 2010的DataSnap,感觉功能真是很强大,现在足有理由证明Delphi7 ...
- Delphi之Code Explorer
Code Explorer(代码浏览器)是Delphi IDE的特性之一,它大受用户的欢迎.正如其名所表示,Code Explorer用于快速浏览源代码单元.Code Explorer通常位于Code ...
- java基础---->数组的基础使用(二)
这里对List(jdk 1.7)列表里面的一些方法做一些简单的分析,以避免有些函数的误用.手写瑶笺被雨淋,模糊点画费探寻,纵然灭却书中字,难灭情人一片心. List中注意的方法 一.Arrays.as ...