无法注入Bean

时间:2013-09-11 21:21:04

标签: java spring javabeans

我正在尝试使用java bean注入。它是一个非常基本的实现,但注入不起作用。你能指导我吗?

test.java

 public class TempClass{
@Autowired
        HashMap<String,HashMap<String,String>> newMap = new HashMap<String,HashMap<String,String>>();


        public void setNewMap(HashMap<String, HashMap<String,String>> newMap)
        {
            newMap= newMap;
        }

        public HashMap<String, HashMap<String,String>> getNewMap()
        {
            return newMap;
        }
    }

此外: 对于我的bean config conn.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:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <util:map id="xyz" map-class="java.util.HashMap">
        <entry key="x" value-ref="x" />
        <entry key="y" value-ref="y" />
        <entry key="z" value-ref="z" />
    </util:map>

    <util:map id="x" map-class="java.util.HashMap">
        <entry key="xx" value="xx" />
        <entry key="xy" value="xy" />
        <entry key="xz" value="xz" />
    </util:map>

    <util:map id="y" map-class="java.util.HashMap">
        <entry key="yx" value="yx" />
        <entry key="yy" value="yy" />
        <entry key="yz" value="yz" />
    </util:map>

    <util:map id="z" map-class="java.util.HashMap">
        <entry key="zx" value="zx" />
        <entry key="zy" value="zy" />
        <entry key="zz" value="zz" />
    </util:map>

 <bean id="bean123" class="reference.to.class" autowire="byName">
      <property name="newMap" ref="xyz" />
   </bean>

</beans>

有人可以指导我出错吗?

2 个答案:

答案 0 :(得分:1)

@Autowired注释应该 要注入的字段。并且不应仅声明该字段。

此外,注入地图并不是最简单的示例。你应该编写一个更简单的测试用例来获取它。例如,

package test;
public class SpringInjectionTest {
    @Autowired
    private String injectThis;

    public void setInjectThis(String s) {
         injectThis = s;
    }

    public String getInjectThis() {
         return injectThis;
    }
}

这是applicationContext:

<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-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />   

    <bean id="testBean" class="test.SpringInjectionTest" autowire="byName"/>
</beans>

答案 1 :(得分:1)

您有两种可能的解决方法:

一个。声明您的newMap属于Map<String, Map<String, String>>类型而不是具体的Hashmap,这是因为内部util:map返回对象类型Map,并且自动装配将无法找到合适的候选对象。

湾从@Autowired中删除newMap,然后通过xml直接注入依赖项,就像您完成的那样:

<bean id="bean123" class="...">
   <property name="newMap" ref="xyz" />
</bean>