好玩Spring之获取ApplicationContext的几种方式

// 方式一:通过 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接口。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值