java的springboot项目读取properties文件属性有很多种方式,今天我们介绍通过PropertiesLoaderUtils来读取properties文件属性。我们通过两个例子来看看具体如何实现?
例子1
使用PropertiesLoaderUtils
app-config.properties
#### 通过注册监听器(`Listeners`) + `PropertiesLoaderUtils`的方式 com.zyd.type=Springboot - Listeners com.zyd.title=使用Listeners + PropertiesLoaderUtils获取配置文件 com.zyd.name=zyd com.zyd.address=Beijing com.zyd.company=in
PropertiesListener.java 用来初始化加载配置文件
package com.zyd.property.listener; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener; import com.zyd.property.config.PropertiesListenerConfig; /** * 配置文件监听器,用来加载自定义配置文件 */ 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); } }
PropertiesListenerConfig.java 加载配置文件内容
package com.zyd.property.config; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.springframework.beans.BeansException; import org.springframework.core.io.support.PropertiesLoaderUtils;
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; } }
Applaction.java 启动类
package com.zyd.property; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.zyd.property.config.PropertiesListenerConfig; import com.zyd.property.listener.PropertiesListener; @SpringBootApplication @RestController public class Applaction { /** * * 通过注册监听器(`Listeners`) + `PropertiesLoaderUtils`的方式 */ @RequestMapping("/listener") public Map<String, Object> listener() { Map<String, Object> map = new HashMap<String, Object>(); map.putAll(PropertiesListenerConfig.getAllProperty()); return map; } public static void main(String[] args) throws Exception { SpringApplication application = new SpringApplication(Applaction.class); 注册监听器 application.addListeners(new PropertiesListener("app-config.properties")); application.run(args); } }
访问结果:
{"com.zyd.name":"zyd","com.zyd.address":"Beijing","com.zyd.title":"使用Listeners + PropertiesLoaderUtils获取配置文件","com.zyd.type":"Springboot - Listeners","com.zyd.company":"in"}
例子2
首先在resources文件夹下建立app-config.properties文件里面有两个属性
my.name=zhangsan my.old=18
文件属性监听器
import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener; 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); } }
编写PropertiesListenerConfig
import org.springframework.beans.BeansException; import org.springframework.core.io.support.PropertiesLoaderUtils; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import java.util.Properties; public class PropertiesListenerConfig { public static Map 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; } }
编写完成之后需要在项目启动的时候注册监听器,修改启动的main函数
public static void main(String[] args) { SpringApplication application = new SpringApplication(DemoSpringbootApplication.class); //注册监听器 application.addListeners(new PropertiesListener("app-config.properties")); application.run(args); }
控制器类
@RestController @RequestMapping(value = "/my") public class MyController { @RequestMapping("/test6") public Map<String, Object> test6() { Map<String, Object> map = new HashMap<String, Object>(); map.putAll(PropertiesListenerConfig.getAllProperty()); return map; } }
通过以上内容我们知道了java springboot使用PropertiesLoaderUtils读取properties文件配置属性的方法。感谢您访问“我爱捣鼓(www.woaidaogu.com)”网站的内容,希望对大家有所帮助!引用本文内容时,请注明出处!谢谢合作!