关于java:Spring-Boot-2x-基础教程配置文件详解

4次阅读

共计 3109 个字符,预计需要花费 8 分钟才能阅读完成。

Springboot 有一个全局配置文件,这个配置文件默认是 properties 文件,就是 application.properties 文件,其实还有一种文件,就是 yml 文件,只不过这种文件是须要本人创立的。这个配置文件是用来批改 Springboot 的默认配置的。

比如说当咱们集成了 start-web 之后,Springboot 默认的 web 服务器是 Tomcat,咱们能够对 Tomcat 的默认配置做一些解决。配置文件能做的事件很多,语法也很简略。

一、根底配置

当疾速入门示例中,咱们创立 Spring Boot 的工程项目时,零碎默认会为咱们在 src/main/java/resources 目录下创立一个 application.properties。集体习惯,我会将 application.properties 改为 application.yml 文件,两种文件格式都反对。

Spring Boot 的默认配置文件地位为: src/main/java/resources/application.properties

在 application.yml 自定义一组属性:

my:
 name: sanke
 age: 11

与其等价的 properties 配置如下

my.name=sanke
my.age=11

咱们须要读取配置文件的值只须要加 @Value(“${属性名}”)

@RestController
public class MyController {@Value("${my.name}")
    private String name;
    @Value("${my.age}")
    private int age;

    @RequestMapping(value = "/my")
    public String my(){return name+":"+age;}
}

启动工程,拜访:localhost:8080/my, 浏览器显示:

sanke:11

二、配置文件的属性对应实体类

当咱们有很多配置属性的时候,这样一个个承受十分繁琐 , 这个时咱们就会把这些属性作为字段来创立一个 JavaBean,并将属性值赋予给他们, 比方:

my:
 name: sanke
 age: 12
 number:  ${random.int}
 uuid : ${random.uuid}
 max: ${random.int(10)}
 value: ${random.value}
 greeting: hi,i'm  ${my.name}

其中配置文件中用到了 ${random},它能够用来生成各种不同类型的随机值。

怎么将这些属性赋于给一个 JavaBean 呢,首先创立一个 JavaBean:

@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 可加可不加。

另外须要在 利用类 或者 application 启动类,加 EnableConfigurationProperties 注解。

@EnableConfigurationProperties 注解的作用是:使应用 @ConfigurationProperties 注解的类失效。

@RestController
@EnableConfigurationProperties({MyConfig.class})
public class MyConfigController {
    @Autowired
    private MyConfig myConfig;

    @GetMapping("/myConfig")
    public String myConfig(){return myConfig.getGreeting()+">>>"+myConfig.getName()+">>>"+ myConfig.getUuid()+">>>"+myConfig.getMax();}
}

启动工程,拜访 localhost:8080/myConfig,咱们会发现配置文件信息读到了。

三、自定义配置文件

下面介绍的是咱们都把配置文件写到 application.yml 中。有时咱们不想把配置都写到 application 配置文件中,这时咱们能够自定义配置文件,比方 my.properties:

com.sanke.name=sanke
com.sanke.age=12

怎么将这个配置文件信息赋予给一个 JavaBean 呢?

@Configuration
@PropertySource(value = "classpath:my.properties")
@ConfigurationProperties(prefix = "com.sanke")
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 @ConfigurationProperties

@RestController
@EnableConfigurationProperties({MyConfig.class, User.class})
public class UserController {
    @Autowired
    private MyConfig myConfig;
    @GetMapping(value = "/user/myConfig")
    public String myConfig(){return myConfig.getGreeting()+">>>"+myConfig.getName()+">>>"+ myConfig.getUuid()+">>>"+myConfig.getMax();}

    @Autowired
    private User user;
    @GetMapping(value = "/user")
    public String user(){return user.getName()+ ":" +user.getAge();}
}

启动工程,关上 localhost:8080/user ; 浏览器会显示:

sanke:12

四、多个环境配置文件

在理论的开发过程中,咱们须要在不同环境下应用不同的配置;格局为 application-{profile}.properties,其中 {profile} 对应你的环境标识,比方:

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

怎么应用?只须要咱们在 application.yml 中加:

spring:
 profiles:
   active: test

其中 application-dev.yml:

 server:
  port: 8090

启动工程,发现程序的端口不再是 8080, 而是 8090,示意测试环境曾经胜利启用了。

正文完
 0