• 可以将@Autowired注解为“传统”的setter方法
package com.mypackage;

import org.springframework.beans.factory.annotation.Autowired;

public class SimpleMovieLister {
private MovieFinder movieFinder; @Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
} //.....
}
  • 可用于构造器或成员变量
package com.mypackage;

import org.springframework.beans.factory.annotation.Autowired;

public class SimpleMovieLister {

	@Autowired
private BeanAnnotation beanAnnotation; private MovieFinder movieFinder; @Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
} //.....
}
  • 默认情况下,如果因找不到合适的bean将会导致autowiring失败抛出异常,可以通过下面的方式避免
package com.mypackage;

import org.springframework.beans.factory.annotation.Autowired;

public class SimpleMovieLister {
private MovieFinder movieFinder; @Autowired(required=false)
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
} //.....
}
  • 每个类只能有一个构造器被标记为required=true
  • @Autowired的必要属性,建议会用@Required注解

例子:

先新建两个接口(InjectionService、InjectionDAO),及实现类

package com.mypackage;

public interface InjectionDAO {

	public void save(String args);
}
package com.mypackage;

import org.springframework.stereotype.Repository;

@Repository
public class InjectionDAOImpl implements InjectionDAO { public void save(String args) {
//模拟数据库操作
System.out.println("DAO保存数据:" + args);
} }
package com.mypackage;

public interface InjectionService {

	public void save(String args);
}
package com.mypackage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class InjectionServiceImpl implements InjectionService{ @Autowired
private InjectionDAO injectionDAO; public void save(String s){
//模拟业务操作
System.out.println("Service接收参数:"+s);
s=s+":"+this.hashCode();
injectionDAO.save(s);
}
}

XML配置:

<?xml version="1.0" encoding="UTF-8"?>
<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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="com.mypackage">
</context:component-scan>
</beans>

单元测试:

package com.mypackage;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class UnitTest { @Test
public void testAutoWired(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beanannotation.xml");
InjectionService service=(InjectionService)context.getBean("injectionServiceImpl");
service.save("this is autowired");; }
}

结果:

2015-7-6 15:52:26 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1342a80d: startup date [Mon Jul 06 15:52:26 CST 2015]; root of context hierarchy
2015-7-6 15:52:26 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-beanannotation.xml]
Service接收参数:this is autowired
DAO保存数据:this is autowired:1811560891

@Autowired也可应用于构造器

修改InjectionServiceImpl实现类(添加setter)

package com.mypackage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class InjectionServiceImpl implements InjectionService{ private InjectionDAO injectionDAO; @Autowired
public void setInjectionDAO(InjectionDAO injectionDAO) {
this.injectionDAO = injectionDAO;
} public void save(String s){
//模拟业务操作
System.out.println("Service接收参数:"+s);
s=s+":"+this.hashCode();
injectionDAO.save(s);
}
}

测试结果:

2015-7-6 15:55:23 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@939b78e: startup date [Mon Jul 06 15:55:23 CST 2015]; root of context hierarchy
2015-7-6 15:55:23 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-beanannotation.xml]
Service接收参数:this is autowired
DAO保存数据:this is autowired:1860295362

Spring学习(8)--- @Autowired注解(一)的更多相关文章

  1. spring boot 中@Autowired注解无法自动注入的错误

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...

  2. 如何实现一个简易版的 Spring - 如何实现 @Autowired 注解

    前言 本文是 如何实现一个简易版的 Spring 系列第四篇,在 上篇 介绍了 @Component 注解的实现,这篇再来看看在使用 Spring 框架开发中常用的 @Autowired 注入要如何实 ...

  3. Spring学习笔记--使用注解装配

    使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...

  4. Spring学习之-各注解的含义总结

    注解配置 @ComponentScan("spittr.web"):/在加载Spring上下文时,会扫描spittr.web包查找组件 @ComponentScan注解扫描的组件有 ...

  5. spring @Resource与@Autowired注解详解

    具有依赖关系的Bean对象,利用下面任意一种注解都可以实现关系注入: 1)@Resource (默认首先按名称匹配注入,然后类型匹配注入) 2)@Autowired/@Qualifier (默认按类型 ...

  6. Spring学习之事务注解@Transactional

    今天学习spring中的事务注解,在学习Spring注解事务之前需要明白一些事务的基本概念: 事务:并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通 ...

  7. Spring中的Autowired注解和Resource注解的区别

    1.所属jar包不同,Autowired是Spring中的Resource是JSR-250规范定义的注解

  8. spring学习笔记二 注解及AOP

    本节需要导入spring-aop包 注解 使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值. 扫描某个包下的所有类中的注解 <?xml version="1. ...

  9. Spring学习之路-注解

    Spring的注解总结. 地址:https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsin ...

  10. Spring学习之常用注解(转)

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...

随机推荐

  1. 开发Angular库的简单指导(译)

    1. 最近工作上用到Angular,需要查阅一些英文资料,虽然英文非常烂,但是种种原因又不得不硬着头皮上,只是每次看英文都很费力,因此决定将一些比较重要的特别是需要反复阅读的资料翻译一下,以节约再次阅 ...

  2. Arraylist动态扩容详解

    ArrayList 概述 ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长. ArrayList不是线程安全的,只能用在单线程环境下. 实现了Serializable接口,因此它支 ...

  3. IT行业能力细分

    在软件行业工作7年了,平时很懒,懒得做分享,今天特意分享一下软件行业,职业大的技术分类,同学们可根据自己职业规划补充学习知识块.

  4. AOJ/初等排序习题集

    ALDS1_1_D-MaximumProfit. Codes: //#define LOCAL #include <cstdio> #include <algorithm> u ...

  5. 针对Mac的DuckHunter攻击演示

    0x00 HID 攻击 HID是Human Interface Device的缩写,也就是人机交互设备,说通俗一点,HID设备一般指的是键盘.鼠标等等这一类用于为计算机提供数据输入的设备. DuckH ...

  6. 如何解决 chrome 58 版本更新导致的 fiddler https 抓包不可用问题

    注意!如果该方法不生效,请先卸载原有 fiddler 后再进行新版本 fiddler 安装步骤即可. chrome 于(上周?上上周?)推送了chrome 58 版本的更新,这次更新中直接去掉了证书未 ...

  7. CKEditor不借助CKFinder实现图片上传(.net版 ashx实现)

    参考博客:http://blog.csdn.net/mydwr/article/details/8669594 本人版本:4.4.6 打开文件:ckeditor/plugins/image/dialo ...

  8. JavaScript中return的用法和this的用法详解

    JavaScript中return的用法详解 最近,跟身边学前端的朋友了解,有很多人对this和函数中的return的用法和意思理解的比较模糊,这里写一篇博客跟大家一起探讨一下return和this的 ...

  9. 简单jQuery 实现手风琴菜单

    简单jQuery 实现手风琴菜单 css代码 如下: *{ margin: 0; padding: 0; } #accordion{ width: 500px; } #accordion>div ...

  10. [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...