Spring学习(9)--- @Autowired注解(二)
- 可以使用@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注解(二)的更多相关文章
- spring boot 中@Autowired注解无法自动注入的错误
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...
- 如何实现一个简易版的 Spring - 如何实现 @Autowired 注解
前言 本文是 如何实现一个简易版的 Spring 系列第四篇,在 上篇 介绍了 @Component 注解的实现,这篇再来看看在使用 Spring 框架开发中常用的 @Autowired 注入要如何实 ...
- Spring学习笔记--使用注解装配
使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...
- Spring学习之-各注解的含义总结
注解配置 @ComponentScan("spittr.web"):/在加载Spring上下文时,会扫描spittr.web包查找组件 @ComponentScan注解扫描的组件有 ...
- spring @Resource与@Autowired注解详解
具有依赖关系的Bean对象,利用下面任意一种注解都可以实现关系注入: 1)@Resource (默认首先按名称匹配注入,然后类型匹配注入) 2)@Autowired/@Qualifier (默认按类型 ...
- Spring学习之事务注解@Transactional
今天学习spring中的事务注解,在学习Spring注解事务之前需要明白一些事务的基本概念: 事务:并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通 ...
- Spring中的Autowired注解和Resource注解的区别
1.所属jar包不同,Autowired是Spring中的Resource是JSR-250规范定义的注解
- Spring学习 Ioc篇(二 )
5.spring依赖注入的方式 方法一:使用构造器方式进行注入 1.dao的类和接口 package com.cvicse.dao.impl; import com.cvicse.dao.Person ...
- Spring学习之路-注解
Spring的注解总结. 地址:https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsin ...
随机推荐
- [视频]物联网&集成系统中的物联交互、数据存储、效果展示形成快速解决方案。附:ServerSuperIO 3.6.2 版本发布。
ServerSuperIO v3.6.2版本更新内容: 设备驱动与实时库对接的Tag配置与OPC Client读取数据的配置统一用一个配置文件. 设备驱动继承DeviceDynamic接口的子类支持存 ...
- [ext4]04 磁盘布局 - Meta Block Groups
Meta Block Groups,可以翻译为元块组集. 如果不采用Meta Block Groups特性,在每个冗余备份的超级块的后面是一个完整的(包含所有块组描述符的)块组描述符表的备份.如前所述 ...
- [进程管理]Linux进程状态解析之R、S、D
Linux是一个分时操作系统,能够在一个cpu上运行多个程序,每个被运行的程序实例对应一个或多个进程,这里介绍一下Linux进程状态. Linux是一个多用户,多任务的系统,可以同时运行多个用户的多个 ...
- sublime text3在指定浏览器上本地服务器(localhost)运行文件(php)
昨天在使用sublime text3时,发现能在本地服务器上运行php文件,于是百度了一下有关知识, 终于成功了,今天总结一下. 首先要让sublime text3 出现侧边栏sidebar,不会的可 ...
- codeforces 782B The Meeting Place Cannot Be Changed (三分)
The Meeting Place Cannot Be Changed Problem Description The main road in Bytecity is a straight line ...
- IOS中的绘图Quartz2D
drawRect 方法的使用 常见图形的绘制:线条.多边形.圆 绘图状态的设置:文字颜色.线宽等 图形上下文状态的保存与恢复 图形上下文栈 Quartz 2D是一个二维绘图引擎,同时支持IOS和MAC ...
- Python之collections序列迭代器下标式循环冒泡算法等
练习题 元素分类 有如下值集合[11,22,33,44,55,66,77,88,99]将所有大于66的数作为一个列表放在字典的key为k1的value小于等于66的为k2的value {'k1':[7 ...
- Hbuilder常用功能汇总
引用 样式表: mui.min.css Js:mui.min.js 常用功能 获取页面 var webView=plus.webview.currentWebview();//获取当前页 var we ...
- OpenCV探索之路(三):滤波操作
滤波处理分为两大类:线性滤波和非线性滤波.OpenCV里有这些滤波的函数,使用起来非常方便,现在简单介绍其使用方法. 线性滤波:方框滤波.均值滤波.高斯滤波 方框滤波 #include<open ...
- TP框架 命名空间 与第三方类
命名空间 相当于虚拟目录 所有类文件都放在虚拟目录 功能:实现自动加载类 TP框架的命名空间要更复杂 内容=> 命名空间中定义和使用 都用\1初始命名空间 相当于 根目录 如:Library文件 ...