SpringBoot自定义起动器实例代码

  


  

  

SpringBoot最强大的功能就是把我们常用的场景抽取成了一个个起动器(场景启动器),我们通过引入SpringBoot为我提供的这些场景启动器,我们再进行少量的配置就能使用相应的功能,即使是这样,SpringBoot也不能囊括我们所有的使用场景,往往我们需要自定义起动器,来简化我们对SpringBoot的使用。
  

  

下面话不多说了,来一起看看详细的介绍吧

  


  

  

<强> 1。实例
  

  

如何编写自动配置?
  

  

我们参照@WebMvcAutoConfiguration为例,我们看看需要准备哪些东西,下面是WebMvcAutoConfiguration的部分代码:
  

        @ configuration   @ConditionalOnWebApplication   @ConditionalOnClass ({Servlet。类,DispatcherServlet。类,WebMvcConfigurerAdapter.class})   @ConditionalOnMissingBean ({WebMvcConfigurationSupport.class})   @AutoConfigureOrder (-2147483638)   @AutoConfigureAfter ({DispatcherServletAutoConfiguration。类,ValidationAutoConfiguration.class})   公开课WebMvcAutoConfiguration {      @ import ({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})   @EnableConfigurationProperties ({WebMvcProperties。类,ResourceProperties.class})   公共静态类WebMvcAutoConfigurationAdapter延伸WebMvcConfigurerAdapter {      @ bean   @ConditionalOnBean ({View.class})   @ConditionalOnMissingBean   公共BeanNameViewResolver BeanNameViewResolver () {   BeanNameViewResolver解析器=new BeanNameViewResolver ();   resolver.setOrder (2147483637);   返回解析器;   }   }   }      

我们可以抽取到我们自定义起动时,同样需要的一些配置。
  

        @ configuration//指定这个类是一个配置类   @ConditionalOnXXX//指定条件成立的情况下自动配置类生效   @AutoConfigureOrder//指定自动配置类的顺序   @ bean//向容器中添加组件   @ConfigurationProperties//结合相关xxxProperties来绑定相关的配置   @EnableConfigurationProperties//让xxxProperties生效加入到容器中      自动配置类要能加载需要将自动配置类,配置在meta - inf/spring.factories中   org.springframework.boot.autoconfigure.EnableAutoConfiguration=\   org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration, \   org.springframework.boot.autoconfigure.aop.AopAutoConfiguration, \      

<强>模式
  

  

我们参照spring-boot-starter我们发现其中没有代码:

  

 SpringBoot自定义起动器实例代码

  

我们在看它的pom中的依赖中有个springboot-starter
  

        & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter   & lt;/dependency>      

我们再看看spring-boot-starter有个spring-boot-autoconfigure
  

        & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-autoconfigure   & lt;/dependency>      

关于web的一些自动配置都写在了这里,所以我们有以下总结:
  

  
  

启动器起动器只是用来做依赖管理
  需要专门写一个类似spring-boot-autoconfigure的配置模块
  用的时候只需要引入启动器起动器,就可以使用自动配置了
  

     

<>强命名规范
  

  

官方命名空间

  
      <李>前缀:spring-boot-starter -李   <李>模式:spring-boot-starter -模块名李   <李>举例:spring-boot-starter-web spring-boot-starter-jdbc李   
  

自定义命名空间

  
      <李>后缀:-spring-boot-starter李   <李>模式:模块-spring-boot-starter李   <李>举例:mybatis-spring-boot-starter李   
  


  

  

我们需要先创建两个工程hello-spring-boot-starter和hello-spring-boot-starter-autoconfigurer
  

  

1。hello-spring-boot-starter
  

  pom

1.。xml
  

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;项目xmlns=" http://maven.apache.org/POM/4.0.0 " xmlns: xsi=" http://www.w3.org/2001/XMLSchema-instance "   xsi: schemaLocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”比;   & lt; modelVersion> 4.0.0

SpringBoot自定义起动器实例代码