是否有一种方法可以在Spring应用程序中静态/全局地请求ApplicationContext的副本?

假设主类启动并初始化了应用程序上下文,它是否需要通过调用堆栈向下传递给任何需要它的类,或者类是否有一种方法来请求先前创建的上下文?(我假设它必须是单例的?)


当前回答

请注意,通过将当前ApplicationContext的任何状态或ApplicationContext本身存储在一个静态变量中(例如使用单例模式),如果使用Spring-test,您将使您的测试不稳定且不可预测。这是因为Spring-test在同一个JVM中缓存和重用应用程序上下文。例如:

测试运行并使用@ContextConfiguration({"classpath:foo.xml"})进行注释。 测试B运行并使用@ContextConfiguration({"classpath:foo.xml", "classpath:bar.xml})进行注释 运行测试C,并且使用@ContextConfiguration({"classpath:foo.xml"})进行注释

当测试A运行时,将创建一个ApplicationContext,并且任何实现ApplicationContextAware或自动装配ApplicationContext的bean都可能写入静态变量。

当测试B运行时,同样的事情也会发生,并且静态变量现在指向测试B的ApplicationContext

当测试C运行时,没有bean被创建,因为测试A的TestContext(这里是ApplicationContext)被重用了。现在,您得到了一个指向另一个ApplicationContext的静态变量,而不是当前为您的测试保存bean的ApplicationContext。

其他回答

这里有一个很好的方法(不是我的,原始参考在这里: http://sujitpal.blogspot.com/2007/03/accessing-spring-beans-from-legacy-code.html

我使用过这种方法,效果很好。基本上,它是一个简单的bean,包含对应用程序上下文的(静态)引用。通过在spring配置中引用它来初始化它。

看看原来的裁判,非常清楚。

如果你使用的是web应用,还有另一种不使用单例的方式来访问应用上下文,那就是使用servletfilter和ThreadLocal。在过滤器中,您可以使用WebApplicationContextUtils访问应用程序上下文,并在TheadLocal中存储应用程序上下文或所需的bean。

警告:如果你忘记撤消ThreadLocal,你将在尝试撤消应用程序时遇到严重的问题!因此,您应该设置它,并立即开始尝试,在最后部分取消对ThreadLocal的设置。

当然,这仍然使用一个单例:ThreadLocal。但实际的豆子不需要再这样了。它甚至可以是请求范围的,如果应用程序中有多个war,并且EAR中的库也可以使用此解决方案。不过,您可能会认为ThreadLocal的这种使用与普通单例的使用一样糟糕。;-)

也许Spring已经提供了类似的解决方案?我没有找到,但我不确定。

在Spring bean中进行自动装配,如下所示:

@Autowired
private ApplicationContext appContext;

您将得到ApplicationContext对象。

我使用一种简单、标准化的方法允许外部访问我自己的任何单例Spring bean。使用这个方法,我继续让Spring实例化Bean。我是这么做的:

定义与外围类类型相同的私有静态变量。 在类的每个构造函数中将该变量设置为this。如果类没有构造函数,则添加一个用于设置变量的默认构造函数。 定义一个返回单例变量的公共静态getter方法。

这里有一个例子:

@Component
public class MyBean {
    ...

    private static MyBean singleton = null;

    public MyBean() {
        ...
        singleton = this;
    }

    ...
    
    public void someMethod() {
        ...
    }

    ...

    public static MyBean get() {
        return singleton;
    }
}

然后我可以在单例bean上调用someMethod,在我代码中的任何地方,通过:

MyBean.get().someMethod();

如果您已经子类化了您的ApplicationContext,您可以直接将这种机制添加到它中。否则,您可以子类化它来完成这个任务,或者将这个机制添加到任何可以访问ApplicationContext的bean中,然后使用它来从任何地方访问ApplicationContext。重要的是,正是这个机制让您能够进入Spring环境。

SpringApplicationContext.java

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Wrapper to always return a reference to the Spring Application 
Context from
 * within non-Spring enabled beans. Unlike Spring MVC's 
WebApplicationContextUtils
 * we do not need a reference to the Servlet context for this. All we need is
 * for this bean to be initialized during application startup.
 */
public class SpringApplicationContext implements 
ApplicationContextAware {

  private static ApplicationContext CONTEXT;

  /**
   * This method is called from within the ApplicationContext once it is 
   * done starting up, it will stick a reference to itself into this bean.
  * @param context a reference to the ApplicationContext.
  */
  public void setApplicationContext(ApplicationContext context) throws BeansException {
    CONTEXT = context;
  }

  /**
   * This is about the same as context.getBean("beanName"), except it has its
   * own static handle to the Spring context, so calling this method statically
   * will give access to the beans by name in the Spring application context.
   * As in the context.getBean("beanName") call, the caller must cast to the
   * appropriate target class. If the bean does not exist, then a Runtime error
   * will be thrown.
   * @param beanName the name of the bean to get.
   * @return an Object reference to the named bean.
   */
  public static Object getBean(String beanName) {
    return CONTEXT.getBean(beanName);
  }
}

来源:http://sujitpal.blogspot.de/2007/03/accessing-spring-beans-from-legacy-code.html