springboot读取配置文件的方式

前三种测试配置文件为springboot默认的application.properties文件。

一、@ConfigurationProperties方式

自定义配置文件

 

自定义配置类:PropertiesConfig.java,加载自定义配置文件实体类并生成set和get方法

注入自定义实体类,用get方法获取数据 

 

二、使用@Value注解方式

 

三、使用Environment

注入Environment 使用getProperty获取数据 

 

四、使用PropertiesLoaderUtils

app-config.properties

 

[html] view plain copy
 
  1. #### 通过注册监听器(`Listeners`) + `PropertiesLoaderUtils`的方式   
  2. com.battle.type=Springboot - Listeners   
  3. com.battle.title=使用Listeners + PropertiesLoaderUtils获取配置文件   
  4. com.battle.name=zyd   
  5. com.battle.address=Beijing   
  6. com.battle.company=in  

PropertiesListener.java 用来初始化加载配置文件

[java] view plain copy
 
  1. import org.springframework.boot.context.event.ApplicationStartedEvent;  
  2. import org.springframework.context.ApplicationListener;  
  3. import com.zyd.property.config.PropertiesListenerConfig;  
  4. public class PropertiesListener implements ApplicationListener<ApplicationStartedEvent> {  
  5.     private String propertyFileName;  
  6.     public PropertiesListener(String propertyFileName) {  
  7.         this.propertyFileName = propertyFileName;  
  8.     }  
  9.     @Override public void onApplicationEvent(ApplicationStartedEvent event) {  
  10.         PropertiesListenerConfig.loadAllProperties(propertyFileName);  
  11.     }  
  12. }  

PropertiesListenerConfig.java 加载配置文件内容

[java] view plain copy
 
  1. import org.springframework.boot.context.event.ApplicationStartedEvent;  
  2. import org.springframework.context.ApplicationListener;  
  3. import com.zyd.property.config.PropertiesListenerConfig;  
  4. public class PropertiesListener implements ApplicationListener<ApplicationStartedEvent> {  
  5.     private String propertyFileName;  
  6.     public PropertiesListener(String propertyFileName) {  
  7.         this.propertyFileName = propertyFileName;  
  8.     }  
  9.     @Override public void onApplicationEvent(ApplicationStartedEvent event) {  
  10.         PropertiesListenerConfig.loadAllProperties(propertyFileName);  
  11.     }  
  12. }  

Applaction.java 启动类

[java] view plain copy
 
  1. import java.io.UnsupportedEncodingException;  
  2. import java.util.HashMap;  
  3. import java.util.Map;  
  4. import org.springframework.boot.SpringApplication;  
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8. import com.zyd.property.config.PropertiesListenerConfig;  
  9. import com.zyd.property.listener.PropertiesListener;  
  10.   
  11. @SpringBootApplication @RestController public class Applaction {  
  12.     /** * * 第四种方式:通过注册监听器(`Listeners`) + `PropertiesLoaderUtils`的方式 * * @author zyd * @throws UnsupportedEncodingException * @since JDK 1.7 */  
  13.     @RequestMapping("/listener") public Map<String, Object> listener() {  
  14.         Map<String, Object> map = new HashMap<String, Object>();  
  15.         map.putAll(PropertiesListenerConfig.getAllProperty());  
  16.         return map;  
  17.     }  
  18.     public static void main(String[] args) throws Exception {  
  19.         SpringApplication application = new SpringApplication(Applaction.class);  
  20.         // 第四种方式:注册监听器 application.addListeners(new PropertiesListener("app-config.properties")); application.run(args); } }  

访问结果:

[java] view plain copy
 
      1. {"com.battle.name":"zyd",  
      2. "com.battle.address":"Beijing",  
      3. "com.battle.title":"使用Listeners + PropertiesLoaderUtils获取配置文件",  
      4. "com.battle.type":"Springboot - Listeners",  
      5. "com.battle.company":"in"}
原文地址:https://www.cnblogs.com/wzb0228/p/10489545.html