前三种测试配置文件为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"}