com.fengwenyi.apistarter.utils.SpringContextUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api-spring-boot-starter Show documentation
Show all versions of api-spring-boot-starter Show documentation
Spring Boot API Starter Project
package com.fengwenyi.apistarter.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
*
* ApplicationContext 工具类
*
* @author Erwin Feng
* @since 1.0.1
*/
public class SpringContextUtils implements ApplicationContextAware {
// ApplicationContext
public static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
/**
* 获取 ApplicationContext
* @return ApplicationContext
*/
public static ApplicationContext getContext() {
Asserts.notNull(context, "获取ApplicationContext失败");
return context;
}
/**
* 根据bean名称获取bean对象
* @param beanName bean名称
* @return bean对象
*/
public static Object getBean(String beanName) {
return getContext().getBean(beanName);
}
}