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 ...
随机推荐
- [转]Installing Memcached on Windows
Installing Memcached on Windows 原文链接https://commaster.net/content/installing-memcached-windows Sub ...
- Linux系统操作指令汇总
1.系统配置 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIO ...
- 开发一款直播APP系统软件应该有哪些功能,如何开发?
1.技术实现层面: 技术相对都比较成熟,设备也都支持硬编码.IOS还提供现成的 Video ToolBox框架,可以对摄像头和流媒体数据结构进行处理,但Video ToolBox框架只兼容8.0以上版 ...
- [KISSY5系列]淘宝全终端框架 KISSY 5--从零开始使用
KISSY 是淘宝一个开源的 JavaScript 库,包含的组件有:日历.图片放大镜.卡片切换.弹出窗口.输入建议等 一.简介 KISSY 是一款跨终端.模块化.高性能.使用简单的 JavaScri ...
- hadoop单机环境搭建
[在此处输入文章标题] Hadoop单机搭建 1. 工具准备 1) Hadoop Linux安装包 2) VMware虚拟机 3) Java Linux安装包 4) Window 电脑一台 2. 开始 ...
- 让Cocos2dx中的TestCPP中的Box2dTest运行起来
一般而言,如果你导入TestCPP到VS2012中去后,会编译十几分钟才会出现窗口界面,这是包含Cocos2dx所有功能的一个demo,功能非常齐全强大,里面有两个关于Box2d的测试案例,一个是Bo ...
- 55 Jump Game i && 45 Jump Game ii
Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...
- 由于losf引起的pxc启动报错处理
PXC主节点启动完成后,再启动node1,error日志报错: 2017-05-02T15:23:42.830888Z 0 [ERROR] WSREP: Failed to read 'ready & ...
- VM VirtrualBox 安装centos6.5后的网络设置
小白学习linux(一) 本文只是介绍VirtrualBox安装centos完成后的网络设置: 每次用虚拟机装完centos后,捣鼓半天才能上网.熟话说的好,好记性不如烂笔头,写个随笔记录下设置过程, ...
- Django入门笔记
Django入门笔记 **文档包含Django安装包.学习的笔记.代码等 安装 Django参考附件,只需要把附件拷贝到你需要的目录就行.Django是1.8.16版本 Python:在附件中,其中有 ...