@ConfigurationProperties注入属性

@ConfigurationProperties注入属性

概述

获取到了自定义属性还是官方的属性,都通过==@Value(“${key}”)==来获取。除了这种方式外,springboot还提供一种 @ConfigurationProperties 属性注入的机制。这也是如下配置的原理

# 环境激活
spring:
  profiles:
    active: dev

分析

@Value(“${key}”) 存在的问题:不具有面向对象的特征。比如:

#自定义属性
ksd:
  weixin:
    appid: 456453sdfsd52342
    mcid: 48878787
    callbackurl: https://www.baidu.com
    apisecret: SDFLKSDJFKLSJKLJ23423423

@ConfigurationProperties :一种具有面向对象特种的属性注入的方式,如何做到呢?如下:

定义一个属性配置类:
package com.example.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@ConfigurationProperties(prefix = "weixin")
@EnableConfigurationProperties(WeixinPayProperties.class)
@Configuration
public class WeixinPayProperties {

//#自定义属性
//weixin:
//    appId: 456453sdfsd52342
//    mcId: 48878787
//    callbackUrl: https://www.baidu.co
//    apiScret: SDFLKSDJFKLSJKLJ23423423

    private String appId;
    private String mcId;
    private String callbackUrl;
    private String apiSecret;

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getMcId() {
        return mcId;
    }

    public void setMcId(String mcId) {
        this.mcId = mcId;
    }

    public String getCallbackUrl() {
        return callbackUrl;
    }

    public void setCallbackUrl(String callbackUrl) {
        this.callbackUrl = callbackUrl;
    }

    public String getApiSecret() {
        return apiSecret;
    }

    public void setApiSecret(String apiSecret) {
        this.apiSecret = apiSecret;
    }
}

在这里插入图片描述

注册配置类到springboot中:

在这里插入图片描述

springboot加载这个属性配置类以及完成属性注入:

@EnableConfigurationProperties(WeixinPayProperties.class)
@Configuration
如何属性配置类:
package com.example.service;

import com.example.config.WeixinPayProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class WeiXinPayService {

    @Autowired
    private WeixinPayProperties weixinPayProperties;

//    @Value("${weixin.appId}")9
//    private String appId;
//    @Value("${weixin.mcId}")
//    private String mcId;
//    @Value("${weixin.callbackUrl}")
//    private String callbackUrl;
//    @Value("${weixin.apiSecret}")
//    private String apiSecret;


    public void testValue(){
        System.out.println(weixinPayProperties.getAppId());
        System.out.println(weixinPayProperties.getMcId());
        System.out.println(weixinPayProperties.getCallbackUrl());
        System.out.println(weixinPayProperties.getApiSecret());
    }
}
打印结果:

在这里插入图片描述

怎么选择@Value和 @ConfigurationProperties?

作为一个开发者,不应该纠结到底使用哪种好哪种坏,因为两者实现和最终的目的都是一样的。

  1. 在开发中如果属性很少的情况可以考虑使用@Value
  2. 如果自定义属性的属性过多或者相关性很强的话,建议使用@ConfigurationProperties

关于自定义属性配置类的自动提示的问题

参考官网:https://docs.spring.io/spring-boot/docs/2.4.7/reference/html/appendix-configuration-metadata.html#configuration-metadata-annotation-processor

在项目的pom.xml中配置:
<!--把项目中的springboot自定义属性配置类生成一个元素数据文件,这个文件可以生成以后
  在未来的配置文件中,我们就达到和官方一致效果,可以自动提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
重新编译工程(核心步骤):

注意:一定要把项目打开的配置文件全部关闭。

然后重新编译项目

手动编译:

mvn clean compile

用工具编译:

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

南宫拾壹

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

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

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

打赏作者

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

抵扣说明:

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

余额充值