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 ...
随机推荐
- linux下kill -9 pid 强制不能杀掉进程原因
今天安装集群的时候,发现一个进程一直存在,kill -9 pid 也干不掉,就找找原因了. kill -9发送SIGKILL信号将其终止,但是以下两种情况不起作用:a.该进程处于"Zomb ...
- 执行RF测试只生成output.xml文件,不生成log和report文件
命令格式: -l --log file HTML log file. Can be disabled by giving a special value `NONE`. Default: log.ht ...
- centos系统-java -jdk 环境配置
方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创建java目录 [root@localhost ~]# mkdir/usr/java[root@localhost ~]# c ...
- 【LeetCode OJ】Search Insert Position
题目:Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 向Windows内核驱动传递用户层定义的事件Event,并响应内核层的通知
完整的程序在下载:http://download.csdn.net/detail/dijkstar/7913249 用户层创建的事件Event是一个Handle句柄,和内核中的创建的内核模式下的KEV ...
- Objective-c官方文档 怎么使用对象
版权声明:原创作品,谢绝转载!否则将追究法律责任. 对象发送和接受消息 尽管有不同的方法来发送消息在对象之间,到目前位置是想中括号那样[obj doSomeThing]:左边是接受消息的接收器,右 ...
- 三个 CSS 预处理器(框架):Sass、LESS 和 Stylus
CSS 预处理器技术已经非常的成熟,而且也涌现出了越来越多的 CSS 的预处理器框架.本文向你介绍使用最为普遍的三款 CSS 预处理器框架,分别是 Sass.Less CSS.Stylus. 首先我们 ...
- Makefile 链接静态库
Linux的静态库是以.a结尾的,要连接静态库有两种方法,一种是在编译命令最后直接加上库路径/库名称. 例如你的库在绝对目录/lib/libtest.a下面你就可以这样来编译$(CC) $(CFLAG ...
- 【Ubuntu】更新系统时出现Hash校验和不符的错误(已解决)
在使用 sudo apt-get update && sudo apt-get upgrade 命令更新系统时出现类似这样的错误信息: W: 无法下载 bzip2:/var/lib/a ...
- 如何修改 Ubuntu 的字符集?
步骤: 1.编辑 local 文件 输入: vi /var/lib/locales/supported.d/local 将 zh_CN.GB2312 加入到后面,保存. 2.执行:locale-gen ...