// 方式一:通过 AnnotationConfigApplicationContext / FileSystemXmlApplicationContext / ClassPathXmlApplicationContext
AnnotationConfigApplicationContext annotationConfigApplicationContext =
new AnnotationConfigApplicationContext(Config.class);
MyClass myClass = annotationConfigApplicationContext.getBean("myClass", MyClass.class);
System.out.println(myClass);
// 方式二:实现ApplicationContextAware接口
ApplicationContext applicationContext = ContextHolder.getApplicationContext();
myClass = applicationContext.getBean("myClass", MyClass.class);
System.out.println(myClass);
// 方式三:继承ApplicationObjectSupport类,
// ApplicationObjectSupport也是实现了ApplicationContextAware接口
myClass = AppSupport.getBean("myClass");
System.out.println(myClass);
// 方式四:继承WebApplicationObjectSupport类,
// 该类又继承了ApplicationObjectSupport类
// 这种方式,必须在web容器下才可以取到
myClass = WebAppSupport.getBean("myClass");
System.out.println(myClass);
// 方式五:利用@Autowired / @Resource 注解
AutoContext autoContext = annotationConfigApplicationContext.getBean("autoContext", AutoContext.class);
myClass = autoContext.getBean("myClass");
System.out.println(myClass);
// 方式六:通过ContextLoader.getCurrentWebApplicationContext
// springboot下,这种方式获取不到
// 参考:https://www.cnblogs.com/zhuzhen/p/8582504.html
WebApplicationContext currentWebApplicationContext = ContextLoader.getCurrentWebApplicationContext();
myClass = (MyClass) currentWebApplicationContext.getBean("myClass");
System.out.println(myClass);
// 方式七:通过WebApplicationContextUtils.getWebApplicationContext
// 这种方式,必须在web容器下才可以取到
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
myClass = (MyClass) ctx.getBean("myClass");
System.out.println(myClass);
方式二,ContextHolder类的代码为:
@Component
public class ContextHolder implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return context;
}
}
方式三,AppSupport类的代码为:
@Component
public class AppSupport extends ApplicationObjectSupport {
private static ApplicationContext context;
@PostConstruct
public void getContext() {
context = super.getApplicationContext();
}
public static <T>T getBean(String beanName){
return (T) context.getBean(beanName);
}
}
方式四,WebAppSupport类的代码:
@Component
public class WebAppSupport extends WebApplicationObjectSupport {
private static ApplicationContext context;
@PostConstruct
public void getContext() {
context = super.getWebApplicationContext();
}
public static <T>T getBean(String beanName){
return (T) context.getBean(beanName);
}
}
方式五
@Component
public class AutoContext {
@Resource
private ApplicationContext ctx;
public <T>T getBean(String beanName){
return (T) ctx.getBean(beanName);
}
}
方式一/二/三/五,可以在非web环境中取到ApplicationContext。
方式四/六/七,可以在web环境中取到ApplicationContext。
方式四,在默认的springboot中取不到ApplicationContext,参考注释的解答。
还是更推荐方式二(实现ApplicationContextAware接口)来获取ApplicationContext,不管是什么环境都通用,而且方式三和方式四,本身也是实现的ApplicationContextAware接口。