手写SpringBoot的启动器
发布日期:2021-05-07 13:38:53 浏览次数:19 分类:原创文章

本文共 5324 字,大约阅读时间需要 17 分钟。

启动器模块是一个空 JAR 文件,仅提供辅助性依赖管理,这些依赖可能用于自动 装配或者其他类库,命名规范如下:
官方命名 : spring-boot-starter-模块名(eg:spring-boot-starter-web、spring-boot-starter-jdbc、spring-boot-starter-thymeleaf)
自定义命名 : 模块名-spring-boot-starter (eg:mybatis-spring-boot-start)

如何编写自动配置

@Configuration //指定这个类是一个配置类@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效@AutoConfigureAfter //指定自动配置类的顺序@Bean //给容器中添加组件@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置@EnableConfigurationProperties //让xxxProperties生效加入到容器中public class XxxxAutoConfiguration {   

上面的都是前置知识,相信大家都很懵逼,先看下面的实例吧,进入正题:

第一步

idea创建一个空项目:
在这里插入图片描述
然后在该空项目中创建下面两个模块:
在这里插入图片描述

可以删除多余的目录结构,最终的结构如下:
在这里插入图片描述

lzl-spring-boot-starter 文件不用写任何东西!他只是个普通的maven项目,只需要将lzl-springboot-starter-autoconfigurer的gav坐标配进来即可:
lzl-spring-boot-starter的pom.xml:

<?xml version="1.0" encoding="UTF-8"?><project 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">    <modelVersion>4.0.0</modelVersion>    <groupId>org.lzl</groupId>    <artifactId>lzl-spring-boot-starter</artifactId>    <version>1.0-SNAPSHOT</version><!--启动器-->    <dependencies>        <!--引入自动配置模块-->        <dependency>            <groupId>org.lzl</groupId>            <artifactId>lzl-springboot-starter-autoconfigurer</artifactId>            <version>0.0.1-SNAPSHOT</version>        </dependency>    </dependencies></project>

重点是写springboot-starter-autoconfigurer文件!
首先说他的pom.xml吧,其实也只要导入spring-boot-starter 这一个依赖:

<?xml version="1.0" encoding="UTF-8"?><project 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">	<modelVersion>4.0.0</modelVersion>	<parent>		<groupId>org.springframework.boot</groupId>		<artifactId>spring-boot-starter-parent</artifactId>		<version>2.3.1.RELEASE</version>		<relativePath/> <!-- lookup parent from repository -->	</parent>	<groupId>org.lzl</groupId>	<artifactId>lzl-springboot-starter-autoconfigurer</artifactId>	<version>0.0.1-SNAPSHOT</version>	<name>lzl-springboot-starter-autoconfigurer</name>	<description>Demo project for Spring Boot</description>	<properties>		<java.version>1.8</java.version>	</properties>	<dependencies>		<!--引入spring-boot-starter 所有starter的基本配置-->		<dependency>			<groupId>org.springframework.boot</groupId>			<artifactId>spring-boot-starter</artifactId>		</dependency>	</dependencies></project>

然后编写下面的类:
HelloProperties.class

package com.lzl.lzlspringbootstarterautoconfigurer;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix="lzl.hello")public class HelloProperties {       private String prefix;    private String suffix;    public String getPrefix() {           return prefix;    }    public void setPrefix(String prefix) {           this.prefix = prefix;    }    public String getSuffix() {           return suffix;    }    public void setSuffix(String suffix) {           this.suffix = suffix;    }}

HelloService.class:

package com.lzl.lzlspringbootstarterautoconfigurer;import org.springframework.beans.factory.annotation.Autowired;public class HelloService {       HelloProperties helloProperties;    public HelloProperties getHelloProperties() {           return helloProperties;    }    public void setHelloProperties(HelloProperties helloProperties) {           this.helloProperties = helloProperties;    }    public String sayHello(String name){           return helloProperties.getPrefix()+"-"+name+"-"+helloProperties.getSuffix();    }}

HelloServiceAutoConfiguration .class

package com.lzl.lzlspringbootstarterautoconfigurer;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration@ConditionalOnWebApplication//web应用才生效@EnableConfigurationProperties(HelloProperties.class)public class HelloServiceAutoConfiguration {       @Autowired    HelloProperties helloProperties;    @Bean    public HelloService helloService(){           HelloService service = new HelloService();        service.setHelloProperties(helloProperties);        return service;    }}

写好这三个类,还需要在META-INF/spring.factories 中配置一下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.lzl.lzlspringbootstarterautoconfigurer.HelloServiceAutoConfiguration

至此该写的都写了,现在将这两个项目install到本地maven仓库,以后就可以使用了。

验证,看看我们的启动器能不能用

新建一个Springboot的web项目
在pom文件中导入我们的启动器:
在这里插入图片描述
编写测试Controller:

package org.lzl.bootstartertest.controller;import com.lzl.lzlspringbootstarterautoconfigurer.HelloService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {       @Autowired    HelloService helloService;    @GetMapping("/hello")    public String hello(){           return helloService.sayHello("lzl");    }}

配置前缀后缀:
application.properties:

lzl.hello.prefix=555555555lzl.hello.suffix=999999999server.port=8888

启动springboot,访问目标:成功!!!!!
在这里插入图片描述

上一篇:SpringBoot缓存入门篇
下一篇:SpringBoot事件监听机制

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年03月28日 09时54分19秒