开发者

How To Replace @Resource Annotation with Java 1.4 Compliant Version

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.


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" />


Consider xdoclet?

XDoclet was really the predecessor to what became annotations in Java 5. It is an open-source code generation library that enables Attribute-oriented programming for Java via insertion of special Javadoc tags. It comes with a library of predefined tags, which simplify coding for various technologies: Java EE, Web services, Portlet etc.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜