@ConfigurationProperties注解

@ConfigurationProperties注解

  • @ConfigurationProperties将配置文件的属性映射到实体类的属性上
  • 有两个属性prefix和value,其实是一个属性可以通过两个其中一个去设值
  • 还有ignoreInvalidFields属性默认为false,可设置为true,设置为true后赋值失败将不会启动失败
  • 有三种用法

第一种

  • 使用@ConfigurationProperties与@Component一起使用,相当与前缀.属性名

配置文件

server:
  port: 9999
people:
  name: "zhangsan"
  age: 22

实体类

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(value = "people")
public class People {
    String name;
    Integer age;
}

使用实体类

@RestController
public class yige {
    @Autowired
    People people;

    @GetMapping("getName")
    public void getName(){
        System.out.println(people.getName());
        System.out.println(people.getAge());
    }
}

//结果
//zhangsan
//22

第二种

  • 和Bean一起使用,对bean的属性进行赋值

配置文件

server:
  port: 9999
people:
  name: "xucanjie"
  age: 22
student:
  name: "xucanjie"
  age: 22

实体类


import lombok.Data;

@Data
public class Student {
    String name;
    Integer age;
}

配置类

import com.xucanjie.pojo.Student;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfig {

    @Bean
    @ConfigurationProperties(prefix = "student")
    Student getStudent(){
        return new Student();
    }
}

验证


@RestController
public class yige {

    @Autowired
    People people;

    @Autowired
    Student student;
    @GetMapping("getName")
    public void getName(){
        System.out.println(people.getName());
        System.out.println(people.getAge());
        wSocket.sendMessageTo(people.getName()+people.getAge());
    }

    @GetMapping("getStudent")
    public Student getStudent(){
        return student;
    }
}

在这里插入图片描述
第三种

  • 加在实体类上不适用@Comment,把@EnableConfigurationProperties加到启动类一起使用

配置类

server:
  port: 9999
people:
  name: "xucanjie"
  age: 22
student:
  name: "xucanjie"
  age: 22
teacher:
  name: "xucanjie"
  age: 22

实体类

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;


@Data
@ConfigurationProperties(prefix = "teacher")
public class Teacher {
    String name;
    String age;
}

启动类

@SpringBootApplication
@EnableConfigurationProperties(Teacher.class)
public class XucanjieApplication {

    public static void main(String[] args) {
        SpringApplication.run(XucanjieApplication.class, args);
    }
}

验证

@RestController
public class yige {
    @Autowired
    People people;

    @Autowired
    Student student;

    @Autowired
    Teacher teacher;
    @GetMapping("getName")
    public void getName(){
        System.out.println(people.getName());
        System.out.println(people.getAge());
    }

    @GetMapping("getStudent")
    public Student getStudent(){
        return student;
    }

    @GetMapping("getTeacher")
    public Teacher getTeacher(){
        return teacher;
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值