spring boot资源文件配置读取

一般业务配置,尽量新建自己的配置文件,来读取,而不是配置在application.properties或application-*.properties或yml/yaml配置中。

application.properties或application-*.properties中如果配置了中文内容,必须得转成Unicode码,否则读取乱码。

转Unicode码可以使用jdk自带工具,cmd切换到jdk下的bin目录,打开native2ascii.exe,然后在下面输入你的中文,回车显示Unicode码,复制到配置文件中来。

整个资源文件中文切换Unicode:cmd切换到jdk下的bin目录,输入native2ascii -encoding utf-8 源文件名.properties 目标文件名.properties

springboot默认properies配置文件内部源代码使用ISO-8859-1编码,即使更改这文件属性编码也是无效的,详细可以参考这篇博文:https://blog.csdn.net/formemorywithyou/article/details/96473169

但yml/yaml中却可以配置中文,正常读取,具体源码也可参考上述地址。

而自定义配置文件,可以指定编码获取,所以,建议业务配置新建自己的配置文件存储,读取。

另外,如果同时存在application yml和properties配置文件,properties中的配置会覆盖yml中相同配置。

分三种方式,读取资源配置

1,通过@value("${key}")获取配置值

     在application.properties中,配置:demo.title=u6d4bu8bd5u6d4bu8bd5

     这里因为是springboot默认配置,所以中文要转Unicode码,不然乱码,见上面文字说明。

     然后,在test类中的对应属性上通过@value("${demo.title}")加载

    注意,如果配置在自定义文件中,还需要指定资源文件位置,这里是直接配置在application.properties默认配置中的。

@RunWith(SpringRunner.class)
@SpringBootTest
public class applicationTest {
    @Value("${demo.title}")
    private String title;
    @Test
    public void testProperties()throws Exception{
        System.out.println(title);
    }
}

2,通过key的前缀,统一获取配置值

   继续读取【在application.properties中,配置:demo.title=u6d4bu8bd5u6d4bu8bd5】

   新建个TestPropertis类,类头部通过注解@ConfigurationProperties(prefix="demo")设置properties key的前缀

   类内部通过属性key后缀来获取值

  

@Component
@ConfigurationProperties(prefix="demo") public class TestPropertis { private String title; //setter getter... }

注意:使用注解@ConfigurationProperties需要引入依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
   <optional>true</optional>
</dependency>

在读取的类中通过@Resource引入TestProperties

@RunWith(SpringRunner.class)
@SpringBootTest
public class applicationTest {
  @Resource
    private TestPropertis testPropertis;
    @Test
    public void testProperties2()throws Exception{
        System.out.println("TEST----------"+testPropertis.getTitle());
    }
}

3、自定义配置文件,通过key前缀统一获取

在src/main/resources新建一个demo.properties,文件属性确保UTF-8

在文件中配置示例:

demo.name=小小灰
demo.sex=M

然后新建一个类:TestDemoPropertis,类头部加入3个注解

@Component     //生成实例,方便springboot调用

@ConfigurationProperties(prefix="demo")    //统一设置资源文件key的前缀  //类中的每个字段为key的对应后缀  

@PropertySource(value={"classpath:demo.properties"},encoding="utf-8")  

//指定配置资源文件位置,这里是string数组,可以多个

//encoding必须加上,如果要读取中文,不然乱码,因为springboot源码加载properties是以ISO-8859-1读取的

@Component
@ConfigurationProperties(prefix="demo")
@PropertySource(value={"classpath:demo.properties"},encoding="utf-8")
public class TestDemoPropertis {

    private String name;
    private String sex;
    //setter getter...
}

注意:使用@ConfigurationProperties要引入依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
   <optional>true</optional>
</dependency>

最后,同上一种在类中通过Resource引入TestDemoPropertis

@RunWith(SpringRunner.class)
@SpringBootTest
public class applicationTest {
    @Resource
    private TestDemoPropertis testDemoPropertis;
    @Test
    public void testProperties3()throws Exception{
        System.out.println("TEST DEMO----------"+testDemoPropertis.getName()+"-----"+testDemoPropertis.getSex());
    }
}
原文地址:https://www.cnblogs.com/ixixi/p/11686685.html