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

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


当前回答

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

其他回答

如果需要访问容器的对象是容器中的bean,则只需实现BeanFactoryAware或ApplicationContextAware接口。

如果容器外部的对象需要访问容器,我为spring容器使用了标准的GoF单例模式。这样,您的应用程序中只有一个单例bean,其余的都是容器中的单例bean。

我使用一种简单、标准化的方法允许外部访问我自己的任何单例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环境。

看一下ContextSingletonBeanFactoryLocator。它提供静态访问器来获取Spring的上下文,假设它们已经以某种方式注册。

它并不漂亮,而且可能比您想要的还要复杂,但它是有效的。

在Spring应用程序中有许多获取应用程序上下文的方法。这些因素如下:

通过ApplicationContextAware: 进口org.springframework.beans.BeansException; 进口org.springframework.context.ApplicationContext; 进口org.springframework.context.ApplicationContextAware; 公共类AppContextProvider实现了ApplicationContextAware { private ApplicationContext; @Override setApplicationContext(ApplicationContext)抛出BeansException { 这一点。applicationContext = applicationContext; } }

这里setApplicationContext(ApplicationContext ApplicationContext)方法将获得ApplicationContext

ApplicationContextAware: 接口,由希望被通知的任何对象实现 它运行的ApplicationContext的。实现此接口 例如,当一个对象需要访问一组 合作bean。

通过Autowired的: @ autowired private ApplicationContext;

这里@Autowired关键字将提供applicationContext。自动连线有一些问题。这将在单元测试时产生问题。

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

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

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

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