码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot学习(三)配置文件详解

时间:2020-05-29 23:27:29      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:方式   代码   对象   java   习惯   log   target   一个个   手动   

正如所说,SpringBoot使用“习惯优于配置”,即项目中存在大量配置,此外还内置了一个习惯性的配置,让你无需手动进行配置,这样,便让你的项目快速运行起来,而如何开启项目中的各个功能模块的默认配置,这就用到了SpringBoot的全局配置文件。

配置文件

在上一篇SpringBoot学习(一)第一个SpringBoot项目这篇文章中讲到,在创建SpringBoot项目的时候,核心配置文件为application.properties,实际上配置文件有两种格式:

  • application.properties
  • application.yml

而创建SpringBoot项目的时候,会默认的配置文件格式为.properties的,而这两种格式的文件有有何区别,举个例子:

application.properties

# port
server.port = 8081

application.yml

# port
server:
  port: 8082

在上面的例子可以看到,以自定义端口号为例,两种配置文件的结构都不同,application.yml文件的数据结构是树形的,并且properties文件是以.作为分隔符,yml文件是以:作为分隔符,这些便是这两个文件的区别,但是两者都可以作为SpringBoot配置文件,本文将对application.properties文件做个详细的介绍。

application.properties介绍

1)自定义属性

application.properties除了可以修改默认配置,还可以配置自定义属性,并在实体bean中加载出来。

application.properties

# port
server.port = 8081

# 自定义属性
com.userName = isKylin
com.userPassword = 123456

方式一:@Value

@RestController
@Slf4j
public class ConfigurationController {
    /**
     * 方式一,使用@Value注解
     */
    @Value("${com.userName}")
    private String userName;
    @Value("${com.userPassword}")
    private String userPassword;

    @RequestMapping("valueTest")
    public String valueTest() {
        log.info("@Value打印结果为:" + userName + "," + userPassword);
        return "success";
    }
}

运行结果:

技术图片

可以看到,控制台打印了配置文件中的自定义属性值。

方式二:@ConfigurationProperties

这是一种官方推荐的用法,通过该注解,绑定一个对象的bean,只需要在注解中指明前缀便可以读取自定义属性,避免了有时间属性太多,一个个绑定在属性字段上工作量会很大。

User.java

@Data
@Component
@ConfigurationProperties(prefix = "com")
public class User {
    private String userName;
    private String userPassword;
}

在没加@Component这个注解的时候,代码会提示错误,原因是只有使用这个注解,声明这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能。

UserController.java

    @Autowired
    User user;

    @RequestMapping("configurationPropertiesTest")
    public String configurationPropertiesTest() {
        log.info("@ConfigurationProperties打印结果为:" + user.getUserName() + "," + user.getUserPassword());
        return "success";
    }

技术图片

2)参数间引用

这部分将演示通过application.properties引用test.properties中的参数属性值

application.properties

# 参数间引用
com.quote = ${test.userName} , ${test.userPassword}

test.properties

# 被引用的参数属性值
test.userName = isKylin
test.userPassword = 123456

现在,两个配置文件的内容都写好了,这时候,只需要在实体类中添加@PropertySource注解,便可以将test.properties中的参数属性值引用到application.properties中:

User.java

@Data
@Component
//@ConfigurationProperties(prefix = "com")
@ConfigurationProperties(prefix = "test")
@PropertySource("classpath:test.properties")
public class User {
    private String userName;
    private String userPassword;
}

UserController.java

    /**
     * 参数间引用
     */
    @RequestMapping("quoteTest")
    public String quoteTest() {
        log.info("参数间引用打印结果为:" + user.getUserName() + "," + user.getUserPassword());
        return "success";
    }

运行结果:

技术图片

配置文件优先级说明

在SpringBoot项目中,配置文件优先级从大到小的排序为:

  1. target/config
  2. target
  3. classpath/config
  4. classpath

此外,如果你在相同优先级位置同时有application.properties和application.yml,那么application.yml里面的属性就会覆盖application.properties里的属性。

Profile多环境配置

在正常的开发中,会有多个环境,例如测试环境、开发环境、生成环境、联调环境等等,在每个环境下,可能配置的信息也会有所不同,因此就需要我们配置多环境配置文件。

技术图片

我们分别修改配置文件端口号,并且自定义不同属性,运行不同的配置文件看输出内容:

application-dev.properties

server.port = 8888
msg = this is application-dev.properties

application-test.properties

server.port = 8880
msg = this is application-test.properties

application-prod.properties

server.port = 8881
msg = this is application-prod.properties

application.properties

# port
server.port = 8081

# 自定义属性
com.userName = isKylin
com.userPassword = 123456

# 参数间引用
com.quote = ${test.userName} , ${test.userPassword}

# 多环境配置
spring.profiles.active = prod
msg = this is default

ProfilesController.java

@RestController
@Slf4j
public class ProfilesController {

    @Value("${msg}")
    private String msg;

    @RequestMapping("profilesTest")
    public String profilesTest() {
        log.info(msg);
        return msg;
    }
}

在默认的配置文件中,通过spring.profiles.active将配置文件设置成了application-prod.properties,在项目没其他任何报错的情况下,运行的端口会在8881,并且打印this is application-prod.properties,其他配置文件同理。

运行结果:

技术图片

SpringBoot学习(三)配置文件详解

标签:方式   代码   对象   java   习惯   log   target   一个个   手动   

原文地址:https://www.cnblogs.com/isKylin/p/12989873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!