Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

autowire spring bean in a regular pojo which is not spring bean

 
Darvesh Niz
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
a quick question.
i have a class which holds a hashmap.

@Component
public class PhotoTypeMapper {
private Map<String,String> photoTypeMap;
}

In the spring config i initilize the map
<bean id="photoTypeMapper" class="com.XXX.services.PhotoTypeMapper">
<property name="photoTypeMap">
<map>
<entry key="TDS">
<value>Exterior</value>
</entry>
<entry key="ETS">
<value>Exterior</value>
</entry>
<entry key="INP">
<value>Interior</value>
</entry>
</map>
</property>
</bean>

i have a regular class for example Test this and this is just a regular class how can i autowire my mapper class in this i tried.
class Test{

@Autowired
private PhotoTypeMapper photoTypeMapper;
}
when i check i get photoTypeMapper as null since the class Test is not a spring bean.

Any shortcuts how to achive this, i can always use the ClassPathApplicationContext and manullay get the bean just is there a way to autowire it.

Thanks
darniz

 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Autowired annotation is used in wrong way.

As in your this is a bean so to test this bean. Added getter and setter to the property photoTypeMap in your pojo.
in your XML file You have already defiened the bean so need for annotations remove all annotations.

In the test Class get the the bean.


Please use code Tags...Just edit your code and use Code tags
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic