spring map使用annotation泛型注入问题分析
来源:互联网 发布:越南游戏市场软件 编辑:程序博客网 时间:2024/05/17 17:55
今天在整一个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 moreCaused 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 moreCaused 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 map使用annotation泛型注入问题分析
- Spring中使用annotation注入
- Spring中使用annotation注入
- Spring中使用annotation注入
- Spring中使用annotation注入
- Spring中使用annotation注入
- spring Annotation 组件注入
- spring mvc导致的已配置placeholder但是placeholder却无法注入的问题(使用annotation)
- spring annotation 注入static 属性
- spring annotation注解 依赖注入
- spring annotation 注入static 属性
- Spring的annotation注入实例
- spring annotation 注入static 属性
- Spring Map依赖注入
- spring注入map
- Spring 注入 Map
- spring泛型注入的问题
- spring泛型注入的问题
- hdu3343
- 使用Spread.NET创建Y Plot图表
- android应用安全——组件通信安全(Intent)
- jQuery实现ajax
- web开发平台的演变
- spring map使用annotation泛型注入问题分析
- android开机速度优化 | 全志A10
- 访问共享时输入的密码怎样保存与更改
- hadoop helloworld
- 罗永浩:我对3月27日那场锤子发布会非常懊悔
- 云里雾里的sizeof(结构体)
- SVN不显示对号的解决方法
- C++的引用与const指针的关系以及各种传递方式
- Canvas学习