SpringBoot读取配置文件

简介:讲解使用@value注解配置文件自动映射到属性和实体类

3种读取properties配置文件中数据的方法

解决配置文件读取中文乱码

配置文件application.properties加载 ,解决配置文件读取中文乱码

#解决中文乱码
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8

test.name = 测试
方式一 使用@Value注解读取

​ 1、Controller上面配置
​ @PropertySource({“classpath:resource.properties”})
​ 2、增加属性
​ @Value("${test.name}")
​ private String name;

package com.beiluo.demo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 微信配置类
 */
@Configuration
@PropertySource(value="classpath:application.properties")
public class WeChatConfig {

    @Value("${wxpay.appid}")
    private String appId;

    @Value("${wxpay.appsecret}")
    private String appsecret;

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAppsecret() {
        return appsecret;
    }

    public void setAppsecret(String appsecret) {
        this.appsecret = appsecret;
    }
}

方式二:实体类配置文件 使用@ConfigurationProperties注解读取

指定配置文件,@PropertySource可以声明多个。
或者使用@PropertySources(@PropertySource(“xxx”),@PropertySource(“xxx”))

​ 步骤:
​ 1、添加 @Component 注解 表示将该类标识为Bean;
​ 2、使用 @PropertySource(value = “config.properties”) 注解 表示配置文件路径,指定配置文件位置;
​ 3、使用 @ConfigurationProperties(prefix = “demo”) 注解 用于绑定属性,其中prefix表示所绑定的属性的前缀,设置相关属性。

​ 4、必须 通过注入IOC对象Resource 进来 , 才能在类中使用获取的配置文件值。
​ @Autowired
​ private ServerSettings serverSettings;

​ 例子:

package com.beiluo.demo.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * 服务器配置
 */

@Component
@PropertySource({"classpath:application.properties"})
//@ConfigurationProperties(prefix = "test") 在上面配置前缀为test,下面在注入的时候可以不写@Value
@ConfigurationProperties
public class ServerSettings {


    // @ConfigurationProperties不加前缀,直接属性名
    @Value("${test.name}")
    private String name;


    public String getName() {
        return name;
    }
}

  @Autowired
    private ServerSettings serverSettings;


    @RequestMapping("testValue")
    public String testValue(){

        System.out.println("测试Value:"+serverSettings.getName());
        return serverSettings.getName();
    }

方式 三:使用Environment读取
    @Autowired
    private Environment environment;

    @RequestMapping("/testEnv")
    public String testEnv(){
        System.out.println("测试Environment:"+environment.getProperty("test.name"));
        return environment.getProperty("test.name");
    }

常见问题:

1、配置文件注入失败,Could not resolve placeholder
解决:根据springboot启动流程,会有自动扫描包没有扫描到相关注解,
默认Spring框架实现会从声明@ComponentScan所在的类的package进行扫描,来自动注入,
因此启动类最好放在根路径下面,或者指定扫描包范围
spring-boot扫描启动类对应的目录和子目录
2、注入bean的方式,属性名称和配置文件里面的key一一对应,就用加@Value 这个注解
如果不一样,就要加@value("${XXX}")

3 、乱码解决 我们application.properties做如下配置

server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8

然后修改IntelliJ IDEA,File --> Settings --> Editor --> File Encodings ,将最下方Default encoding for properties files设置为UTF-8,并勾选Transparent native-to-ascii conversion。

参考博客
https://www.cnblogs.com/wangjunwei/p/11401360.html

posted @ 2020-04-29 17:04  我有满天星辰  阅读(28)  评论(0编辑  收藏  举报