springboot加载配置文件的方法

springboot加载配置文件的方法

第一种方法:用PropertiesLoaderUtils进行加载,然后用map集合经行接收
Properties properties = PropertiesLoaderUtils.loadAllProperties("sql.properties");
Map<String, String> propertiesMap = new HashMap<String, String>();
      for (Object key : properties.keySet()) {
          String keyStr = key.toString();
          try {
              // PropertiesLoaderUtils的默认编码是ISO-8859-1,在这里转码一下
              propertiesMap.put(keyStr, new String(properties.getProperty(keyStr).getBytes("ISO-8859-1"), "utf-8"));
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (java.lang.Exception e) {
              e.printStackTrace();
          }
      }
2.第二种方法:因为配置文件通常同时全局的,可以通过PropertiesListener的监听经行启动的时候就获取到。可以避免每调用一次方法就加载一次

1.编写一个配置文件监听的类PropertiesListener
每当ApplicationContext发布ApplicationEvent时,ApplicationListener Bean将自动被触发

public class PropertiesListener implements ApplicationListener<ApplicationStartedEvent> {
    private String propertyFileName;
    public PropertiesListener(String propertyFileName) {
        this.propertyFileName = propertyFileName;
    }
    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        PropertiesListenerConfig.loadAllProperties(propertyFileName);
    }
}

2.在编写一个配置文件的加载类PropertiesListenerConfig

public class PropertiesListenerConfig {
    public static Map<String, String> propertiesMap = new HashMap<>();
    
    private static void processProperties(Properties props) throws BeansException {
        propertiesMap = new HashMap<String, String>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            try {
                // PropertiesLoaderUtils的默认编码是ISO-8859-1,在这里转码一下
                propertiesMap.put(keyStr, new String(props.getProperty(keyStr).getBytes("ISO-8859-1"), "utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (java.lang.Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void loadAllProperties(String propertyFileName) {
        try {
            Properties properties = PropertiesLoaderUtils.loadAllProperties(propertyFileName);
            processProperties(properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getProperty(String name) {
        return propertiesMap.get(name).toString();
    }

    public static Map<String, String> getAllProperty() {
        return propertiesMap;
    }
}

3.在springboot的启动类上面添加监听

public static ApplicationContext applicationContext;

      public static void main(String[] args) {
		  SpringApplication application = new SpringApplication(WebApp.class);
	       // 第四种方式:注册监听器
          application.addListeners(new PropertiesListener("sql.properties"));
          applicationContext =  application.run(args);
     }
 }

4.在方法上调用通过PropertiesListenerConfig.getAllProperty();调用

Map<String, String> allProperty = PropertiesListenerConfig.getAllProperty();
发布了18 篇原创文章 · 获赞 3 · 访问量 1293

猜你喜欢

转载自blog.csdn.net/qq_22899129/article/details/102583795