Spring Boot获取applicationcontext
方案一:
package com.ding.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* 应用上下文工具类
*/
public class ApplicationContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtils.applicationContext = applicationContext;
}
/**
* 获取applicationContext
*
* @return Context
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过name获取Bean.
*
* @param name 名称
* @return bean
*/
public static Object getBean(String name) {
return getApplicationContext().getB