简体   繁体   中英

Not able to inject Bean

I am trying to inject using a java bean . Its a very basic implementation however the injection is not working. Could you please guide me?

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;
        }
    }

Also: for my 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>

Could someone guide me as to what is wrong?

The @Autowired annotation should be above the field you want to inject. And that field shouldn't be assigned only declared.

Also, injecting a map isn't the easiest example to start with. You should write a simpler test case just to get the hang of it. For example,

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

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

    public String getInjectThis() {
         return injectThis;
    }
}

And here's the 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>

You have two possible fixes:

a. Declare your newMap as being of type Map<String, Map<String, String>> not concrete Hashmap, this is because internally util:map returns an object type of Map and autowiring will not be able to find suitable candidates.

b. Remove the @Autowired from newMap and instead inject the dependency directly through xml, the way you have done:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM