06-Springboot第三方组件注入:yml文件和@Bean的第三方组件注入

上一篇:05-Springboot构造器绑定:yum文件和Properties类的构造器绑定https://blog.csdn.net/fsjwin/article/details/109700066
yml中配置的数据也可以在@Bean标注的类上,示例如下:

1. application.yml

ymlother:
  str: 普通字符串
  specialStr: "换\n行字符串"
  num: 520
  dnum: 520:22
  birth: 2020/11/11 11:11:11
  list:
    - 大鸟
    - 小鸟
    - 中鸟
  set:
    - set1
    - set2
    - set3
  #set: [set3,set5,set6]
  map: {k1: v1, k2: v2}
  users:
    - name: yuhl
      age: 20
    - name: rll
      age: 18

2. YmlService

package com.yuhl.service;

import com.yuhl.YmlOtherProperties;
import com.yuhl.YmlProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

/**
 * @author yuhl
 * @Date 2020/11/14 11:09
 * @Classname YmlService
 * @Description TODO
 */
@Service  //不用标注@configuration就可以定义bean了 2.1的新特性
public class YmlService {

    @Bean
    @ConfigurationProperties(prefix = "ymlother")
    public YmlOtherProperties ymlOtherProperties(){
        return new YmlOtherProperties();
    }

}

3. HelloController

    @RequestMapping("/ymlOther")
    public YmlOtherProperties ymlOther() {
        System.out.println("ymlOtherProperties:" +ymlOtherProperties);
        return ymlOtherProperties;
    }

4 测试

在这里插入图片描述

5. 总结

这与这种类型的注入,显得特别灵活。在spring的底层代码中使用较多。
下一篇:07-Springboot中@ConfigurationProperties && @Value https://blog.csdn.net/fsjwin/article/details/109706479

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值