spring-boot导入配置文件属性多种方法
**
一,Spring 导入bean属性的多种方式
**
1,@Configuration bean类和测试类必须有Component注释,测试类必须用Autowired注释
2,@PropertySource (只能用于properties文件) 结合读取指定文件
专门创建指定的配置文件 *.properties
person.lasename=张三
person.age=12
person.birth=2014/12/14
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=tom
person.dog.age=14
3,@PropertySource(value={“classpath=:person.properties”})
4,@ImportResource读取外部文件
创建xml配置文件
<bean id =“helloService” class com.atguigu.com.helloService">
将@impoerReurce标注到主配置类上
5,@ImportResource (locations={“calsspath”:beans.xml})
**
二,配置文件的随机占位符
**
person.last-name=张三 ¥{random.uuid}
person.age=random.intperson.dog.name={random.int}
person.dog.name=random.intperson.dog.name={person.last-name}dog
person.dog.name=${person.hello:hello}_dog —如果没有配置person的值,那么直接将默认的hello写入
**
三,加粗样式Profile
**
多Profile文件
可以创建多多个文件 文件名可以是 application-{profile}.properties/yml,例如application.dev/prod(生产环境和开发环境 ).pproperties,默认生效的文件为application.properties文件
如何使配置文件生效,在主配置文件中激活
spring.profiles.active=dev
yml文件配置文档模块------标注端口被用于什么样的环境
server:
port: 8080
spring:
profiles:
active:dev 激活开发环境的时候,端口号变为8081
—
server:
port: 8081
spring:
profiles: dev
server:
port: 8081
spring:
profiles: prod
也可以使用命令行的模式进行激活
也可以使用打包后命令行的方式 java -jar *.jar --spring.profiles.active=dev
注意:如何SpringBoot导入bean对象为null,可能的原因就是没有注释比如: @Autowired和@Componen
- Spring Boot 配置文件 - 属性大全
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- SpringBoot如何导入自定义配置的yml文件
- SpringBoot 通过属性文件外置配置
- SpringBoot之属性配置文件详解
- Spring Boot打jar包后配置文件的外部优化配置方法
- SpringBoot如何导入自定义配置的yml文件
- SpringBoot如何导入自定义配置的yml文件
- 2、spring-boot 工程配置——配置文件详解:自定义属性、随机数、多环境配置等
- Spring Boot属性配置文件详解
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- Spring Boot属性文件配置文档(全部)
- Spring Boot属性配置文件详解
- 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】
- SpringBoot如何导入自定义配置的yml文件
- SpringBoot如何导入自定义配置的yml文件
- Spring Boot application.propertis配置文件的相关通用属性
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- spring boot-application.properties配置文件属性