1

In DAO:

private Map<Integer,String> departments = new LinkedHashMap<Integer, String>();

@Override
public List<DepartmentEntity> getAllDepartments() {
    return this.sessionFactory.getCurrentSession().createQuery("from DepartmentEntity de order by LOWER(de.departmentname)").list();

}

@Override
public Map<Integer, String> loadDepartments() {
    departments.clear();
    for (DepartmentEntity de : getAllDepartments())
        departments.put(de.getDepartmentid(), de.getDepartmentname());
    return departments;
}

Its Working fine, but in spring creation of objects manually its bad code

private Map<Integer,String> departments;

So, how to inject map object of LinkedHashMap type from out side in my case ?.

I tried but i got exceptions like null pointer exception

Please any one help me..

Developer
  • 433
  • 3
  • 8
  • 20
  • There is no need to declare the departments outside the method.You can declare inside the method and use it. – Keval Trivedi Jun 25 '14 at 05:13
  • @KevalTrivedi if i will declare in side the method it is also same, but wish is inject object type of LinkedHashMap from outside. – Developer Jun 25 '14 at 05:21
  • This link helps you :http://stackoverflow.com/questions/3627897/spring-framework-populating-a-mapenum-object-with-utilmap – Keval Trivedi Jun 25 '14 at 05:25

3 Answers3

1
 <util:map id="myMap" map-class="java.util.LinkedHashMap" key-type="java.lang.Integer" value-type="java.lang.String"/>

<bean id="departmentDAOImpl" class="com.leadwinner.infra.assets.dao.DepartmentDAOImpl">
    <property name="departments" ref="myMap"></property>
</bean>
Developer
  • 433
  • 3
  • 8
  • 20
0

You can do something like below: eg.

class A{
   private B b;

   public setB(B b){
      this.b = b;
   }

   public Map getMapFromA(){
      return b.getMap();
   }

}

class B{
   private Map tmp;

   public void setMap(HashMap t){
      tmp.putAll(t);
   }
   public HashMap getMap(){
      return tmp;
   }
}

And in web.xml

<bean id="classB" class="default.B"/>
<bean id ="classA" class="default.A"/>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject"><ref local="classA"></property>
        <property name="targetMethod"><value>setB</value></property>
        <property name="arguments"><ref local="classB"/></property>
</bean>

Now spring beans are by default singleton scoped. So you can do the following.

function do(){
   B b = ctx.getBean("classB");
   b.setMap(someMap);

   A a = ctx.getBean("classA");
   a.getMapFromA();

}

I havent tried out the code but it will give you an idea I hope so. More details on MethodInvokingFactoryBean : here

And if you dont want to do it by Spring and if you want less efforts try using ThreadLocal to pass parameters.

Sumeet Sharma
  • 2,573
  • 1
  • 12
  • 24
0

Populate map in this way (using constructor injection):

<bean name="DAO" class="path.to.yourDAOClass">
  <constructor-arg index="0">
    <map>
      <entry key="1" value="One" />
      <entry key="2" value="Two" />
    </map> 
  </constructor-arg>
<bean>

By default target class for <map /> is a LinkedHashMap, but you can change target class using a MapFactoryBean to construct your map object in this way by replace the <map /> tag with:

<bean class="org.springframework.beans.factory.config.MapFactoryBean">
  <property name="targetMapClass">
    <value>java.util.HashMap</value>
  </property>
  <property name="sourceMap">
    <map>
      <entry key="1" value="One" />
      <entry key="2" value="Two" />
    </map> 
  </property>
</bean>
Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69