用过 Spring Boot 的大概都知道无论是通过 Spring Initializr 还是自己手动创建的 Spring Boot 项目,在 src/main/resources 目录下都会有一个 application.properties 或者 application.yml 文件。而我们可以通过这个文件对默认的一些配置进行修改。
1. 两者配置文件有什么不同吗?
这两种配置文件在功能上是没有任何区别的。两者相比较的话, yaml 类型的配置文件可读性更高,文件结构更清晰,所以推荐使用 yaml 类型的配置文件。
2. 自定义属性配置
除了 Spring Boot 默认提供的配置外,Spring Boot 允许我们在配置文件中配置一些自定义的属性。
## 自定义属性
neko.blog.name=NekoChips' Blog
neko.blog.url=https://chenyangjie.com.cn
创建一个类来接收这些配置属性:
@Component
public class MyBlog {
@Value("${neko.blog.name:NekoChips}")
private String name;
@Value("${neko.blog.url:https://hacpai.com}")
private String url;
// 省略setter、getter
}
测试这些属性配置是否生效:
ApplicationContext context = SpringApplication.run(NekoBootApplication.class, args);
MyBlog myBlog = (MyBlog) context.getBean("myBlog");
System.out.println(String.format("blog name: %s%nblog address: %s", myBlog.getName(), myBlog.getUrl()));
blog name: NekoChips' Blog
blog address: https://chenyangjie.com.cn
我们还可以通过 @ConfigurationProperties
的方式进行配置,Spring Boot 中大部分配置都是使用的这种方式。
在 src/main/resources 目录下新增 config 文件夹,并创建 test.properties 文件:
test.appName=NekoBootApplication
test.address=localhost
test.port=8080
test.author=NekoChips
test.version=v1.0
test.date=2020-3-24
创建 MyProperties 配置类接收属性值:
@Configuration
@ConfigurationProperties(prefix = "test")
@PropertySource("classpath:config/test.properties")
public class MyProperties {
private String appName;
private String address;
private int port;
private String author;
private String version;
private String date;
// 省略 setter、getter
}
使用
@ConfigurationProperties
注解指定配置前缀,@PropertySource
指定配置文件路径。配置类中的属性值与配置文件中的对应,即可读取对应配置。做完这些,我们还需添加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
测试属性配置是否生效:
ApplicationContext context = SpringApplication.run(NekoBootApplication.class, args);
MyProperties myProperties = (MyProperties) context.getBean("myProperties");
System.out.println(myProperties);
MyProperties{appName='NekoBootApplication', address='localhost', port=8080, author='NekoChips', version='v1.0', date='2020-3-24'}
3. Profile 配置
Profile 用于在不同的环境中使用不同的配置文件,在项目拥有多个环境配置的时候,文件名必须以 application-{profile} 的方式命名,如 开发环境:application-dev ,生产环境:application-prod。
当我们没有指定 profile 属性时,项目启动会使用默认的配置,也就是不带后缀的配置文件:
INFO 12348 --- [main] com.demo.nekoBoot.NekoBootApplication : No active profile set, falling back to default profiles: default
我们创建两个不同环境的配置文件:
## application-dev.yml 文件
server:
port: 8081
## application-prod.yml 文件
server:
port: 8082
在 application.properties 文件中指定 profile:
spring.profiles.active=dev
启动项目查看控制台输出:
INFO 8404 --- [main] com.demo.nekoBoot.NekoBootApplication : The following profiles are active: dev
......
INFO 8404 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
4. Banner 图
在我们项目启动时,控制台会有一个如下的输出:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.9.RELEASE)
这是 Spring Boot 默认提供的 banner 图,我们也可以自定义 banner 图,从源码以及配置文件中可以看出 banner 除了 txt 格式外,还可以是 image 甚至 gif 格式的。通常情况下 txt 格式的就可以了。
{
"name": "spring.banner.location",
"type": "org.springframework.core.io.Resource",
"description": "Banner text resource location.",
"defaultValue": "classpath:banner.txt"
},
......
{
"name": "spring.banner.image.location",
"type": "org.springframework.core.io.Resource",
"description": "Banner image file location (jpg or png can also be used).",
"defaultValue": "classpath:banner.gif"
},
......
仅需在 http://www.network-science.de/ascii/ 生成自己想要的内容,然后将内容复制到 src/main/resources 目录下的 banner.txt 文件中即可。
_ _ _ _____ _ _
| \ | | | | / __ \| | (_)
| \| | ___ | | __ ___ | / \/| |__ _ _ __ ___
| . ` | / _ \| |/ // _ \ | | | '_ \ | || '_ \ / __|
| |\ || __/| <| (_) || \__/\| | | || || |_) |\__ \
\_| \_/ \___||_|\_\\___/ \____/|_| |_||_|| .__/ |___/
(SpringBoot 2.1.9.RELEASE) | |
|_|
启动项目插卡控制台输出:
Connected to the target VM, address: '127.0.0.1:9260', transport: 'socket'
_ _ _ _____ _ _
| \ | | | | / __ \| | (_)
| \| | ___ | | __ ___ | / \/| |__ _ _ __ ___
| . ` | / _ \| |/ // _ \ | | | '_ \ | || '_ \ / __|
| |\ || __/| <| (_) || \__/\| | | || || |_) |\__ \
\_| \_/ \___||_|\_\\___/ \____/|_| |_||_|| .__/ |___/
(SpringBoot 2.1.9.RELEASE) | |
|_|
源码地址:https://github.com/NekoChips/SpringDemo/tree/master/01.springboot-config
关于作者:NekoChips
本文地址:https://chenyangjie.com.cn/articles/2020/04/26/1587866159546.html
版权声明:本篇所有文章仅用于学习和技术交流,本作品采用 BY-NC-SA 4.0 许可协议,如需转载请注明出处!
许可协议:
