@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注解的更多相关文章

  1. Spring @Autowired 注解 学习资料

    Spring @Autowired 注解 学习资料 网址 Spring @Autowired 注解 https://wiki.jikexueyuan.com/project/spring/annota ...

  2. Spring @Autowired注解用在集合上面,可以保持接口的所有实现类

    CourseService课程接口有2个子类,HistroyCourseServiceImpl和MathsCourseServiceImpl public interface CourseServic ...

  3. Spring @Autowired 注解自动注入流程是怎么样?

    面试中碰到面试官问:"Spring 注解是如果工作的?",当前我一惊,完了这不触及到我的知识误区了吗?,还好我机智,灵机一动回了句:Spring 注解的工作流程倒还没有看到,但是我 ...

  4. Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  5. 【转】Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  6. Spring @Autowired注解在非Controller中注入为null

    问题描述 今天在写一个工具类,里面用了@Autowired注入了StringRedisTemplate以及RedisTemplate时,在template.opsForValue().set(key, ...

  7. Spring@Autowired注解与自动装配(转发)

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  8. [转] Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  9. Spring @Autowired 注解不生效

    @Autowired默认不生效.为了生效,需要在xml配置:<context:annotation-config> 注解一<context:component-scan base-p ...

随机推荐

  1. 【web端权限维持】利用ADS隐藏webshell

    0X01 前言 未知攻,焉知防,在web端如何做手脚维护自己拿到的权限呢?首先要面临的是webshell查杀,那么通过利用ADS隐藏webshell,不失为一个好办法. 0X02 利用ADS隐藏web ...

  2. 使用librtmp进行H264与AAC直播

    libx264 版本是 128libfaac 版本是 1.28 1.帧的划分 1.1 H.264 帧 对于 H.264 而言每帧的界定符为 00 00 00 01 或者 00 00 01. 比如下面的 ...

  3. HttpClient(一)-- HelloWorld

    一.简介 HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的 ...

  4. MQTT服务器搭建--Mosquitto用户名密码配置

    Mosquitto用户认证配置 前言:基于Mosquitto服务器已经搭建成功,大部分都是采用默认的是允许匿名用户登录模式,正式上线的系统需要进行用户认证. 1.用户参数说明 Mosquitto服务器 ...

  5. php git pull

    http://jondavidjohn.com/git-pull-from-a-php-script-not-so-simple/

  6. 【Linux基础】Linux基础命令行学习笔记

    绝对路径:cd /home/python相对路径:cd Downloads . 表示:当前那路径..表示:当前路径的上一层../.. 表示:当前路径的上二层 没有...或者以上的 ls: ls 查看当 ...

  7. es6 - class的学习

    http://es6.ruanyifeng.com/#docs/class:class Person { constructor{ //构造函数,里边放不被继承的私有属性和方法 this.proper ...

  8. Linux SVN 服务器

    一. SVN 简介 Subversion(SVN) 是一个开源的版本控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库 (repository) 中 ...

  9. CentOS 安装PostregSQL9.2 同时出现pg安装错误

    错误: Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension. /usr/local/bin ...

  10. Java内存泄露监控工具:JVM监控工具介绍【转】

    jstack?-- 如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程 ...