您的位置:首页 > 编程语言 > Java开发

spring-boot导入配置文件属性多种方法

2019-08-21 10:46 344 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/weixin_39846205/article/details/99943335

**

一,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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: