NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
##### 获取spring 的bean 方法总结 一. 实现ApplicationContextAware\(适用于springboot\) ``` @Configuration public class AppContextUtil implements ApplicationContextAware { private static ApplicationContext ctx; public static Object getBean(String name) { return ctx.getBean(name); } public static void setCtx(ApplicationContext ctx) { AppContextUtil.ctx = ctx; } @Override public void setApplicationContext(ApplicationContext context) throws BeansException { AppContextUtil.ctx = context; } } ``` 二. 获取ApplicationContext的方法 ``` ApplicationContext applicationContext = new FileSystemXmlApplicationContext("applicationContext.xml"); ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); ``` 三. 继承自抽象类ApplicationObjectSupport 四. 继承自抽象类WebApplicationObjectSupport