SpringBoot2,java语言入门书籍

我们除了可以在 Spring Boot 的配置文件中设置各个 Starter 模块中预定义的配置属性,也可以在配置文件中定义一些我们需要的自定义属性。比如在 application.properties 中添加:

blog.id=1

blog.title=SpringBoot

blog.author=fish

然后,在应用中我们可以通过@Value注解来加载这些自定义的参数,比如:

@Data

@Component

public class Blog {

@Value("${blog.id}")

private Long id;

@Value("${blog.title}")

private String title;

@Value("${blog.author}")

private String author;

}

大家有没有发现一个问题,如果一个对象属性太多,一个一个绑定到属性字段上是不是太麻烦,如何解决呢?

@Data

@Component

@ConfigurationProperties(prefix = “blog”)

public class Blog2 {

private Long id;

private String title;

private String author;

}

这里配置完还需要在 Spring Boot 启动类加上@EnableConfigurationProperties 并指明要加载哪个bean

@EnableConfigurationProperties({Blog2.class})

@SpringBootApplication

public class Chapter2Application {

public static void main(String[] args) {

SpringApplication.run(Chapter2Application.class, args);

}

}

参数间引用


在application.properties中的各个参数之间也可以直接引用来使用,就像下面的设置:

blog.id=1

blog.title=SpringBoot

blog.author=fish

blog.desc=${blog.author} wrote an article about ${blog.title}

@RestController

public class TestController {

@Autowired

private Blog2 blog2;

@RequestMapping("/test2")

public String test2() {

return blog2.toString();

}

}

@Data

@Component

@ConfigurationProperties(prefix = “blog”)

public class Blog2 {

private Long id;

private String title;

private String author;

private String desc;

}

在这里插入图片描述

自定义配置文件地址


有时候我们不希望把所有配置都放在 application.properties 里面,这时候我们可以另外定义一个,这里我明取名为 blog.properties ,路径跟也放在 src/main/resources 下面。

custom.blog.id=2

custom.blog.title=SpringBoot custom config

custom.blog.author=fish

custom.blog.desc=${blog.author} wrote an article about ${blog.title}

@RestController

public class TestController {

@Autowired

private CustomBlog customBlog;

@RequestMapping("/test3")

public String test3() {

return customBlog.toString();

}

}

@Data

@Component

@ConfigurationProperties(prefix = “custom.blog”)

@PropertySource(“classpath:blog.properties”)

public class CustomBlog {

private Long id;

private String title;

private String author;

private String desc;

}

在这里插入图片描述

外部配置


SpringBoot 可以使用命令 java -jar 命令来启动 SpringBoot 应用,该命令除了启动应用之外,还可以在命令行中来指定应用的参数,比如:java -jar 项目名.jar --server.port=8081,直接以命令行的方式,来设置 server.port 属性,另启动应用的端口设为 8081。

可以看出,命令行中连续的两个减号–就是对application.properties中的属性值进行赋值的标识。所以java -jar 项目名.jar --server.port=8081 等价于在 application.properties 中添加属性server.port=8081。

如果你怕命令行有风险,可以使用 SpringApplication.setAddCommandLineProperties(false) 禁用它。

实际上,Spring Boot 应用程序有多种设置途径,Spring Boot 能从多重属性源获得属性,包括如下几种:

  • 在您的主目录(当 devtools 被激活,则为 ~/.spring-boot-devtools.properties)中的 Devtools 全局设置属性。

  • 在测试中使用到的 @TestPropertySource 注解。

  • 在测试中使用到的 properties 属性,可以是 @SpringBootTest 和用于测试应用程序某部分的测试注解。

  • 命令行参数。

  • 来自 SPRING_APPLICATION_JSON 的属性(嵌入在环境变量或者系统属性【system propert】中的内联 JSON)。

  • ServletConfig 初始化参数。

  • ServletContext 初始化参数。

  • 来自 java:comp/env 的 JNDI 属性。

  • Java 系统属性(System.getProperties())。

  • 操作系统环境变量。

  • 只有 random.* 属性的 RandomValuePropertySource。

  • 在已打包的 jar 外部的指定 profile 的应用属性文件(application-{profile}.properties 和 YAML 变量)。

  • 在已打包的 jar 内部的指定 profile 的应用属性文件(application-{profile}.properties 和 YAML 变量)。

  • 在已打包的 jar 外部的应用属性文件(application.properties 和 YAML 变量)。

  • 在已打包的 jar 内部的应用属性文件(application.properties 和 YAML 变量)。

  • 在 @Configuration 类上的 @PropertySource 注解。

  • 默认属性(使用 SpringApplication.setDefaultProperties 指定)。

这里列表按组优先级排序,也就是说,任何在高优先级属性源里设置的属性都会覆盖低优先级的相同属性,列如我们上面提到的命令行属性就覆盖了application.properties 的属性。

在我平时工作中,用的比较多的方式是YAML、application.properties、jar命令行,其他的都很少用

优先级


我们创建一个 Spring Boot 工程时,默认 resources 目录下就有一个 application.properties 文件,可以在 application.properties 文件中进行项目配置,但是这个文件并非唯一的配置文件,在 Spring Boot 中,一共有 4 个地方可以存放 application.properties 文件。

  • 当前项目根目录下的 config 目录下

  • 当前项目的根目录下

  • resources 目录下的 config 目录下

  • resources 目录下

加载优先级:当前项目根目录下的 config 目录下 > 当前项目的根目录下 > resources 目录下的 config 目录下 > resources 目录下

也就是说,src/main/resources/config 下 application.properties 覆盖 src/main/resources

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 开源分享

下application.properties 中相同的属性

多环境配置


当应用程序需要部署到不同运行环境时,一些配置细节通常会有所不同,例如日志级别、数据源、各种密码等等,如果按照以前的做法,就是每次发布的时候替换掉配置文件,这样太麻烦了,如何解决呢?

在 Spring Boot 中多环境配置文件名需要满足 application-{profile}.properties 的格式,其中{profile}对应你的环境标识,比如:

  • application-dev.properties:开发环境

  • application-test.properties:测试环境

  • application-pre.properties:灰度环境

  • application-prod.properties:生产环境

想要使用对应的环境,只需要在application.properties中使用spring.profiles.active属性来设置,值对应上面提到的{profile},例如:spring.profiles.active=dev 就会加载 application-dev.properties 配置文件内容。

至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应配置文件中的{profile}值。如:spring.profiles.active=test就会加载application-test.properties配置文件内容。

当然您也可以用命令行启动的时候带上参数:

java -jar 项目名称.jar --spring.profiles.active=dev

application.properties

spring.profiles.active=dev

application-dev.properties

blog.id=3

blog.title=SpringBoot profiles dev config

blog.author=fish

blog.desc=${blog.author} wrote an article about ${blog.title}

application-pro.properties

blog.id=4

blog.title=SpringBoot profiles pro config

blog.author=fish

blog.desc=${blog.author} wrote an article about ${blog.title}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值