专栏名称: ImportNew
伯乐在线旗下账号,专注Java技术分享,包括Java基础技术、进阶技能、架构设计和Java技术领域动态等。
目录
相关文章推荐
51好读  ›  专栏  ›  ImportNew

SpringBoot 编写自定义的 starter

ImportNew  · 公众号  · Java  · 2017-04-13 13:38

正文

(点击 上方公众号 ,可快速关注)


来源:fangjian0423,

fangjian0423.github.io/2016/11/16/springboot-custom-starter/

如有好文章投稿,请点击 → 这里了解详情


在之前的文章中,我们分析过SpringBoot内部的自动化配置原理和自动化配置注解开关原理。


自动化配置原理

http://fangjian0423.github.io/2016/06/12/springboot-autoconfig-analysis/

自动化配置注解开关原理

http://fangjian0423.github.io/2016/11/13/springboot-enable-annotation/


我们先简单分析一下mybatis starter的编写,然后再编写自定义的starter。


mybatis中的autoconfigure模块中使用了一个叫做MybatisAutoConfiguration的自动化配置类。


这个MybatisAutoConfiguration需要在这些Condition条件下才会执行:


  1. @ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })。需要SqlSessionFactory和SqlSessionFactoryBean在classpath中都存在


  2. @ConditionalOnBean(DataSource.class)。 spring factory中需要存在一个DataSource的bean


  3. @AutoConfigureAfter(DataSourceAutoConfiguration.class)。需要在DataSourceAutoConfiguration自动化配置之后进行配置,因为mybatis需要数据源的支持


同时在META-INF目录下有个spring.factories这个properties文件,而且它的key为org.springframework.boot.autoconfigure.EnableAutoConfiguration,这样才会被springboot加载:


# Auto Configure

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration


有了这些东西之后,mybatis相关的配置会被自动加入到spring container中,只要在maven中加入starter即可:


org.mybatis.spring.boot

mybatis-spring-boot-starter

1.1.1


编写自定义的starter


接下来,我们来编写自定义的starter:log-starter。


这个starter内部定义了一个注解,使用这个注解修饰方法之后,该方法的调用会在日志中被打印并且还会打印出方法的耗时。starter支持exclude配置,在exclude中出现的方法不会进行计算。


pom文件:


org.springframework.boot

spring-boot-starters

1.3.5.RELEASE

org.springframework.boot

spring-boot-starter


定义修饰方法的注解@Log:


package me.format.springboot.log.annotation;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

public @interface Log { }


然后是配置类:


package me.format.springboot.log.autoconfigure;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;

@ConfigurationProperties(prefix = "mylog")

public class LogProperties {

private String exclude;

private String[] excludeArr;

@PostConstruct

public void init() {

this.excludeArr = StringUtils.split(exclude, ",");

}

public String getExclude() {

return exclude;

}

public void setExclude(String exclude) {

this.exclude = exclude;

}

public String[] getExcludeArr() {

return excludeArr;

}

}


接下来是AutoConfiguration:


package me.format.springboot.log.autoconfigure;

import me.format.springboot.log.annotation.Log;

import me.format.springboot.log.aop.LogMethodInterceptor;

import org.aopalliance.aop.Advice;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.aop.Pointcut;

import org.springframework.aop.support.AbstractPointcutAdvisor;

import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;

import org.springframework.beans.factory.annotation.Autowired;







请到「今天看啥」查看全文