springboot 自动配置如何使用

1.新建一个新的项目
这里不再阐述
2.新建想要自动配置的Bean
举例 Logger


@Configuration
public class ILogger {
    public static ILogger getInstance(){
        return new ILogger();
    }
    Logger info = LoggerFactory.getLogger("info");
    Logger error = LoggerFactory.getLogger("error");
    Logger debug = LoggerFactory.getLogger("debug");
    Logger common = LoggerFactory.getLogger("common");
    Logger warn = LoggerFactory.getLogger("warn");

    {
        if (info == null) {
            info = LoggerFactory.getLogger(this.getClass());
        }
        if (error == null) {
            error = LoggerFactory.getLogger(this.getClass());
        }
        if (debug == null) {
            debug = LoggerFactory.getLogger(this.getClass());
        }
        if (common == null) {
            common = LoggerFactory.getLogger(this.getClass());
        }
        if (warn == null) {
            warn = LoggerFactory.getLogger(this.getClass());
        }

    }

    public void service(String content, Object... obj) {
        info.info(content, obj);
    }
    public void warn(String content , Object... obj){
        warn.warn(content,obj);
    }
    public void error(String content ,Object... obj){
        error.error(content,obj);
    }
    public void debug(String content,Object... obj){
        debug.debug(content,obj);
    }
    public void info(String content, Object... obj){
        common.info(content,obj);
    }
    public void service(String content){
        info.info(content);
    }
    public void error(String content){
        error.error(content);
    }
    public void debug(String content){
        debug.debug(content);
    }
    public void info(String content){
        common.info(content);
    }
    public void warn(String content) {
        warn.warn(content);
    }
}

通过@Configuration 或者 @Component 注解将类纳入spring管理 , 在别的地方自己配置Bean 也可以

3.配置spring.factories
s在这里插入图片描述
resources 目录下 新建META-INF 文件夹,再在里面新建spring.factories
spring会根据这个文件,进行自动配置
4对应配置

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.package.common.all.util.log.ILogger\
  ,com.package.common.all.util.log.TestConfig

其中需要注意的点就只有 1. 逗号隔开多个类

2. 换行使用 \ 符号
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值