手写SpringBoot的启动器

启动器模块是一个空 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;

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(){
        return helloService.sayHello("lzl");
    }
}

配置前缀后缀:
application.properties:

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

键盘歌唱家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值