这样手写Spring Boot 启动配置的操作,有点骚

大家好,我是十一郎。

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

一,实例

如何编写自动配置 ?

我们参照@WebMvcAutoConfiguration为例,我们看看们需要准备哪些东西,下面是WebMvcAutoConfiguration的部分代码(也可以自己跟进代码进行查看):

       

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

 

@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 我们发现其中没有代码:

我们在看它的pom中的依赖中有个 Spring Boot-Starter

 

<dependency>    
   <groupId>org.springframework.boot</groupId>  
   <artifactId>spring-boot-Starter</artifactId>
</dependency>              

我们再接着去看看 spring-boot-Starter 有个 spring-boot-autoconfigure

 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>              

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

       

 

启动器(Starter)是一个空的jar文件,仅仅提供辅助性依赖管理,这些依赖可能用于自动装配或其他类库。

需要专门写一个类似spring-boot-autoconfigure的配置模块

用的时候只需要引入启动器Starter,就可以使用自动配置了

命名规范

官方命名空间

  • 前缀:spring-boot-Starter-

  • 模式:spring-boot-Starter-模块名

  • 举例:spring-boot-Starter-web、spring-boot-Starter-jdbc

自定义命名空间

  • 后缀:-spring-boot-Starter

  • 模式:模块-spring-boot-Starter

  • 举例:mybatis-spring-boot-Starter

二,自定义Starter实例

我们需要先创建一个父maven项目:Spring Boot_custome_Starter

两个Module: tulingxueyuan-spring-boot-Starter 和 tulingxueyuan-spring-boot-Starter-autoconfigurer

其中 Spring Boot_custome_Starter

pom.xml

       

于是我们的就可以这么写 :

1. tulingxueyuan-spring-boot-Starter

1.pom.xml

       

注意:如果使用spring Initializr创建的需要删除 启动类、resources下的文件,test文件。

2. tulingxueyuan-spring-boot-Starter-autoconfigurer

1. pom.xml

       

2. HelloProperties

               

3. IndexController

       

        

4. HelloAutoConfitguration

       

        

5. spring.factories

在 resources 下创建文件夹 META-INF 并在 META-INF 下创建文件 spring.factories ,内容如下:

       

 

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.Starter.tulingxueyuan.HelloAutoConfitguration

到这儿,我们的配置自定义的Starter就写完了 ,我们hello-spring-boot-Starter-autoconfigurer、hello-spring-boot-Starter 安装成本地jar包。

三、测试自定义Starter

我们创建个Module: 12_Spring Boot_Starter,来测试下我们写的Start。

1. pom.xml

       

        

2.浏览

http://localhost:8080/

       

由于在自动配置上设置了

 

@ConditionalOnProperty(value = "tuling.hello.name")

但我们还没有配置。so......

3. application.properties

tuling.hello.name="XX公司"              

再次访问:http://localhost:8080/

       

 最近我花了半个月的时间整理了一些大厂的面经,扫下方名片可以领一下,记得备注一下“面经”,我好知道你要干啥~

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值