简体   繁体   中英

Loading an application context by annotating a class

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?

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 ...
}

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM