@ConfigurationProperties的三种使用方式

@ConfigurationProperties
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定,若进行绑定,首先这个类要受Spring管理,spring才能操作里面的数据,有三种使用场景,而通常情况下使用的最多的只是其中的一种场景。本文介绍一下三种场景的使用情况,若有错误还请各位大佬批评指正!

第一种
@Component和@ConfigurationProperties放在bean定义类上。
1、新建application.yml配置文件

person:
  id: 1
  name: 张三

2、定义一个bean

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

import java.io.Serializable;

@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person implements Serializable {
    private Integer id;
    private String name;
}

3、测试

import com.gwm.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class SpringBootConfigTest {

    @Autowired
    private Person person;
    
    @Test
    public void  test01(){
        System.out.println(person.getId() +"==="+ person.getName());
    }
}

4、结果
在这里插入图片描述

第二种
@ConfigurationProperties和@Bean注解在配置类的Bean定义方法上。
1、配置文件添加

dataSource:
  url: jdbc:mysql://localhost:3306/springboottest?allowMultiQueries=true&serverTimezone=Asia/Shanghai

2、新建配置类

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TestConfig {
	// 注意:配置文件中是大小写混合的,prefix中要全部小写,否则会报错
    @ConfigurationProperties(prefix = "datasource")
    @Bean
    public DruidDataSource duidDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource;
    }
}

3、测试

import com.alibaba.druid.pool.DruidDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class SpringBootConfigTest {
    @Autowired
    private DruidDataSource druidDataSource;

    @Test
    public void  test02(){
        System.out.println(druidDataSource.getUrl());
    }
}

4、结果
在这里插入图片描述

第三种
@ConfigurationProperties注解到普通类,然后启动类添加@EnableConfigurationProperties定义为Bean。此处以第一种的代码为例,做修改即可。
1、注释@Component注解

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

import java.io.Serializable;

//@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person implements Serializable {
    private Integer id;
    private String name;
}

2、启动类新加注解

import com.gwm.pojo.Person;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

// 注解后面要加上开启配置的类----->类名.class
@EnableConfigurationProperties({Person.class})
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

3、测试

import com.gwm.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class SpringBootConfigTest {

    @Autowired
    private Person person;
    
    @Test
    public void  test01(){
        System.out.println(person.getId() +"==="+ person.getName());
    }
}

4、结果
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@ConfigurationProperties(prefix = "")是一个注解,用于将属性文件中的配置值与Java类中的属性进行绑定。在该注解中,prefix参数指定了属性的前缀,可以在属性文件中使用该前缀来标识需要绑定的属性。通过这种方式,我们可以方便地将属性文件中的配置值注入到Java类中的对应属性中。 在上述代码中,通过在普通类上添加@ConfigurationProperties注解,并指定prefix参数为"user1",我们可以将"user1"前缀开头的属性值与User类中的属性进行匹配赋值。这样,在使用@EnableConfigurationProperties注解对User进行实例化时,就会使用@ConfigurationProperties的功能,自动将属性文件中以"user1"为前缀的配置值注入到User类对应的属性中。 在Spring Boot应用程序中,通常会在启动类上添加@EnableConfigurationProperties注解,并通过参数指定需要进行属性绑定的类。例如,在@SpringBootApplication注解下添加@EnableConfigurationProperties({User.class}),将User类作为参数传递给@EnableConfigurationProperties注解,就可以在应用程序启动时进行User类的实例化,并将属性文件中的配置值注入到User类的属性中。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Spring Boot中注解@ConfigurationProperties三种使用场景](https://blog.csdn.net/wo541075754/article/details/104575745)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值