spring boot (二) 配置文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhenghuasheng/article/details/53163945

Spring Boot的默认属性配置文件是在resources下的application.properties,也可以使用application.yaml来配置默认参数。如果是使用Spring Boot自带的配置的话,配置即使用,无需再单独去声明内容。

例如:

###spring datasource###
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/sms?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull
spring.datasource.username=zhenghuasheng
spring.datasource.password=zhs2111014
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-active=10
spring.datasource.max-wait=10000
spring.datasource.max-idle=5
spring.datasource.min-idle=0
###mybatis config###
mybatis.mapper-locations=classpath:/mybatis/*Mapper.xml
mybatis.type-aliases-package=com.sample.model

Spring Boot推荐使用类型安全的方式来进行自定义配置,例如:

@Component
@ConfigurationProperties( //1
        prefix = "usetest",
        locations = "classpath:config/app.properties"
)
public class TestInfoSettings {
    private String name;
    private String age;
    //省略get、set
}

说明:
1:@ConfigurationProperties加载properties文件内的配置,prefix属性指定配置的前缀,例如:usetest.*=string 。locations可以指定自定义的文件位置,不指定默认使用application.properties。

猜你喜欢

转载自blog.csdn.net/zhenghuasheng/article/details/53163945