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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421862.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

(JAVA)Object类之toString()和equals()

Object类&#xff1a;存储于java.lang包中&#xff0c;有构造方法&#xff0c;无super 一.toString():返回该对象的字符串表示-----String类型&#xff0c;输出类型中&#xff0c;默认调用toString()1.public String toString() {return getClass().getName() "" I…

(JAVA)Object类之Scanner

/* Scanner类&#xff1a;使用正则表达式解析基本类型和字符串的简单文本扫描器 一.源代码&#xff1a;public final class Scanner implements Iterator<String>, Closeable {...}1.不允许继承2.使用时必须导入包 import java.util.Scanner&#xff1b; jdk1.5以上版本才…

序列化 小复习

想要序列化一个对象则需要使其继承serializable或者externalizable接口 一下是一个实例小程序&#xff1a; ser_test 1 import java.io.*;2 3 public class ser_test{4 public static void main(String[] args) throws Exception{5 person p1 new person(1,1.2,&q…

(JAVA)Object类之String类

/* 字符串&#xff1a; 一、概述&#xff1a;1.字符串在JAVA中&#xff0c;使用""表示2.java.lang.String类3.只要写""就是字符串对象。不需要new二、空参构造器new Sting();private final char value[];public String() {this.value "".value;…

(JAVA)String类之比较方法

/* 字符串&#xff1a; 一、概述&#xff1a;1.字符串在JAVA中&#xff0c;使用""表示2.java.lang.String类3.只要写""就是字符串对象。不需要new二、空参构造器new Sting();private final char value[];public String() {this.value "".value;…

云计算之路-黎明前的黑暗:20130424网站故障经过

一、背景 4月18日的访问高峰扛过去之后&#xff0c;我们和阿里云一直在努力寻找问题的真正原因。是问题&#xff0c;躲不去的&#xff0c;不找到根源&#xff0c;随时会突然袭击。 压力测试未能重现问题&#xff0c;只能进行大海捞针般的猜测&#xff1a;SLB&#xff08;均衡均…

hdu2955 Robberies (动态规划之背包)

http://acm.hdu.edu.cn/showproblem.php?pid2955 题意&#xff1a;Roy想要抢劫银行&#xff0c;每家银行多有一定的金额和被抓到的概率&#xff0c;知道Roy被抓的最大概率P&#xff0c;求Roy在被抓的情况下&#xff0c;抢劫最多。 分析&#xff1a;被抓概率可以转换成安全概率…

黑马Java学习笔记之-----集合框架

---------------------- android培训、java培训、期待与您交流&#xff01; ---------------------- 一&#xff0e;概述&#xff1a; Java的集合类是一种特别有用的工具类&#xff0c;它可以用于存储数量不等的多个对象&#xff08;实际上是对象的引用&#xff09;&#xff0c…

(JAVA)基本数据类型 对象包装类

package com.book.lite;/*** author zhangyu* date 2021年08月15日 4:51 下午* 基本数据类型 对象包装类* 对八个基本数据类型&#xff0c;提供8个类&#xff0c;&#xff0c;将基本数据类型&#xff0c;封装成8个对象* byte Byte* short Short* int I…

作业自动提示功能设计思路

1、利用现在FLEX项目中的心跳包机制&#xff0c;使用SOCKET心跳包技术获取最新的作业情况。 2、在现在FLEX项目中有一个&#xff1a; 核心代码&#xff1a; 这样我们可以利用这个通道&#xff0c;获取相应的信息。 具体修改步骤如下&#xff1a; 1、准备工作 创建一张表&#x…