自定义Spring Boot starter并使用EnableAutoConfiguration装配

j360-boot

spring-boot入门工程之j360-boot:(欢迎star、fork)

https://github.com/xuminwlt/j360-boot

Spring Boot EnableAutoConfiguration DIY With Starter

Spring Boot是个优秀的框架不是说他有多牛,而在于创造了新的生产力,今年的JAX Award winner就是Spring Boot.

https://jaxenter.com/winners-jax-innovation-awards-2016-jax-london-129588.html

图片

The ‘Most innovative contribution to the Java ecosystem’ category aims to compliment the achievements of both the heavyweights of the Java ecosystem and the field’s newcomers. The emphasis ranges from data stores, language features, frameworks to JVM languages, tools and more. 1st place: Spring Boot 2nd place: Camunda BPM 3rd place: Apache Spark

Spring Boot从去年1.3开始研究了大半年,现在已经1.4发布了RELEASE版本,之前搞来搞去总算是搞明白了Spring Boot到底是什么,能帮助我们干什么,同时借助微服务的东风,DevOps的春风基本上已经吹遍大江南北了,我想说的是,学习Spring Boot,不要停下来。

@EnableAutoConfiguration

Spring Boot @EnableXXX注解是Spring Boot使用过程中的头等功能,血脉纯正,其中最大名鼎鼎的是@EnableAutoConfiguration。

之前我们提到过@SpringBootApplication,等同与以下3个注解,所以这里其实是使用了@EnableAutoConfiguration功能

@ComponentScan
@EnableAutoConfiguration
@Configuration
public class J360Configuration {

}

@EnableXXX注解,顾名思义,启用自动装配,自动装配什么东西呢,Spring Boot官方提供了很多自动装配的功能,(需要依赖该功能对应的starter jar包),比如提供了<b>spring-boot-starter-web</b>,则会自动开启spring MVC功能,使用Enable功能推荐使用键盘输入enable*,会提示出当前可被使用的一些enable选项。

图片

比如开启了@EnableAutoConfiguration功能后,在上面添加@EnableXXX功能,后会自动注入并生成对应的功能,本节的主题就是如何创建自己的@EnableXXX功能,在创建这个之前需要先看看@EnableXXX功能到底干了什么事情,再来DIY。

认识@EnableXXX

以@EnableJms为例

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({JmsBootstrapConfiguration.class})
public @interface EnableJms {
}

做了一件事情,就是定义了Jms的启动配置,表示启动时回去注册JmsBootstrapConfiguration中的功能,再往里面看

@Configuration
@Role(2)
public class JmsBootstrapConfiguration {
    public JmsBootstrapConfiguration() {
    }

    @Bean(
        name = {"org.springframework.jms.config.internalJmsListenerAnnotationProcessor"}
    )
    @Role(2)
    public JmsListenerAnnotationBeanPostProcessor jmsListenerAnnotationProcessor() {
        return new JmsListenerAnnotationBeanPostProcessor();
    }

    @Bean(
        name = {"org.springframework.jms.config.internalJmsListenerEndpointRegistry"}
    )
    public JmsListenerEndpointRegistry defaultJmsListenerEndpointRegistry() {
        return new JmsListenerEndpointRegistry();
    }
}

写的很清楚,定义了Jms和JmsListener相关联的一些Bean

同样@EnableMvc,同样是组装一个组建的Configuration

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {

}

如果不适用@Enable功能,我们通常使用@Import来定义一个Configuration,@Enable无非是提供了一套Configuration的标准入口,这个在平常开发中尤为重要,在平常使用做多的application.xml配置时,我们通常对一类组建配置在一起,比如application-shiro.xml/applicaiton-mybatis.xml等,同样@Enable也可以是@EnableShiro,@EnableMybatis

如果使用了autoconfiguration功能,则@EnableAutoConfiguration注解则自动注入上述的功能,稍后讨论,这里实现的是没有实现autoconfiguration情况下的EnableXXX功能。

DIY @EnableXXX

diy部分无非就是找一个样子照抄一份,这里就不贴源码了,源码在这里: 参见module: j360-enablediy https://github.com/xuminwlt/j360-boot

关于autoconfiguration

autoconfiguration功能是spring-starter方式的提供方式,通常将组建打包成diy-spring-boot-autoconfigure.jar的jar方式,配置

META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration = xxx.xxx.xxx.DiyAutoConfiguration

但是在平常的使用当中,我们经常用的是starter结尾的jar包,其实这个包并没有什么东西,仅仅是像dubbo一样,在里面配置一个导航,用于提供方pom的配置导航,命名为diy-spring-boot-starter.jar

META-INF/spring.provides

provides: diy-spring-boot-autoconfigure

使用diy-spring-boot-starter

和官方的使用方式一样,直接@EnableAutoConfiguration,Spring Boot会读取diy-spring-boot-autoconfigure中的配置信息,并对其进行初始化并注册,xxx.xxx.xxx.DiyAutoConfiguration

这里xxx.xxx.xxx.DiyAutoConfiguration究竟是个什么鬼,通常我们这样定义他

@Configuration
@EnableConfigurationProperties(DiyProperties.class)
@Conditional(DiySetCondition.class)
@AutoConfigureAfter(Diy2AutoConfiguration.class)
public class DiyAutoConfiguration {

}

上面使用的注解,已经可以使用之前的知识解释了,恩,就这样。

现在我们可以自己写一个简单的框架,并对其使用Spring Boot Starter方式进行自动装配,抛弃掉之前复杂繁琐的各种xml配置,回到之前大奖,spring boot诞生的初衷的确让人刮目相看。

未开启讨论功能请往这走,?:

欢迎star github: https://github.com/xuminwlt/j360-boot

详见个人博客: https://www.j360.me

转载于:https://my.oschina.net/smartsales/blog/775550

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值