springboot profiles.active配置多个文件_Spring Boot如何自定义配置文件,Spring Boot自定义配置文件详解...

3af0167207d9fc679dc4dab078d34616.png

springboot采纳了建立生产就绪Spring应用程序的观点。 Spring Boot优先于配置的惯例,旨在让您尽快启动和运行。在一般情况下,我们不需要做太多的配置就能够让spring boot正常运行。在一些特殊的情况下,我们需要做修改一些配置,或者需要有自己的配置属性。

自定义属性

当我们创建了SpringBoot-maven 项目后,自动生成 src/main/resources/application.properties(application.yml) 配置文件进行spring的配置

3c99e9638160e93608d29ba873f13dd9.png

配置自定义属性 :

配置服务启动的端口号 : 
 server.port=8000 (application.properties)
 server : (application.yml)
 port : 8000

读取配置文件的属性

定义普通的java对象
public class Student{
 private String name;
 
 private Integer age;
 
 private String sex;
 
 // 省略 getter setter 方法
}
@RestController
@RequestMapping("student")
public class StudentController {
 @Value("${student.name}")
 private String name;
 @Value("${student.age}")
 private Integer age;
 @RequestMapping("index1")
 public Student index1(){
 Student student = new Student();
 student.setAge(age);
 student.setName(name);
 return student;
 }
}
访问 localhost:8000/student/index1 name以及age注入进来

读取配置文件注入到javaBean中,使用ConfigurationProperties(prefix="student") 不用再使用@Value("${name}") 设置值,并且得添加一个依赖.另外需要在应用类或者application类,加EnableConfigurationProperties注解。

cb88998370c206bbd013cd3bb0729bdf.png
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
 private String name;
 private Integer age;
 private String sex; 
 // 省略 getter setter方法
}
@RestController
@RequestMapping("student")
@EnableConfigurationProperties({Student.class})
public class StudentController {
 @Autowired
 private Student student;
 @RequestMapping("index2")
 public Student index2(){
 System.out.println("0000");
 return student;
 }
}
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-configuration-processor</artifactId>
 <optional>true</optional>
 </dependency>

c043cd9a98f4efca08ab529464b405d7.png

自定义配置文件(不适用默认的application.properties)

- 新建配置文件(${random.xxx} 获取随机数)

 test.properties
 users.name=name
 users.age=${random.int}
 users.max=${random.int(20)}
 users.uuid=${random.uuid}
 users.hi=hi
 users.value=Hello,${users.name}
- 在需要注入的javaBean 上添加注解(@PropertiesSource(value="classpath:test.properties") @ConfigurationProperties(profix="users"))
 @Component
 @PropertySource("classpath:test.properties")
 @ConfigurationProperties(prefix = "users")
 public class Users {
 private String name;
 private Integer age;
 private String uuid;
 private String value;
 private Integer max;
 private String hi;
 }
- **使用自定义的 test.yml 一直没有注入成功**
- 使用 在controller中添加 @EnableConfigurationProperties({Users.class})
 @RestController
 @RequestMapping("user")
 @EnableConfigurationProperties({Users.class})
 public class UserController {
 
 @Autowired
 private Users users;
 @RequestMapping("index3")
 public Users index3(){
 return users;
 }
 }

0df91660375e02d91f25c97475f0c5be.png

- 多个环境配置文件

1. 在现实的开发环境中我们可能需要多个不同环境(开发,调试,生产)的配置文件可以使用 application-{profile}.properties进行配置如

  • application-dev.properties : 开发环境
  • application-test.properties : 测试环境
  • application-prod.properties : 生产环境

2. 使用 : 在application.properties中配置

spring.profiles.active=dev 表示启动 开发环境

3. 启动程序发现端口变成了 dev 下配置的

------本文完结-------

感谢你的阅读,如果喜欢的话**评论、转发**一下再走吧!!!

以后会有更多精彩内容呈现欢迎**关注**!!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值