获取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);
    }
}
原创:做时间的朋友
原文地址:https://www.cnblogs.com/PythonOrg/p/15544164.html