views:

105

answers:

3

Hello,

I have a test class that has a @Resource annotation for a setter and I need to make it Java 1.4 compliant, so obviously the annotation has to go. I'm using Spring.

So, how would I replace something like @Resource("my.resource") so that the setter gets the correct dependency injection? Would I need to make a bean in an xml file?

I'm pretty new to this so if I'm not providing enough information, let me know.

A: 

A hacky way to do it, is implementing the ApplicationContextAware interface, and then getting the bean using ApplicationContext.getBean inside setApplicationContext in your class.

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/context/ApplicationContextAware.html

Matias Valdenegro
+4  A: 

If you are in a Java 1.4 environment you cannot rely on Annotations as you already mentioned correctly. So you have to declare dependencies and bean definitions inside your XML-document that configures your Spring ApplicationContext.

<bean id="myBeanName" class="my.package.MyClass">
   <!-- injects otherBean into propertyName -->
   <property name="propertyName" ref="otherBean" />

   <!-- injects propertyValue into otherProperty -->
   <property name="otherProperty" value="propertyValue" />

   <!-- injects an instance of an anonymous bean into innerBean -->
   <property name="innerBean">
       <bean class="my.package.InnerBean" />
   </property>
</bean>

<bean id="otherBean" class="my.package.OtherBean" />
codescape
That cleared up my confusion. Thank you.
AHungerArtist
A: 

Consider xdoclet?

Matthew Flynn