Spring Boot学习(3):Spring Boot属性配置

 

目录

前言

Spring Boot配置

一、自定义属性

二、自定义配置文件

三、参数间引用

四、默认属性配置

五、随机值配置

六、命令行参数配置

七、多环境配置

 八、配置的优先级(由高到低)

九、配置文件优先级

结束语


前言

上一篇文章讲了Spring Boot集成MyBatis,我们在application.properties中配置MyBatis,这篇文章我们就来详细的介绍Spring Boot属性配置。

Spring Boot配置

Spring Boot使用了一个全局的配置文件application.properties,放在src/main/resources目录下或者类路径的/config下。Sping Boot的全局配置文件的作用是对一些默认配置的配置值进行修改。

一、自定义属性

com.test.name=taogege
com.test.password=123456

想要使用自定义属性的时候只需要@Value("${com.test.name}")引入即可。

@Service
public class PropertyService {
    @Value("${com.test.name}")
    private String name;
    @Value("${com.test.password}")
    private String password;
    
    public String getInfo(){
        return "name:" + name + ", password:"+password;
    }
}

二、自定义配置文件

上面我们讲的自定义属性是在application.properties中配置的,那么有时候我们不想在application.properties中设置,那么我们可以自定义配置文件。在读取时可以定义对应的实体类,在类上加@ConfigurationProperties(prefix = "")前缀注解自动注入属性值。

test.name=testName
test.password=testPassword
@Component
@ConfigurationProperties(prefix = "test")
public class TestPropertyUtils {
    private String name;
    private String password;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
@Service
public class TestPropertyService {
    @Autowired
    private TestPropertyUtils testPropertyUtils;
    
    public String getInfo(){
        return "name : " + testPropertyUtils.getName() + ", password : " + testPropertyUtils.getPassword();
    }
}

三、参数间引用

Spring Boot配置参数之间,可以利用占位符${name}相互引入

com.test.name=taogege
com.test.password=123456

#利用占位符引用定义的变量
com.test.info=name : ${com.test.name} and password : ${com.test.password}

四、默认属性配置

在Spring Boot集成MyBatis中,在application.properties中配置MyBatis属性,就属于Spring Boot默认属性。

server.port=8083
server.servlet.path=/dev

spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

五、随机值配置

#随机值
spring.test.mySecret=${random.value}
spring.test.myInt1=${random.int}
spring.test.myInt2=${random.int(10)}
spring.test.myInt3=${random.int[10,20]}
spring.test.myLong=${random.long}
spring.test.myString=${random.value}
spring.test.myUuid=${random.uuid}

六、命令行参数配置

Spring Boot属性也可以在运行jar包的时候注入属性值,注入格式:--属性名=属性值

#注入端口号和自定义com.test.name属性值
java -jar demo.jar --server.port=9999 --com.test.name=test

七、多环境配置

在我们项目开发中,都需要用到多环境配置,何为多环境配置,简单的来说:项目开发中,至少有开发(dev)和生产(prod)两套环境,两套环境中所用的数据库必然不是同一个,那么就需要有多个配置,也就是所谓的多环境配置。

注意:多环境配置文件的命名规则为 "application-${active}.properties" ,当项目启动时,会将占位符${active}替换为对应的属性值。例如:spring.profiles.active=dev时,会加载application-dev.properties中的内容。

下面分别给出多环境配置时,application.properties、application-dev.properties、application-prod.properties的内容:

#系统属性,默认读取application-dev.properties中配置
#部署到生产环境上时,需要--spring.profiles.active=prod切换到application-prod.properties配置
spring.profiles.active=dev
#application-dev.properties配置文件

server.port=8083
server.servlet.path=/dev

spring.datasource.url=jdbc:mysql://localhost:3306/dev?characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#application-prod.properties配置文件

server.port=8082
server.servlet.path=/prod

spring.datasource.url=jdbc:mysql://localhost:3306/prod?characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 八、配置的优先级(由高到低)

  • 测试中的@TestPropertySource注解。
  • 测试中的@SpringBootTest#properties注解特性。
  • 命令行参数
  • SPRING_APPLICATION_JSON中的属性(环境变量或系统属性中的内联JSON嵌入)。
  • ServletConfig初始化参数。
  • ServletContext初始化参数。
  • java:comp/env里的JNDI属性
  • JVM系统属性
  • 操作系统环境变量
  • 随机生成的带random.* 前缀的属性(在设置其他属性时,可以应用他们,比如${random.long})
  • 应用程序以外的application.properties或者appliaction.yml文件
  • 打包在应用程序内的application.properties或者appliaction.yml文件
  • 通过@PropertySource标注的属性源
  • 默认属性(通过SpringApplication.setDefaultProperties指定)

这里列表按组优先级排序,也就是说,任何在高优先级属性源里设置的属性都会覆盖低优先级的相同属性,列如我们上面提到的命令行属性就覆盖了application.properties的属性。

九、配置文件优先级

  • 外置,在相对于应用程序运行目录的/congfig子目录里。
  • 外置,在应用程序运行的目录里
  • 内置,在config包内
  • 内置,在Classpath根目录

同样,这个列表按照优先级排序,也就是说,src/main/resources/config下application.properties覆盖src/main/resources下application.properties中相同的属性。

结束语

Spring Boot的属性配置就到此为止了,希望能够帮助各位博友!

猜你喜欢

转载自blog.csdn.net/qq_17450057/article/details/81094810