Spring boot 读取properties文件的四种方式

Spring boot 读取properties文件的四种方式

方式一

使用@Value注解

在application.properties文件中添加属性

my.name=lisi
my.old=19

在代码中使用

@RestController
@RequestMapping(value = "/my")
public class MyController {
@Value("${my.name}")
private String name;
@Value("${my.old}")
private int old;

@RequestMapping(value = "/test3")
 public String test3() {
     return "my name is " + name + "---" + old;
 }
}

方式二

使用Environment

配置文件还是原来的配置文件

@RestController
@RequestMapping(value = "/my")
public class MyController {    
@Autowired
private Environment env;

@RequestMapping(value = "/test5")
public String test5() {
    return "my name is " + env.getProperty("my.name") + " --" + env.getProperty("my.old");
   }
}

方式三

通过@ConfigurationProperties注解,把对应的属性编写对应的配置类

@Component
@ConfigurationProperties(prefix = "my")  //如果配置文件中参数是my.test.name,则prefix=“my.test”
public class PropertiesConfig {
private String name;
private int old;

public String getName() {
    return name;
  }

public void setName(String name) {
    this.name = name;
  }

public int getOld() {
    return old;
  }

public void setOld(int old) {
    this.old = old;
  }
}
@RestController
@RequestMapping(value = "/my")
@ConfigurationProperties(prefix = "my")
public class MyController {  
private String name;
private int old;
@Autowired
private PropertiesConfig  config;

@RequestMapping(value = "/test4")
public String test4() {
    return "my name is " + config.getName() + config.getOld();
   }
}

方式四 使用PropertiesLoaderUtils

首先在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;
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值