(转) SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解
springboot采纳了建立生产就绪spring应用程序的观点。 Spring Boot优先于配置的惯例,旨在让您尽快启动和运行。在一般情况下,我们不需要做太多的配置就能够让spring boot正常运行。在一些特殊的情况下,我们需要做修改一些配置,或者需要有自己的配置属性。
一、自定义属性
当我们创建一个springboot项目的时候,系统默认会为我们在src/main/Java/resources目录下创建一个application.properties。个人习惯,我会将application.properties改为application.yml文件,两种文件格式都支持。
在application.yml自定义一组属性:
1 2 3 | my: name: forezp age: 12 |
如果你需要读取配置文件的值只需要加@Value(“${属性名}”):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @RestController public class MiyaController { @Value ( "${my.name}" ) private String name; @Value ( "${my.age}" ) private int age; @RequestMapping (value = "/miya" ) public String miya(){ return name+ ":" +age; } } |
启动工程,访问:localhost:8080/miya,浏览器显示:
forezp:12
二、将配置文件的属性赋给实体类
当我们有很多配置属性的时候,这时我们会把这些属性作为字段来创建一个javabean,并将属性值赋予给他们,比如:
1 2 3 4 5 6 7 8 | my: name: forezp age: 12 number: ${random. int } uuid : ${random.uuid} max: ${random. int ( 10 )} value: ${random.value} greeting: hi,i'm ${my.name} |
其中配置文件中用到了${random} ,它可以用来生成各种不同类型的随机值。
怎么讲这些属性赋于给一个javabean 呢,首先创建一个javabean :
1 2 3 4 5 6 7 8 9 10 11 12 13 | @ConfigurationProperties (prefix = "my" ) @Component public class ConfigBean { private String name; private int age; private int number; private String uuid; private int max; private String value; private String greeting; 省略了getter setter.... |
需要加个注解@ConfigurationProperties,并加上它的prrfix。另外@Component可加可不加。另外spring-boot-configuration-processor依赖可加可不加,具体原因不详。
1 2 3 4 5 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional> true </optional> </dependency> |
另外需要在应用类或者application类,加EnableConfigurationProperties注解。
1 2 3 4 5 6 7 8 9 10 | @RestController @EnableConfigurationProperties ({ConfigBean. class }) public class LucyController { @Autowired ConfigBean configBean; @RequestMapping (value = "/lucy" ) public String miya(){ return configBean.getGreeting()+ " >>>>" +configBean.getName()+ " >>>>" + configBean.getUuid()+ " >>>>" +configBean.getMax(); } |
启动工程,访问localhost:8080/lucy,我们会发现配置文件信息读到了。
三、自定义配置文件
上面介绍的是我们都把配置文件写到application.yml中。有时我们不愿意把配置都写到application配置文件中,这时需要我们自定义配置文件,比如test.properties:
1 2 | com.forezp.name=forezp com.forezp.age= 12 |
怎么将这个配置文件信息赋予给一个javabean呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @Configuration @PropertySource (value = "classpath:test.properties" ) @ConfigurationProperties (prefix = "com.forezp" ) public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this .name = name; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } } |
在最新版本的springboot,需要加这三个注解。@Configuration
@PropertySource(value = “classpath:test.properties”)
@ConfigurationProperties(prefix = “com.forezp”);在1.4版本需要
PropertySource加上location。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @RestController @EnableConfigurationProperties ({ConfigBean. class ,User. class }) public class LucyController { @Autowired ConfigBean configBean; @RequestMapping (value = "/lucy" ) public String miya(){ return configBean.getGreeting()+ " >>>>" +configBean.getName()+ " >>>>" + configBean.getUuid()+ " >>>>" +configBean.getMax(); } @Autowired User user; @RequestMapping (value = "/user" ) public String user(){ return user.getName()+user.getAge(); } } |
启动工程,打开localhost:8080/user;浏览器会显示:
forezp12
四、多个环境配置文件
在现实的开发环境中,我们需要不同的配置环境;格式为application-{profile}.properties,其中{profile}对应你的环境标识,比如:
- application-test.properties:测试环境
- application-dev.properties:开发环境
- application-prod.properties:生产环境
怎么使用?只需要我们在application.yml中加:
1 2 3 | spring: profiles: active: dev |
其中application-dev.yml:
1 2 | server: port: 8082 |
启动工程,发现程序的端口不再是8080,而是8082。
源码下载:https://github.com/forezp/SpringBootLearning
五、参考文献
spring-boot-reference-guide-zh
优秀文章推荐:
- 更多springboot 教程:springBoot非官方教程 | 文章汇总
- 更多springcoud 教程:史上最简单的 SpringCloud 教程 | 文章汇总
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2016-07-31 Docker容器管理