springboot通过注解把配置文件自动映射到属性和实体类

1、直接在java类中获取配置

如在controller中添加注解
@PropertySource("classpath:application.properties")
即获取application.properties中配置信息,再通过@value注解获取配置内容

image.png

image.png

2、通过配置类进行获取配置信息

注解:
@Component
@PropertySource("classpath:application.properties")
@ConfigurationProperties
eg:配置类

@Component
@PropertySource("classpath:application.properties")
@ConfigurationProperties

public class ServerConfig {

    @Value("${server.port}")
    private  String port;
    @Value("${file.path}")
    
    private String pathx;
    
    public String getPort() {
        return port;
    }
    public void setPort(String port) {
        this.port = port;
    }
}

获取配置类中配置内容


image.png

3、通过前缀方式获取配置内容

eg:前缀为boot的配置参数


image.png
image.png

你可能感兴趣的:(springboot通过注解把配置文件自动映射到属性和实体类)