自定义springboot 起步依赖

定义start子项目

项目结构在这里插入图片描述
配置自动注册扫描

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.example.start_template.autoconfig.HelloAutoConfiguration

配置类转换:

@ConfigurationProperties(prefix = CustomProperties.CUSTOM_PROFILE_PREFIX)
public class CustomProperties {

    public static final String CUSTOM_PROFILE_PREFIX = "custom.profile";
    private Map<String, Object> info;

    public Map<String, Object> getInfo() {
        return info;
    }

    public void setInfo(Map<String, Object> info) {
        System.out.println("CustomProperties-setInfo" + info.toString());
        this.info = info;
    }
}

选择注入实现:

@Configuration
public class FormatAutoConfiguration {

    //没有指定的包则进行注入
    @ConditionalOnMissingClass("com.alibaba.fastjson.JSON")
    @Bean
    @Primary
    public FormatProcessor stringFormat(){
        return new StringFormatProcessor();
    }

    //存在指定的包则进行注入
    @ConditionalOnClass(name = "com.alibaba.fastjson.JSON")
    @Bean
    public FormatProcessor jsonFormat(){
        return new JsonFormatProcessor();
    }

}

待扫描类:

@Import(FormatAutoConfiguration.class)
@EnableConfigurationProperties(CustomProperties.class)
@Configuration
public class HelloAutoConfiguration {

    @Bean
    public HelloFormatTemplate helloFormatTemplate(CustomProperties customProperties, FormatProcessor formatProcessor) {
        return new HelloFormatTemplate(customProperties,formatProcessor);
    }

}

另一项目引入依赖,完成调用:

#自定义配置文件
custom.profile.info.key1=value1
custom.profile.info.key2=value1
custom.profile.info.key3=value1
custom.profile.info.key4=value1
@RestController
public class TestController {

    @Autowired
    private HelloFormatTemplate helloFormatTemplate;

    @RequestMapping("/get/info")
    public String getMessage(){
        return helloFormatTemplate.getCustomPropertiesString();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值