How to replace @Resource annotation with Java 1.4

I have a test class that has @Resource annotation for the setter, and I need to make it compatible with Java 1.4, so obviously the annotation should go. I am using Spring.

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

I am new to this, so if I have not provided enough information, let me know.

+3
source share
3 answers

If you are in a Java 1.4 environment, you cannot rely on annotations, as you mentioned correctly. Therefore, you must declare dependencies and bean definitions inside your XML document, which sets up 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" />
+4
source

Consider xdoclet ?

XDoclet was truly the forerunner of what became annotations in Java 5. It is an open source code generation library that allows attribute-based programming for Java by inserting custom Javadoc tags. It comes with a library of predefined tags that simplify coding for various technologies: Java EE, web services, portlet, etc.

+1
source

, ApplicationContextAware, bean ApplicationContext.getBean setApplicationContext .

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

0
source

Source: https://habr.com/ru/post/1749952/


All Articles