wood burning stoves 2.0*
The moose likes Spring and the fly likes autowire spring bean in a regular pojo which is not spring bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "autowire spring bean in a regular pojo which is not spring bean" Watch "autowire spring bean in a regular pojo which is not spring bean" New topic
Author

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

Darvesh Niz
Ranch Hand

Joined: May 12, 2008
Posts: 119
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

Joined: Oct 27, 2009
Posts: 92
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: autowire spring bean in a regular pojo which is not spring bean
 
Similar Threads
Invoking Method
Spring property value reference ...
Access hibernate session inside onMessage using ActiveMQ
Please explain the logic
List box initialization through managed bean