获取springboot中的properties属性值

1.抽象BeanContext,内嵌静态方法

@Component
public class BeanContext implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        return (T) applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) throws BeansException {
        return applicationContext.getBean(clazz);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        BeanContext.applicationContext = applicationContext;
    }

2.抽象获取属性工具类ConfigUtilis

public class ConfigUtils {

    private static Environment environment = BeanContext.getApplicationContext().getEnvironment();

    public static String getProperties(String key){
        return environment.getProperty(key);
    }
}
posted @ 2021-11-12 13:31  SpecialSpeculator  阅读(478)  评论(0编辑  收藏  举报