程序中非action获得spring容器

java类:
public class MySpringContext implements ApplicationContextAware{
  private static ApplicationContext context;//声明一个静态变量保存 
  @SuppressWarnings("static-access")
  public void setApplicationContext(ApplicationContext contex) throws BeansException {
    this.context=contex; 
  }
  public static ApplicationContext getContext(){ 
    return context; 
  }

}
 
spring配置文件中配置:
<bean class="com.tsingsoft.basedata.sendMessage.MySpringContext"></bean>

调用类调用:

ApplicationContext app =MySpringContext.getContext();


原文地址:https://www.cnblogs.com/keanuyaoo/p/3301793.html