java 读取配置文件

末蓝、 2023-07-15 02:49 143阅读 0赞

引入的jar文件

  1. <dependency>
  2. <groupId>org.apache.commons</groupId>
  3. <artifactId>commons-lang3</artifactId>
  4. <version>3.5</version>
  5. </dependency>

代码块: PropertiesUtil.java

  1. package com.example.demo.util;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.util.Properties;
  8. /** * @description: 读取配置文件工具类 * @create: 2020/03/15 09:41 **/
  9. public class PropertiesUtil {
  10. private static Logger log = LoggerFactory.getLogger(PropertiesUtil.class);
  11. private static Properties props;
  12. static{
  13. String filename = "application.properties";
  14. props = new Properties();
  15. try {
  16. props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(filename),"UTF-8"));
  17. } catch (IOException e) {
  18. log.error("配置文件读取异常",e);
  19. }
  20. }
  21. public static String getProperty(String key){
  22. String value = props.getProperty(key.trim());
  23. if(StringUtils.isBlank(value)){
  24. return null;
  25. }
  26. return value;
  27. }
  28. /** * 使用场景:如果忘记在配置文件中配置key,那么也不会取到null值,因为我们有个兜底的,传入了默认值value * @param key * @param defaultValue * @return */
  29. public static String getProperty(String key, String defaultValue){
  30. String value = props.getProperty(key.trim());
  31. if(StringUtils.isBlank(value)){
  32. value = defaultValue;
  33. }
  34. return value;
  35. }
  36. }

PropertiesUtil.java的使用解释:

  1. PropertiesUtil的实现要在tomcat启动的时候就要读取到application.properties里面的配置,所以需要使用static块来解决这类问题。
  2. 执行顺序:static块优于普通代码块,普通代码块优于构造代码块。static块只执行一次。在类被加载的时候执行且仅会执行一次。一般都用static块做初始化静态变量。类被java的class loader【类加载器】加载到jvm里,然后static里面的代码会被执行且只执行一次。构造代码块在构造对象的时候每次都会执行
  3. key.trim()的作用,去掉前后空格。避免配置文件中这种等号后面有空格的写法:server.servlet.context-path = /demo

配置文件application.properties

  1. server.servlet.context-path=/demo
  2. # 8080前后有空格,获取到这个值需要用trim()去前后空格
  3. server.port = 8080

代码块:测试获取配置文件中的配置

  1. package com.example.demo;
  2. import com.example.demo.util.PropertiesUtil;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. @SpringBootApplication
  9. public class DemoApplication {
  10. @RequestMapping("/hello")
  11. public void hello(){
  12. String value = PropertiesUtil.getProperty("name","刘德华");
  13. System.out.println("配置文件没有配置的key=" + value);
  14. value = PropertiesUtil.getProperty("server.servlet.context-path");
  15. System.out.println("配置文件配置的key=" + value);
  16. }
  17. public static void main(String[] args) {
  18. SpringApplication.run(DemoApplication.class, args);
  19. }
  20. }

启动项目并且访问localhost:8080/demo/hello,访问结果
配置文件没有配置的key=刘德华
配置文件配置的key=/demo

发表评论

表情:
评论列表 (有 0 条评论,143人围观)

还没有评论,来说两句吧...

相关阅读