在 上一节中,我们用 Spring Boot实现了一个简单的Web应用,但如果将这个项目作为生产级的项目,显然是不足够的。接下来,在本节中,笔者将带着大家一起来学习,如何在 Spring Boot中添加一些配置信息。
自定义配置
Spring Boot针对添加配置信息这块,一共提供了2种方案。
配置文件
-
在./resources中,新增application.yml(application.properties),添加如下配置
my : name : heheda message : Hello
-
新增主函数
package com.boot.demo; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class BootApplication { @Value("${my.name}") private String name; @Value("${my.name}") private String message; public static void main(String[] args) { SpringApplication.run(BootApplication.class,args); } @GetMapping("/") public String hello(){ return String.format("Hello %s,this is your message : %s",name,message); } }
-
运行主函数,访问http://localhost:8080/,结果如下
命令行配置
-
点击
Edit Configuration
-
在
Program arguments
中添加如下启动参数--my.message="commend line init"
-
运行主函数,访问http://localhost:8080/,结果如下
tips:从以上两个例子中,我们可以知道命令行中添加的配置是优先于配置文件中的。
@ConfigurationProperties
@Value这个注解可以很方便的将我们所需的配置信息给注入进正确位置,但是假如我们存在很多配置信息需要注入时,如果用**@Value注入的话,代码看上去就会非常恶心。优秀的Spring Boot**框架给我们提供了另一种配置注入的方法,可以让我们避免看到以上这种恶心的代码。
-
添加如下依赖
compileOnly "org.springframework.boot:spring-boot-configuration-processor"
-
在com.boot.demo.config.properties下新增MyProperties类
package com.boot.demo.config.properties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties("my") public class MyProperties { private String name; private String message; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
-
修改BootApplication
package com.boot.demo; import com.boot.demo.config.properties.MyProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class BootApplication { @Autowired private MyProperties myProperties; public static void main(String[] args) { SpringApplication.run(BootApplication.class,args); } @GetMapping("/") public String hello(){ return String.format("Hello %s,this is your message : %s",myProperties.getName(),myProperties.getMessage()); } }
-
运行主函数,访问http://localhost:8080/,结果如下
多环境配置
在实际开发中,我们往往会根据环境的不同,选择不同的配置属性。以前在SpringMVC的项目中,我们用的是profile这个属性来选择加载不同的配置文件,同理,我们在Spring Boot中,也是用这个属性来选择加载不同的配置。
-
在./resources目录下新增application-dev.yml
server: servlet: context-path: '/dev'
-
在application.yml中,添加如下配置
spring: profiles: active: dev
-
运行主函数,访问http://localhost:8080/,结果如下
-
访问http://localhost:8080/dev/,结果如下