• 可以使用@Autowired注解那些众所周知的解析依赖性接口,比如:BeanFactory,ApplicationContext,Environment,ResourceLoader,ApplicationEventPublisher,MessageSource
package com.mypackage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; public class MovieRecommender { @Autowired
private ApplicationContext context; public MovieRecommender(){
} //...
}
  • 可以通过添加注解给需要该类型的数组的字段和方法,以提供ApplicationContext中的所有特定类型的bean
private Set<MovieCatalog> movieCatalogs;

@Autowired
public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
  • 可以用于装配key为String的Map
private Map<String , MovieCatalog>  movieCatalog;

@Autowired
public void setMovieCatalog(Map<String, MovieCatalog> movieCatalog) {
this.movieCatalog = movieCatalog;
}
  • 如果希望数组有序,可以让bean实现org.springframework.core.Ordered接口或使用的@Order注解
  • @Autowired是由Spring BeanPostProcessor处理的,所以不能在自己的BeanPostProcessor或BeanFactoryPostProcessor类型应用这些注解,这些类型必须通过XML或者Spring的@Bean注解加载

数组及Map的自动注入——例子:

先定义一个BeanInterface接口

package com.multibean;

public interface BeanInterface {

}

在定义两个实现类

package com.multibean;

import org.springframework.stereotype.Component;

@Component
public class BeanInterfaceImpl implements BeanInterface { }
package com.multibean;

import org.springframework.stereotype.Component;

@Component
public class BeanInterface2Impl implements BeanInterface{ }

定义BeanInvoker实现数组和Map

package com.multibean;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class BeanInvoker { @Autowired
private List<BeanInterface> list; @Autowired
private Map<String,BeanInterface> map; public void say(){
if(null != list){
System.out.println("list...");
for(BeanInterface bean:list){
System.out.println(bean.getClass().getName());
}
}else{
System.out.println("List<BeanInterface> list is null.");
} if(null != map && 0 != map.size()){
System.out.println("map...");
for(Map.Entry<String,BeanInterface> entry:map.entrySet()){
System.out.println(entry.getKey()+"-----"+entry.getValue().getClass().getName());
}
}else{
System.out.println("Map<String,BeanInterface> map is null.");
}
} }

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.multibean">
</context:component-scan>
</beans>

单元测试:

package com.multibean;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class UnitTest { @Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beansnnotation.xml");
BeanInvoker beanInvoker = (BeanInvoker)context.getBean("beanInvoker");
beanInvoker.say();
}
}

结果:

七月 06, 2015 10:46:41 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@32482417: startup date [Mon Jul 06 22:46:41 CST 2015]; root of context hierarchy
七月 06, 2015 10:46:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
list...
com.multibean.BeanInterface2Impl
com.multibean.BeanInterfaceImpl
map...
beanInterface2Impl-----com.multibean.BeanInterface2Impl
beanInterfaceImpl-----com.multibean.BeanInterfaceImpl

@Order注解----例子:

改造一下两个实现类,加上@Order注解

package com.multibean;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Order(value = 1)
@Component
public class BeanInterfaceImpl implements BeanInterface { }
package com.multibean;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Order(value = 2)
@Component
public class BeanInterface2Impl implements BeanInterface{ }

测试同上

结果:

七月 06, 2015 10:58:58 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e397bcb: startup date [Mon Jul 06 22:58:58 CST 2015]; root of context hierarchy
七月 06, 2015 10:58:58 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
list...
com.multibean.BeanInterfaceImpl
com.multibean.BeanInterface2Impl
map...
beanInterface2Impl-----com.multibean.BeanInterface2Impl
beanInterfaceImpl-----com.multibean.BeanInterfaceImpl

PS:@Order只针对数组,对于map无效

Spring学习(9)--- @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学习 Ioc篇(二 )

    5.spring依赖注入的方式 方法一:使用构造器方式进行注入 1.dao的类和接口 package com.cvicse.dao.impl; import com.cvicse.dao.Person ...

  9. Spring学习之路-注解

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

随机推荐

  1. Linux-进程描述(4)之进程优先级与进程创建执行

    进程优先级 进程cpu资源分配就是指进程的优先权(priority).优先权高的进程有优先执行权利. 权限与优先级.权限(privilege)是指在多用户计算机系统的管理中,某个特定的用户具有特定的系 ...

  2. C专家编程阅读笔记

    周末闲来无事,(哗),好久之前买的C专家编程一直没看,翻起来看了一下 尽量不使用unsigned 尽量不要在代码中使用unsigned,尤其是一些看起来是无符号类型的数字,比如年龄等,因为难免要使用u ...

  3. virtualbox中实施ASM扩容实施记录

    实施ASM在线扩容 1. 首先关闭集群 [root@node1 bin]# ./crsctl stop crs [root@node2 bin]# ./crsctl stop crs 关机,halt ...

  4. ArcGIS 网络分析[2] 利用自定义基础数据创建网络数据集

    前言 似乎除了官方介绍的例子,我还没有在网上见过一篇介绍如何"使用自己的数据"创建"网络数据集"的文章. 有介绍几何网络的,有介绍如何用官方SanFrancis ...

  5. JDK的下载与配置

    一.下载JDK 1.进入oracle官网 下载完成后直接运行就能生成jdk文件. 二.配置环境 1.打开环境配置 右击计算机选择属性 选择高级系统数据 点击环境变量 2.配置JAVA_HOME 新建一 ...

  6. poj2481 Cows 树状数组

    题目链接:http://poj.org/problem?id=2481 解题思路: 这道题对每组数据进行查询,是树状数组的应用.对于二维的树状数组, 首先想到排序.现在对输入的数据按右值从大到小排序, ...

  7. 为Distinct准备的通用对比器

    使用Linq过滤重复对象的时候,我们使用Distinct. 但是Distinct对int long等值类型才有效果,对于对象我们需要自己写个对象. 以下利用泛型封装了两个类: CommonCompar ...

  8. 如何实现在Windows上运行Linux程序,附示例代码

    微软在去年发布了Bash On Windows, 这项技术允许在Windows上运行Linux程序, 我相信已经有很多文章解释过Bash On Windows的原理, 而今天的这篇文章将会讲解如何自己 ...

  9. springboot(二):web综合开发

    上篇文章介绍了spring boot初级教程:spring boot(一):入门篇,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续为大家介绍spring boot的其它 ...

  10. vue1.0和vue2.0的区别(一)

    今天我们来说一说vue1.0和vue2.0的主要变化有哪些 一.在每个组件模板,不在支持片段代码 VUE1.0是: <template> <h3>我是组件</h3> ...