前三種測試配置文件為springboot默認的application.properties
文件。
一、@ConfigurationProperties方式
自定義配置文件
自定義配置類:PropertiesConfig.java,加載自定義配置文件實體類並生成set和get方法
注入自定義實體類,用get方法獲取數據
二、使用@Value注解方式
三、使用Environment
注入Environment 使用getProperty獲取數據
四、使用PropertiesLoaderUtils
app-config.properties
- #### 通過注冊監聽器(`Listeners`) + `PropertiesLoaderUtils`的方式
- com.battle.type=Springboot - Listeners
- com.battle.title=使用Listeners + PropertiesLoaderUtils獲取配置文件
- com.battle.name=zyd
- com.battle.address=Beijing
- com.battle.company=in
PropertiesListener.java 用來初始化加載配置文件
- 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 加載配置文件內容
- 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);
- }
- }
Applaction.java 啟動類
- 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`的方式 * * @author zyd * @throws UnsupportedEncodingException * @since JDK 1.7 */
- @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.battle.name":"zyd",
- "com.battle.address":"Beijing",
- "com.battle.title":"使用Listeners + PropertiesLoaderUtils獲取配置文件",
- "com.battle.type":"Springboot - Listeners",
- "com.battle.company":"in"}