Spring获取当前的ApplicationContext
我正在使用Spring MVC为我的Web应用程序。 我的bean是用“ spring-servlet.xml ”文件编写的 
 现在我有一个类MyClass ,我想用spring bean访问这个类 
 在spring-servlet.xml我写了以下内容 
 <bean id="myClass" class="com.lynas.MyClass" /> 
 现在我需要使用ApplicationContext访问这个 
 ApplicationContext context = ?? 
所以我可以做
 MyClass myClass = (MyClass) context.getBean("myClass"); 
这个怎么做??
简单地注入它..
 @Autowired private ApplicationContext appContext; 
或者实现这个接口: ApplicationContextAware
我认为这个链接演示了在任何地方获取应用程序上下文的最佳方式,即使在非bean类中也是如此。 我觉得它非常有用。 希望你也一样。 下面是它的抽象代码
创build一个新的类ApplicationContextProvider.java
 package com.java2novice.spring; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class ApplicationContextProvider implements ApplicationContextAware{ private static ApplicationContext context; public static ApplicationContext getApplicationContext() { return context; } @Override public void setApplicationContext(ApplicationContext ac) throws BeansException { context = ac; } } 
在application-context.xml中添加一个条目
 <bean id="applicationContextProvder" class="com.java2novice.spring.ApplicationContextProvider"/> 
获取这样的上下文
 TestBean tb = ApplicationContextProvider.getApplicationContext().getBean("testBean", TestBean.class); 
干杯!!
如果你需要从HttpServlet中访问上下文,而HttpServlet本身没有被Spring实例化 (因此@Autowire和ApplicationContextAware都不能工作)。
 WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 
要么
 SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); 
至于其他一些答复, 在你这样做之前要三思:
 new ClassPathXmlApplicationContext("..."); // are you sure? 
…因为这不会给你当前的背景,而是为你创造另一个实例。 这意味着1)大量的内存和2)豆在这两个应用程序上下文中不共享。
如果你正在实现一个没有被Spring实例化的类,比如JsonDeserializer,你可以使用:
 WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext(); MyClass myBean = context.getBean(MyClass.class); 
将此添加到您的代码中
 @Autowired private ApplicationContext _applicationContext; //Add below line in your calling method MyClass class = (MyClass) _applicationContext.getBean("myClass"); // Or you can simply use this, put the below code in your controller data member declaration part. @Autowired private MyClass myClass; 
这将简单地将myClass注入到您的应用程序中
第1步 :在课堂上注入以下代码
 @Autowired private ApplicationContext _applicationContext; 
第2步 :写Getter&Setter
第3步 :在定义了bean的xml文件中定义autowire =“byType”
基于Vivek的回答,但我认为以下会更好:
 public class ApplicationContextProvider implements ApplicationContextAware { private ApplicationContext context; public static ApplicationContext getApplicationContext() { final ApplicationContextProvider cprovider = new ApplicationContextProvider(); return cprovider.getContext(); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } ApplicationContext getContext() { return context; } } 
从实例方法写入静态字段是一个不好的做法,如果有多个实例被操纵,那么这个做法是危险的。
另一种方法是通过servlet注入applicationContext。
这是使用Spring Web服务时如何注入依赖关系的一个例子。
 <servlet> <servlet-name>my-soap-ws</servlet-name> <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class> <init-param> <param-name>transformWsdlLocations</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:my-applicationContext.xml</param-value> </init-param> <load-on-startup>5</load-on-startup> </servlet> 
另一种方法是在web.xml中添加应用程序上下文,如下所示
 <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/my-another-applicationContext.xml classpath:my-second-context.xml </param-value> </context-param> 
基本上你试图告诉servlet它应该在这些上下文文件中定义bean。
 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-servlet.xml"); 
那么你可以检索这个bean:
 MyClass myClass = (MyClass) context.getBean("myClass"); 
参考: springbyexample.org