Spring Boot配置文件(二)

Bertha 。 2024-02-18 19:17 131阅读 0赞

自动配置:

Spring Boot 提供了默认的配置,如默认的 Bean ,去运行 Spring 应用。它是非侵入式的,只提供一个默认实现。

大多数情况下,自动配置的 Bean 满足了现有的业务场景,但如果自动配置做的不够好,还需要覆盖配置。

Spring Boot 提供了对应用进行自动化配置。

会根据类路径中的jar包、类,为jar包里的类自动配置,这样可以极大的减少配置的数量

相比以前 XML 配置方式,很多显式方式是不需要的。大多数默认的配置足够实现开发功能,从而更快速开发。

Spring Boot 不仅从 application.properties 获取配置,还可以在程序中多种设置配置属性。按照以下列表的优先级排列:

1.命令行参数

2.java:comp/env 里的 JNDI 属性

3.JVM 系统属性

4.操作系统环境变量

5.RandomValuePropertySource 属性类生成的 random.* 属性

6.应用以外的 application.properties(或 yml)文件

7.打包在应用内的 application.properties(或 yml)文件

8.在应用 @Configuration 配置类中,用 @PropertySource 注解声明的属性文件

9.SpringApplication.setDefaultProperties 声明的默认属性

可见,命令行参数优先级最高.可以在测试或生产环境中快速地修改配置参数值,而不需要重新打包和部署应用。

自定义属性:

工程如下:

Center

maven加载的包:

  1. <!-- Spring Boot 启动父依赖 -->
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>1.5.1.RELEASE</version>
  6. </parent>
  7. <dependencies>
  8. <!-- Spring Boot Web 依赖 -->
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. </dependency>
  13. <!-- Spring Boot Test 依赖 -->
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-test</artifactId>
  17. <scope>test</scope>
  18. </dependency>
  19. <!-- Junit -->
  20. <dependency>
  21. <groupId>junit</groupId>
  22. <artifactId>junit</artifactId>
  23. <version>4.12</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-test</artifactId>
  28. <version>1.4.2.RELEASE</version>
  29. <scope>test</scope>
  30. </dependency>
  31. </dependencies>

(1)属性文件

application-dev.properties 开发环境 (以Key-Value的方式)

  1. ##\u5B66\u751F\u65B0 dev
  2. student.province=zhangsan
  3. student.city=HangZhou
  4. student.desc=dev\: I'm living in ${student.province} ${student.city}

application-prod.properties 生产环境

  1. ##\u5B66\u751F\u65B0
  2. student.province=\u5F20\u4E09
  3. student.city=BeiJin
  4. student.desc=dev\: I'm living in ${student.province} ${student.city}

application.properties (多环境配置)

  1. ##Spring Profiles Active
  2. spring.profiles.active=dev
  3. #spring.profiles.active=prod

说明: spring.profiles.active 属性指定加载哪个属性文件;

(2)实体类

  1. //用 @Component 作为 Bean 注入到 Spring 容器中。
  2. @Component
  3. @ConfigurationProperties(prefix="student")//将配置文件中以 student 前缀的属性值自动绑定到对应的字段中。
  4. //@ConfigurationProperties(prefix="home") //改成home来调用这个配置文件application.yml
  5. public class StudentBean {
  6. //setXXX and getXXX
  7. private String province; // 省份
  8. private String city; // 城市
  9. private String desc; // 描述
  10. @Override
  11. public String toString() {
  12. return "StudentBean [province=" + province + ", city=" + city
  13. + ", desc=" + desc + "]";
  14. }

(3)springBoot启动类

  1. @SpringBootApplication
  2. public class Application implements CommandLineRunner {
  3. //注入学生类
  4. @Autowired
  5. private StudentBean studentBean;
  6. /**
  7. * 程序的入口点 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
  8. *
  9. * @param args
  10. */
  11. public static void main(String[] args) {
  12. SpringApplication.run(Application.class, args);
  13. }
  14. /**
  15. *运行输出结果
  16. */
  17. @Override
  18. public void run(String... arg0) throws Exception {
  19. // TODO Auto-generated method stub
  20. System.out.println(studentBean+"\n");
  21. }
  22. }

(4)运行springboot!

从控制台中可以看出,是加载了 application-dev.properties 的属性输出:

Center 1
将 spring.profiles.active 设置成 prod,重新运行Application.java

从控制台中可以看出,是加载了 application-prod.properties 的属性输出:

Center 2
****************************************************************************************************************

其他实体类应用:

  1. @Component
  2. @ConfigurationProperties(prefix="user")
  3. public class UserBean {
  4. private Long id; // 编号
  5. private int age;// 年龄
  6. private String username;// 姓名
  7. private String uuid;// 用户 UUID

application.yml文件

说明: application.properties 配置中文值的时候,读取出来的属性值会出现乱码问题。但是 application.yml 不会出现乱码问题。

原因是,Spring Boot 是以 iso-8859 的编码方式读取 application.properties 配置文件。

  1. ## 家属性
  2. home:
  3. province: 江苏省
  4. city: 南京市
  5. desc: 我家住在${home.province}的${home.city}
  6. ## 随机属性
  7. user:
  8. id: ${random.long}
  9. age: ${random.int[1,200]}
  10. username: 软件工程师是${random.value}
  11. uuid: ${random.uuid}

注意: 键值对冒号后面,必须空一格。

说明:Spring Boot 通过 RandomValuePropertySource 提供了很多关于随机数的工具类。可以生成随机字符串、随机 int 、随机 long、某范围的随机数。

测试类:(必须放在com.hlx.springboot.entity包下哦!)如工程图所示

  1. /**
  2. * 自定义配置文件测试类
  3. *
  4. * @author Administrator
  5. *
  6. */
  7. @RunWith(SpringRunner.class)
  8. @SpringBootTest
  9. public class TestProperties {
  10. // 获得日志对象
  11. private final Logger logger = LoggerFactory.getLogger(TestProperties.class);
  12. // 注入学生类
  13. @Autowired
  14. private StudentBean studentBean;
  15. // 注入用户类
  16. @Autowired
  17. private UserBean userBean;
  18. // 测试学生
  19. @Test
  20. public void testStudent() {
  21. logger.info("\n\n"+studentBean + "\n");
  22. }
  23. // 测试用户
  24. @Test
  25. public void testUser() {
  26. logger.info("\n\n"+userBean + "\n");
  27. }
  28. }

运行效果:

Center 3

多次运行,可以发现每次输出不同 User 属性值

Center 4

发表评论

表情:
评论列表 (有 0 条评论,131人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Boot配置文件()

    自动配置: Spring Boot 提供了默认的配置,如默认的 Bean ,去运行 Spring 应用。它是非侵入式的,只提供一个默认实现。 大多数情况下,自动配置的 Be