读取配置yml、xml的九种写法

@Configuration
@EnableConfigurationProperties(WxCpProperties.class)
public class WxCpConfiguration {

    private WxCpProperties properties;

    @Autowired
    public WxCpConfiguration( WxCpProperties wxCpProperties) {
        //目前需求只是操作通讯录,还不需要配服务器
        this.properties = wxCpProperties;
    }

//装比:
@ConfigurationProperties注解可以生效是因为
在SpringBoot中有一个类叫ConfigurationPropertiesAutoConfiguration,
它为@ConfigurationProperties注解的解析提供了支持的工作,
而@ConfigurationProperties为何能支持?这个类上还存在了@Configuration@EnableConfigurationProperties这两个注解
//WxCpConfiguration 的EnableConfigurationProperties纯粹是为了WxCpProperties 少写个@Component
@EnableConfigurationProperties(A.class)的作用就是如果 A 这个类上使用了 @ConfigurationProperties 注解,那么 A 这个类会与 xxx.properties 进行动态绑定,并且会将 A 这个类加入 IOC 容器中,并交由 IOC 容器进行管理
// WxCpConfiguration 的Configuration是为了WxCpConfiguration(WxCpProperties wxCpProperties)用@Autowired修饰时允许它能有资格取。但没有什么return wxCpConfiguration的需要,亦也就没有加@Bean了(看来作者想法是cpServices不需要注入ioc(WxCpConfiguration 其实也不需要,但没有办法,@Autowired要用,既然必须注入,那注入ioc时会调构造函数,那干脆properties 初始化也放那吧,废物利用,真是物尽其用简约代码 ),只要静态取就完事了),那也可以说没啥配置的味,所以还真的可以类上@Configuration换回@Component,反而最贴切,否则浪费。可能作者也是配置类如此写装比惯了,一时没转过弯
--------------------------
/**
 * 企业微信properties
 */
@Data
@ConfigurationProperties(prefix = "wechat.cp")
public class WxCpProperties {
  /**
   * 设置企业微信的corpId
   */
  private String corpId;
@Component
@Data
@ConfigurationProperties(prefix = "constant")
public class SystemProperties {
   private String fastdfsImg;

-----------------------
  @Autowired
    private SystemProperties systemProperties;
@Component
public class Consumer {
    @Value("${rocketmq.consumer.consumerGroup}")

2021年1月26日其实我的另一篇文章早提过了(md,怪不得写此篇下笔之前觉得写过,又没翻到,故又有了当前博文。。。↑)
https://blog.csdn.net/CW_SZDX/article/details/106868298

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值