SpringBoot 获取yml配置文件信息

1.@ConfigurationProperties获取yml配置文件信息

1.1:pom配置

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.22</version>
        </dependency>

        <!--导入配置文件处理器,配置文件绑定就会有提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

1.2:application.yml

person:
  lastName: zhangsan
  age: 18
  boss: false
  brith: 2019/12/01
  maps: {k1: v1,k2: v2}
  lists:
    - list
    - wang
  dog:
     name: 小狗
     age: 12

1.3:entiy

/**
    将配置文件中配置的每一个属性的值,映射在这个组件中
    ConfigurationProperties:告诉spring boot将本类中的所有属性和配置文件中的相关配置进行绑定:
    prefix = "person "配置文件中哪个下面的所有属性进行--映射

    只有这个组件是容器中的组件,才能使用容器提供的ConfigurationProperties功能

    注意:默认读取全局配置文件
 */
@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person {
    private String lastName;
    private Integer age;
    private boolean boss;
    private Date brith;
    private Map<String,Object> maps;
    private List<String> lists;
    private Dog dog;
    
}

1.4:测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootDempApplicationTests {

    @Autowired
    private Person person;

    @Test
    public void contextLoads() {
        System.out.println(person);
    }

}

注意:

如果是application.properties配置文件编码问题.(idea默认是utf-8)。

解决:settings--搜索(file enc)

2.获取application.properties配置文件信息

2.1:application.properties

person.last-name=沈宁
person.age=22
person.boss=false
person.brith=2019/12/01
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c,d,e,e,p
person.dog.name=宗臣
person.dog.age=18

2.2:entity

@Data
@Component
//@Validated //数据校验
public class Persons {
    //获取配置文件中的属性
    @Value("${person.last-name}")
    private String lastName;
    //直接给变量赋值
    @Value("#{11*2}")
    private Integer age;
    @Value("${person.boss}")
    private boolean boss;
    @Value("${person.brith}")
    private Date brith;
    // @Value 不支持
    private Map<String,Object> maps;
    @Value("${person.lists}")
    private List<String> lists;
    private Dog dog;
}

2.3:测试

  同1.4

2.4.@Value和@ConfigurationProperties区别

 @ConfigurationProperties@Value
功能批量注入配置文件的属性一个个指定
松散绑定(松散语法)支持不支持
spel(spring表达式语言)不支持支持
JSR303数据校验支持不支持
复杂类型封装支持不支持

根据需求使用。。

3. @PropertySource使用

3.1:自定义一个配置文件(我就全部加载了)

@PropertySource作用:加载自定义配置文件.

@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person {
    private String lastName;
    private Integer age;
    private boolean boss;
    private Date brith;
    private Map<String,Object> maps;
    private List<String> lists;
    private Dog dog;
}

 4.@Configuration(自定义配置类)

/**
 Configuration:指定当前类是一个配置类
 Bean:将方法的返回值添加到容器中
 */
@Configuration
public class MyTest {

    @Bean
    public String testData(){
        String test="我是返回值";
        return test;
    }
}

5.配置文件占位符

#随机uuid
person.last-name==宁${random.uuid}
person.age=22
person.boss=false
person.brith=2019/12/01
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c,d,e,e,p

#获取之前配置信息的值,如果没有默认值
person.dog.name=${person.hello:sss}_宗臣
#person.dog.name=${person.last-name}_宗臣
person.dog.age=18

6.Profile

Profile是spring对不同环境提供不同配置功能的支持。

6.1:多properties激活

在application.properties里配置,注销spring.profiles.active,激活自己。

#激活配置文件
spring.profiles.active=dev

6.2:yml激活

server:
  port: 8010
#这是代表激活dev块的配置,不配置自己8010
spring:
  profiles: dev
---
server:
  port: 8011
spring:
  profiles: dev
---
server:
     port: 8012

spring:
  profiles: prod

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值