【SpringBoot】第3章 SpringBoot的系统配置

3.1 系统配置文件

3.1.1 application.properties

SpringBoot支持两种不同格式的配置文件,一种是Properties,一种是YML。

SpringBoot默认使用application.properties作为系统配置文件,项目创建成功后会默认在resources目录下生成application.properties文件。该文件包含SpringBoot项目的全局配置。

我们可以在application.properties文件中配置SpringBoot支持的所有配置项,比如端口号、数据库连接、日志、启动图案等。

#服务器端口配置

server.port = 8081

 

#thymeleaf 模板

spring.thymeleaf.prefix=classpath:/templates/

spring.thymeleaf.suffix=.html

spring.thymeleaf.mode=HTML

spring.thymeleaf.encoding=UTF-8

spring.thymeleaf.servlet.content-type=text/html

以上示例将thymeleaf模板相关的配置放在一起,清晰明了,从而便于快速找到thymeleaf的所有配置。

 

3.修改默认配置文件名

new SpringApplicationBuilder(ApplicationDemo.class).properties("spring.config.location=classpath:/application.propertie").run(args);

 

3.2 自定义配置项

3.2.2 Environment

@Autowired

private Environment env;

@Test

void getEnv(){

      System.out.println(env.getProperty("com.weiz.costum.firstname"));

System.out.println(env.getProperty("com.weiz.costum.secondname"));

}

 

3.2.3 @ConfigurationProperties

在实际项目开发中,需要注入的配置项非常多时,前面所讲的@value和Environment两种方法就会比较繁琐。这时可以使用注解@ConfigurationProperties将配置项与实体Bean关联起来,实现配置项与实体类字段的关联,读取配置文件数据。下面通过示例演示@ConfigurationProperties注解如何读取配置文件。

1.创建自定义配置文件

在resources下创建自定义的website.properties配置文件,示例代码如下:

com.weiz.resource.name=weiz

com.weiz.resource.website=www.weiz.com

com.weiz.resource.language.=java

在上面的示例中,创建了自定义的website.properties配置文件,增加了name、website、language等三个配置项,这些配置项的名称的前缀都是com.weiz.resource。

 

2.创建实体类

创建WebSiteProperties自定义配置对象类,然后使用@ConfigurationProperties注解将配置文件中的配置项注入到自定义配置对象类中,示例代码如下:

@Configuration

@ConfigurationProperties(prefix = "com.weiz.resource")

@PropertySource(value = "classpath:website.properties")

public class WebSiteProperties{

       private String name;

       private String website;

       private String language;

       public String getName(){

                  return name;

}

         public void setName(String name){

        this.name=name;

}        

        public String getWebsite(){

        return website;

}

        public void setWebsite(String website){

       this.website=website;

}

       public String getLanguage(){

      return language;

}

      public void setLanguage(String language){

     this.language=language;

}

}

从上面的示例代码可以看到,我们使用了@Configuration注解、@ConfigurationProperties和@PropertySource三个注解来定义WebSiteProperties实体类:

1)@Configuration定义此类为配置类,用于构建bean定义并初始化到Spring容器。

2)@ConfigurationProperties(prefix="com.weiz.resource")绑定配置项,其中prefix表示所绑定的配置项的前缀。

3)@ProperSource(value="classpath:website.properties")指定读取的配置文件及其路径。

@PropertySource不支持引入YML文件。

通过上面的WebSiteProperties类即可读取全部对应的配置项。

3.调用配置项

使用配置实体类中的方式也非常简单,只需将WebSiteProperties注入到需要使用的类中,示例代码如下:

@Autowired

private WebSiteProperties website;

@Test

void getProperties(){

     System.out.println(website.getName());

    System.out.println(website.getWebsite());

    System.out.println(website.getLanguage());

}

 

 

相关推荐

  1. SpringBoot3 SpringBoot系统配置

    2024-07-22 07:04:05       16 阅读
  2. SpringBoot3 系统配置之日志配置

    2024-07-22 07:04:05       15 阅读
  3. springboot3.2 RedisCacheManager配置

    2024-07-22 07:04:05       16 阅读
  4. redisspringboot配置

    2024-07-22 07:04:05       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 07:04:05       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 07:04:05       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 07:04:05       45 阅读
  4. Python语言-面向对象

    2024-07-22 07:04:05       55 阅读

热门阅读

  1. Python中with 关键字、tell() 和 seek() 方法

    2024-07-22 07:04:05       17 阅读
  2. 初识数据结构中的“栈”

    2024-07-22 07:04:05       17 阅读
  3. 44、PHP 实现数据流中的中位数(含源码)

    2024-07-22 07:04:05       16 阅读
  4. Python面试题:Python中的单例模式及其实现

    2024-07-22 07:04:05       18 阅读
  5. JVM 中的OopMap与安全点

    2024-07-22 07:04:05       17 阅读
  6. 在 Ubuntu 22.04/20.04 安装 CVAT 和 SAM 指南

    2024-07-22 07:04:05       19 阅读
  7. C++多线程编程中的锁详解

    2024-07-22 07:04:05       18 阅读
  8. 生成对抗网络(GAN):目标检测的新前沿

    2024-07-22 07:04:05       15 阅读
  9. 机器学习浅讲

    2024-07-22 07:04:05       16 阅读
  10. 动态内存规划

    2024-07-22 07:04:05       17 阅读