1

I am new at Spring and am wondering if one can load an application just by annotating the class whose variables must be injected (instead of using ApplicationContext ctx = new ApplicationContext("myAppContext")).

Let me give the following example:

I have this class TestSpring.java in which a string should be autowired

package mytest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

//Is it possible to put an annotation here that loads the application context "TestSpringContext.xm"??
public class TestSpring {

    @Autowired
    @Qualifier("myStringBean")
    private String myString;


    /**
     * Should show the value of the injected string
     */
    public void showString() {
        System.out.println(myString);
    }

}

The spring bean configuration file (TestSpringContext.xml) looks like this

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
        >

 <context:annotation-config />

 <bean id="myStringBean" class="java.lang.String">
 <constructor-arg value="I am  an injected String."/>
</bean>
</beans>

Now I would like to display the value of the autowired string myString (declared in TestSpring.java) using following code in RunTestSpring.java:

package mytest;

public class RunTestSpring {

    public static void main(String[] args) {
        TestSpring testInstance = new TestSpring();
        testInstance.showString();

    }

}

Now my question, is it possible to run "RunTestSpring.java" successfully while loading the application context by just annotating RunTestSpring.java. If yes, with which annotation?

2 Answers 2

2

@Configurable is probably what you are looking for, it will ensure that objects which are not instantiated by Spring can have their dependencies autowired by Spring. However the catch is that it requires AspectJ compile time/load time weaving for it to work(not Spring AOP).

Here is one reference: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aop-atconfigurable

Sign up to request clarification or add additional context in comments.

Comments

2

I would suggest writing a JUnit class that would use spring injection for environment initialization. Something like this -

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/spring/spring-wireup.xml", inheritLocations = true)
public class MyTestCase extends TestCase {
    // your test methods ...
}

2 Comments

Thx for the answer but I already know how to do it in a junit test. I would like to use the class TestSpring.java in a Util class (which is out of Test scope) and that s why the annotation @RunWith(SpringJUnit4ClassRunner.class) would not fit in this case. But
Not sure if @ImportResource is the annotation that you need. I haven't tried it. See the documentation here [static.springsource.org/spring/docs/3.1.x/javadoc-api/org/… and an example here [theserverside.com/tip/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.