spring map使用annotation泛型注入问题分析
今天在整一个spring的ioc学习demo,碰到一个问题,居然@Autowire在set方法注入map时,map的key类型不能为String之外的其他类型,具体看下面问题
@Autowiredpublic void setTypeMap(Map<Integer,String> typeMap) {this.typeMap = typeMap;}
xml注入配置,即时在map上加上key和value的类型也不行。
<property name="typeMap"><map key-type="java.lang.Integer" value-type="String"><entry key="1"><value>COO)</value></entry><entry key="2"><value>CFO</value></entry><entry key="3"><value>CEO</value></entry></map></property>
报错:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springweb.service.impl.HelloServiceImpl.setTypeMap(java.util.Map); nested exception is org.springframework.beans.FatalBeanException: Key type [class java.lang.Integer] of map [java.util.Map] must be assignable to [java.lang.String]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:589)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)... 13 more
Caused by: org.springframework.beans.FatalBeanException: Key type [class java.lang.Integer] of map [java.util.Map] must be assignable to [java.lang.String]at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:761)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:547)
查看spring的代码
else if (Map.class.isAssignableFrom(type) && type.isInterface()) {Class keyType = descriptor.getMapKeyType();if (keyType == null || !String.class.isAssignableFrom(keyType)) {if (descriptor.isRequired()) {throw new FatalBeanException("Key type [" + keyType + "] of map [" + type.getName() +"] must be assignable to [java.lang.String]");}return null;}
也就是spring使用Autowired进入注入时,map的key类型只能为string,但是
如果我使用set方法注入后者构造方法注入,map的可以是可以自动转为integer的。
set方法注入,不要在set方法上使用antowired
public void setTypeMap(Map<Integer,String> typeMap) {this.typeMap = typeMap;}
//构造方法注入public HelloServiceImpl(String greetting,Map<Integer,String> typeMap){this.greetting = greetting;this.typeMap = typeMap;System.out.println("call HelloServiceImpl constructor" + this.greetting);}
<constructor-arg type="Map" index="1"><map><entry key="1"><value>COO</value></entry><entry key="2"><value>CFO</value></entry><entry key="3"><value>CEO</value></entry></map></constructor-arg>
可以参考http://forum.springsource.org/showthread.php?105558-Autowired-injection-of-a-Map
今天在整一个spring的ioc学习demo,碰到一个问题,居然@Autowire在set方法注入map时,map的key类型不能为String之外的其他类型,具体看下面问题
@Autowiredpublic void setTypeMap(Map<Integer,String> typeMap) {this.typeMap = typeMap;}
xml注入配置,即时在map上加上key和value的类型也不行。
<property name="typeMap"><map key-type="java.lang.Integer" value-type="String"><entry key="1"><value>COO)</value></entry><entry key="2"><value>CFO</value></entry><entry key="3"><value>CEO</value></entry></map></property>
报错:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springweb.service.impl.HelloServiceImpl.setTypeMap(java.util.Map); nested exception is org.springframework.beans.FatalBeanException: Key type [class java.lang.Integer] of map [java.util.Map] must be assignable to [java.lang.String]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:589)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)... 13 more
Caused by: org.springframework.beans.FatalBeanException: Key type [class java.lang.Integer] of map [java.util.Map] must be assignable to [java.lang.String]at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:761)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:547)
查看spring的代码
else if (Map.class.isAssignableFrom(type) && type.isInterface()) {Class keyType = descriptor.getMapKeyType();if (keyType == null || !String.class.isAssignableFrom(keyType)) {if (descriptor.isRequired()) {throw new FatalBeanException("Key type [" + keyType + "] of map [" + type.getName() +"] must be assignable to [java.lang.String]");}return null;}
也就是spring使用Autowired进入注入时,map的key类型只能为string,但是
如果我使用set方法注入后者构造方法注入,map的可以是可以自动转为integer的。
set方法注入,不要在set方法上使用antowired
public void setTypeMap(Map<Integer,String> typeMap) {this.typeMap = typeMap;}
//构造方法注入public HelloServiceImpl(String greetting,Map<Integer,String> typeMap){this.greetting = greetting;this.typeMap = typeMap;System.out.println("call HelloServiceImpl constructor" + this.greetting);}
<constructor-arg type="Map" index="1"><map><entry key="1"><value>COO</value></entry><entry key="2"><value>CFO</value></entry><entry key="3"><value>CEO</value></entry></map></constructor-arg>
可以参考http://forum.springsource.org/showthread.php?105558-Autowired-injection-of-a-Map
转载于:https://www.cnblogs.com/zhwj184/archive/2013/04/15/3027417.html
spring map使用annotation泛型注入问题分析相关推荐
- (转)Spring对注解(Annotation)处理源码分析1——扫描和读取Bean定义
1.从Spring2.0以后的版本中,Spring也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5中引入的一个新特性,用于简化Bean的配置,某些场合可以 ...
- Spring按类型自动装配注入数组、集合、Map
Spring按类型自动装配注入数组.集合.Map时,是把应用上下文中对应类型的bean装配进集合,而不是直接查找一个对应类型的集合然后注入.以下面这段代码为例: import org.springfr ...
- Spring IOC容器的依赖注入流程(收集和注册、分析和组装)
Spring IOC容器的依赖注入流程 Spring IOC容器的依赖注入工作可以分为两个阶段: 阶段一:收集和注册 第一个阶段可以认为是构建和收集bean定义的阶段,在这个阶段中,我们可以通过XML ...
- 初学者都能看懂的 Spring 源码之依赖注入(DI)源码分析
前言 在面试中,经常被问到 Spring 的 IOC 和 DI (依赖注入),很多人会觉得其实 IOC 就是 DI ,但是严格上来说这两个其实并不等价,因为 IOC 注重的是存,而依赖注入注重的是取, ...
- Java程序员进阶——Spring依赖注入原理分析
Spring依赖注入原理分析 下面谈谈Spring是如何实现反转模式IOC或依赖注入模式DI: 平时,我们需要生成一个对象,使用new语法,如一个类为A public class A{public v ...
- Spring框架中XML配置文件注入集合(数组、LIST、MAP、SET)属性
Spring框架中XML配置文件注入集合属性 前言 创建测试类与属性 配置XML配置文件 建立调用类 调用结果 前言 某些类的属性是可能是集合,包括:数组.LIST.MAP.SET等集合,在Sprin ...
- 一步一步手绘Spring DI运行时序图(Spring 自动装配之依赖注入)
相关内容: 架构师系列内容:架构师学习笔记(持续更新) 一步一步手绘Spring IOC运行时序图一(Spring 核心容器 IOC初始化过程) 一步一步手绘Spring IOC运行时序图二(基于XM ...
- Spring源码剖析 循环注入
版权声明:本文为CSDN博主「shadow?s」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明. 原文链接:https://blog.csdn.net/java_ly ...
- Spring Cloud底层原理以及项目实战分析
一.业务场景介绍 二.Spring Cloud核心组件------------------------------------------------------------------------- ...
最新文章
- STM32开发 -- UART应用层通信协议分析
- python_易忘的简单知识点总结
- 三次握手和四次挥手之间的关系
- 矩阵分解 java_使用矩阵分解为推荐系统
- oracle打开当前表的编辑,oracle sqlplus常用命令
- Mangos源码分析(15):游戏对象的实现
- SQL server 2008 中的五个系统数据库详解
- Python 学习笔记 - 11.模块(Module)
- struct and typedef
- 关于分辨率,你该知道这些!
- python竖线_6.1. re模块搜索时要注意竖线|的使用
- 数据结构第二遍思维导图
- I/O设备和CPU之间数据传送控制方式
- 华为ensp联动Wmware虚拟机Openstack平台实现Vlan网络模式
- RDKit 操作分子对象
- 网易Airtest简介
- webgl_浏览器支持问题
- Unity多分辨率适配
- 清华大学历任计算机学院院长,历任领导
- React 原理揭秘总结
热门文章
- [react] shouldComponentUpdate方法是做什么的
- [react] 状态管理器它精髓是什么?
- 前端学习(3068):vue+element今日头条管理-上午总结
- 前端学习(2705):重读vue电商网站26之路由导航守卫控制访问权限
- 前端学习(2533):mapgetter和actions
- 前端学习(2526):Vuex成果和展示
- 前端学习(1847)vue之电商管理系统电商系统的功能划分
- 前端学习(1418):服务器响应的数据格式
- [GAN学习系列2] GAN的起源
- mysql执行过程五步_简单五步教你搭建MySQL主从复制