Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复java Spring bean Autowire无法处理(123){_Spring_Autowired_Spring Framework Beans - Fatal编程技术网

如何修复java Spring bean Autowire无法处理(123){

如何修复java Spring bean Autowire无法处理(123){,spring,autowired,spring-framework-beans,Spring,Autowired,Spring Framework Beans,我想插入Spring的基础知识:用Spring的@Value动态注入值。我该怎么做 我用密码试过了 @Value("#{{2: 'Catch22', 22: '1984', 32: 'Pride and Prejudice'}}") 但我有个例外 private Map <Integer,String>studentTeacherMap; @Value("#{{2: 'Catch22', 22: '1984', 32: 'Pride and Prejudice'}}") publi

我想插入Spring的基础知识:用Spring的@Value动态注入值。我该怎么做

我用密码试过了

@Value("#{{2: 'Catch22', 22: '1984', 32: 'Pride and Prejudice'}}")
但我有个例外

private Map <Integer,String>studentTeacherMap;
@Value("#{{2: 'Catch22', 22: '1984', 32: 'Pride and Prejudice'}}")
public Map<Integer, String> loki()
return studentTeacherMap;
但结果却有一个例外:


不能处理123’{’/p>< p>如果您想将此映射登记为bean,请考虑将其放入@配置注释类:

@配置 公共类应用程序配置{ @价值{{2:'Catch22',22:'1984',32:'傲慢与偏见'} 洛基马普私人地图; @豆子 洛基马普公共地图{ 返回洛基马普; } } 并将这个bean注入到您想要使用它的类中:

@自动连线 洛基马普私人地图; 或者您可以直接使用映射,而无需将其声明为bean,但在这种情况下,您不需要@Autowired注释,只需将@Value放在映射下:

@价值{{2:'Catch22',22:'1984',32:'傲慢与偏见'} 洛基马普私人地图;
感谢您的快速重播,正如您提到的,我使用了Autowire bean配置,现在我得到了一个新的异常,即map[java.util.map]的键类型[class java.lang.Integer]必须可分配给[java.lang.String]@bean公共映射getStudentTeacherMap{return studentTeacherMap;}@Value{{1:'Catch-22',2:'1984',3:'Pride and Prediction'}public void setStudentTeacherMap studentTeacherMap{this.studentTeacherMap=studentTeacherMap;}@Loki首先,您的@Bean方法名称应该命名为studentTeacherMap。如果您将方法名称设置为getStudentTeacherMap,则生成的Bean名称将是:getStudentTeacherMap,但不是studentTeacherMap。在您的情况下,它起作用了,因为自动连线首先是按Bean的类型执行的。其次,我无法重现您提到的错误。您能这样做吗提供完整的代码?谢谢你的帮助,我计算了错误代码,在一开始,我提到不能处理123'{',这是因为,在我使用Spring版本2.5.6之前,现在我将其更改为5.1.3。现在发布它可以处理错误代码并获得输出。谢谢你的帮助。
2= Catch22, 22= 1984, 32= Pride and Prejudice