Spring boot源码初识三 自定义starter

项目结构

1.父级项目:my-springboot-starter
2.子module:my-spring-boot-starter
3.子module:my-spring-boot-starter-autoconfigure
4.starter引用autoconfigure
在这里插入图片描述

demo下载

下载地址

demo截图

注:spring.factories中不同key,spring boot对应生命周期时会调用对应类
在这里插入图片描述

demo部分示例

创建父pom

创建父项目my-springboot-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.6.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.my</groupId>
    <artifactId>my-springboot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-springboot-starter</name>
    <description>自定义spring boot starter</description>
    <packaging>pom</packaging>

    <modules>
        <module>my-spring-boot-starter</module>
        <module>my-spring-boot-starter-autoconfigure</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

创建starter

在父项目中创建module,名字叫my-spring-boot-starter
此jar包内容为空,关键pom中引入autoconfigure
对外提供的时候只需引入starter包,自动引入autoconfigure

<?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>
    <groupId>com.my</groupId>
    <artifactId>my-spring-boot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-spring-boot-starter-autoconfigure</name>
    <description>自动配置类</description>

    <parent>
        <groupId>com.my</groupId>
        <artifactId>my-springboot-starter</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

</project>

创建autoconfigure

创建my-spring-boot-starter-autoconfigure项目,此项目实现自动注入

pom文件

<?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>
    <groupId>com.my</groupId>
    <artifactId>my-spring-boot-starter-autoconfigurer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-spring-boot-starter-autoconfigurer</name>
    <description>自动配置类</description>

    <parent>
        <groupId>com.my</groupId>
        <artifactId>my-springboot-starter</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

</project>

创建 META-INF/spring.factories

设置自动注入配置

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.my.springboot.starter.autoconfigurer.HelloAutoConfitguration

创建HelloAutoConfitguration

package com.my.springboot.starter.autoconfigurer;

import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloAutoConfitguration {

    public HelloAutoConfitguration(){
        System.out.println("------EnableAutoConfiguration------");
    }
}

maven install 成jar包

其他spring boot项目pom中引入starter则自动注入配置

<dependency>
    <groupId>com.my</groupId>
    <artifactId>my-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值