【问题标题】:Loading an application context by annotating a class通过注释类加载应用程序上下文
【发布时间】:2012-07-20 17:29:40
【问题描述】:

我是 Spring 的新手,想知道是否可以通过注释必须注入其变量的类来加载应用程序(而不是使用 ApplicationContext ctx = new ApplicationContext("myAppContext"))。

让我举个例子:

我有这个类TestSpring.java,其中一个字符串应该是自动装配的

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);
    }

}

spring bean 配置文件(TestSpringContext.xml)长这样

<?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>

现在我想在RunTestSpring.java 中使用以下代码显示自动装配字符串myString(在TestSpring.java 中声明)的值:

package mytest;

public class RunTestSpring {

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

    }

}

现在我的问题是,在加载应用程序上下文时是否可以通过注释RunTestSpring.java 成功运行“RunTestSpring.java”。如果是,使用哪个注释?

【问题讨论】:

    标签: java spring


    【解决方案1】:

    @Configurable 可能是您正在寻找的,它将确保未由 Spring 实例化的对象可以由 Spring 自动装配其依赖关系。然而问题是它需要 AspectJ 编译时间/加载时间编织才能工作(不是 Spring AOP)。

    这是一个参考: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aop-atconfigurable

    【讨论】:

      【解决方案2】:

      我建议编写一个 JUnit 类,该类将使用 spring 注入进行环境初始化。像这样 -

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

      【讨论】:

      • 感谢答案,但我已经知道如何在 junit 测试中做到这一点。我想在 Util 类(不在测试范围内)中使用类 TestSpring.java,这就是为什么注释 @RunWith(SpringJUnit4ClassRunner.class) 不适合这种情况。但是
      • 不确定@ImportResource 是否是您需要的注释。我没试过。请参阅此处的文档 [static.springsource.org/spring/docs/3.1.x/javadoc-api/org/… 和此处的示例 [theserverside.com/tip/…
      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 2011-05-13
      • 2016-06-27
      • 2016-07-06
      相关资源
      最近更新 更多