• 可以将@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. 闭包(匿名函数) php

    php中的闭包,之前不理解.以前项目中虽然有用到,也是别人怎么用,自己也跟着怎么用,也没具体去看一下,时间长了就忘了,也不知道闭包是怎么回事.今天网上搜集了关于php闭包相关的文章,看了7,8篇,干货 ...

  2. JS设计模式---缓存代理

    缓存代理可以为一些开销大的运算结果提供暂时的存储,在下次运算的时候,传进来的参数跟上次是一致, 则可以直接返回前面存储的结果. 运行上面的代码我们发现,当第二次再调用proxyMult(1,2,3)的 ...

  3. 重启mysql提示:The server quit without updating PID file问题的解决办法

    今天因为需要开启事件调度器event_scheduler,所以修改了mysql的配置文件/etc/my.cnf 就因为配置多了个分号,导致一直启动失败,如下图所示: 然后去网上搜了帖子(MySQL提示 ...

  4. VopSdk一个高逼格微信公众号开发SDK(源码下载)

    看之前回复很多说明大家很有热情&文章被误删掉了,不想让有的朋友错失这个高逼格的东西,现在重新发布,这次就直接放出源码,文章最末下载地址. 看之前回复很多说明大家很有热情&文章被误删掉了 ...

  5. AngularJS的相关应用

    一.[AngularJS常用指令]        1.ng-app:声明Angular所管辖的区域.一般写在body或html上,原则上一个页面只有一个:           <body ng- ...

  6. Brief introduction to Java String Split 【简单介绍下Java String Split】

    Split is a common function in Java. It split a full string to an array based on delimeter. For examp ...

  7. TP5.0 PHPExcel 数据表格导出(原)

    今天看的是PHPExcel这个扩展库,Comporse 下载不下来,最后只能自己去github里面手动下载,但有一个问题就是下载下来的PHPExcel没有命名空间,所以框架里面的use根本引入不进去, ...

  8. Java中File

    1.什么是流? Java中的流是个抽象的概念,当程序需要从某个数据源读入数据的时候,就会开启一个数据流,数据源可以是文件.内存或网络等等.2.使用File类操作文件或目录属性 public class ...

  9. UI-UIwindow

    1.什么是UI? UI  (User Interface) : 用户界面,用户看到的各种各样的页面元素: 2.什么是UIWindow ? UIWindow : 一个基础容器,往窗口上放不同的东西,每个 ...

  10. JavaScript之通用addLoadEvent代码源码

    在执行javascript代码时 很多情况下 我们是希望代码在网页加载完毕后立刻进行的 大家可能会立刻想到使用window.onload时间处理函数,然后通过 window.onload=functi ...