/**
* <p>
* 全局获取配置文件属性,Bean对象
* </p>
*
* @author jpge
* @since 2023-05-30
*/
@Component
@Slf4j
public class ContextHelper implements ApplicationContextAware, InitializingBean {
public static ApplicationContext applicationContext;
public static Environment environment;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ContextHelper.applicationContext = applicationContext;
}
@Override
public void afterPropertiesSet() throws Exception {
if (ObjectUtils.isEmpty(ContextHelper.environment)) {
ContextHelper.environment = ContextHelper.applicationContext.getEnvironment();
}
}
public static String getProperty(String key) {
return ContextHelper.environment.getProperty(key);
}
public static <T> T getBean(String name, Class<T> type) {
return applicationContext.getBean(name, type);
}
public static <T> T getBean(Class<T> type) {
return applicationContext.getBean(type);
}
}
SpringBoot 全局获取配置文件属性,Bean对象
最新推荐文章于 2024-08-04 17:47:55 发布
ContextHelper是一个实现了ApplicationContextAware和InitializingBean接口的@Component类,用于全局获取配置文件属性和管理Bean。它静态持有ApplicationContext和Environment对象,提供了静态方法getProperty用于按需获取配置属性,以及getBean方法来根据名称或类型获取Bean实例。
摘要由CSDN通过智能技术生成