• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

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

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic