SpringBoot-starter原理之手写

目录

一、前言

二、实现思路

2.1 命名规范

2.2 实现步骤

三、手写实现

3.1 新建第三方jar

3.2 新建starter工程

3.3 引入jar包

3.4 编写自动配置类

3.5 编写配置发现文件

3.6 打包发布

四、测试


一、前言

SpringBoot-starter的原理,在之前的文章中已作解析,可点此传送

今天要来手动实现一个自己的SpringBoot-starter。

二、实现思路

2.1 命名规范

  • Spring提供的starter命名规范spring-boot-starter-xxx.jar,如spring-boot-starter-web-2.1.3.jar
  • 第三方提供的starter命名规范xxx-spring-boot-starter.jar,如druid-spring-boot-starter-1.1.13.jar

当然,只是规范而已,偏不按这个来也没关系。

2.2 实现步骤

  1. 准备需要引入的第三方的jar包
  2. 建自己的starter工程
  3. 引入spring-boot-starter、spring-boot-autoconfigure和第三方jar包(如需自动生成配置元信息文件META-INF/spring-configuration-metadata.json,加入spring-boot-configuration-processor)
  4. 编写自动配置类xxxAutoConfiguration
  5. 编写发现配置文件spring.factories
  6. 打包发布

三、手写实现

3.1 新建第三方jar

新建Maven工程share,在新建的包com.szh.share中新建类ShareDemo,加入一些属性和一个方法,如下:

接着mvn命令打包为share-0.0.1.jar,待starter引入。

3.2 新建starter工程

新建Maven工程share-spring-boot-starter,新建包com.szh.share.spring.boot.autoconfigure。

3.3 引入jar包

右键工程名share-spring-boot-starter,选择Maven,继续选择Add Dependency,输入框中输入spring-boot-starter并确认,如下:

同样地,引入spring-boot-autoconfigure.jar和share-0.0.1.jar,以及spring-boot-configuration-processor.jar。最后的pom依赖如下:

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.szh</groupId>
        <artifactId>share</artifactId>
        <version>0.0.1</version>
    </dependency>
</dependencies>

3.4 编写自动配置类

在starter工程的包com.szh.share.spring.boot.autoconfigure中,新建自动配置类ShareAutoConfiguration,因为它是一份配置,所以为其添加注解@Configuration;因为这份配置里需要有配置bean,所以在新建的方法上添加注解@Bean,并返回share-0.0.1.jar中的ShareDemo;因为这个starter的配置属性也是来源于被引用的项目中的application.xml,所以在自动配置类ShareAutoConfiguration上添加注解@EnableConfigurationProperties(ShareProperties.class),以此启用配置属性ShareProperties。

紧接着,再去包com.szh.share.spring.boot.autoconfigure中新建配置属性类ShareProperties,添加注解@ConfigurationProperties(prefix = "szh.share"),代表application.xml中以szh.share为前缀的属性都是属于share-spring-boot-starter的;另外需在它里面定义需要的配置属性。

ShareProperties代码如下:

package com.szh.share.spring.boot.autoconfigure;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "szh.share")
public class ShareProperties {

    private String name;
    private String desc;
    private String url;
    /**
     * 用户名属性
     */
    private String username;
    private String password;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

ShareAutoConfiguration代码如下:

package com.szh.share.spring.boot.autoconfigure;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.szh.share.ShareDemo;

@Configuration
@EnableConfigurationProperties(ShareProperties.class)
public class ShareAutoConfiguration {

    @Bean
    public ShareDemo getShareDemo(ShareProperties shareProperties) {
        ShareDemo shareDemo = new ShareDemo();
        shareDemo.setName(shareProperties.getName());
        shareDemo.setDesc(shareProperties.getDesc());
        shareDemo.setUrl(shareProperties.getUrl());
        shareDemo.setUsername(shareProperties.getUsername());
        shareDemo.setPassword(shareProperties.getPassword());
        return shareDemo;
    }
}

3.5 编写配置发现文件

为starter工程新建文件夹META-INF,新建配置发现文件spring.factories,将自动配置类加入进去,内容如下:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.szh.share.spring.boot.autoconfigure.ShareAutoConfiguration

3.6 打包发布

在打包之前,我们肯定是需要将META-INF/spring.factories文件也打进去,因此pom.xml中还需加入以下构建配置:

<build>
    <resources>
        <resource>
            <directory>META-INF</directory>
            <targetPath>META-INF/</targetPath>
        </resource>
    </resources>
</build>

接着mvn命令打包为share-spring-boot-starter-0.0.1.jar,待另外的工程引入。这里打开看看打包好的share-spring-boot-starter-0.0.1.jar:

META-INF/spring-configuration-metadata.json则是spring-boot-configuration-processor帮我们创建的ShareProperties元数据文件,凭借它的介绍,我们可以在application.xml中准确配置使用。

四、测试

新建另外的工程,引入share-spring-boot-starter-0.0.1.jar,查看是否可以自动依赖第三方jar包share-0.0.1.jar,在application.xml中配置szh.share.name、szh.share.url等,查看是否可以成功注入share-spring-boot-starter中的bean——ShareDemo,它的配置属性是否生效。

  • 12
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一个简单的示例,展示了如何手写一个Spring Boot Starter: 首先,创建一个 Maven 项目,并添加以下依赖项: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.5.4</version> </dependency> </dependencies> ``` 接下来,创建一个自定义的自动配置类,用于配置你的 Starter: ```java @Configuration @EnableConfigurationProperties(MyStarterProperties.class) public class MyStarterAutoConfiguration { private final MyStarterProperties properties; public MyStarterAutoConfiguration(MyStarterProperties properties) { this.properties = properties; } // 在此处定义你的自动配置逻辑 @Bean public MyStarterService myStarterService() { return new MyStarterService(properties); } } ``` 然后,创建一个属性类,用于将外部配置绑定到属性上: ```java @ConfigurationProperties("my.starter") public class MyStarterProperties { private String message; // 提供 getter 和 setter } ``` 最后,创建一个自定义的服务类,该服务类将在你的 Starter 中使用: ```java public class MyStarterService { private final MyStarterProperties properties; public MyStarterService(MyStarterProperties properties) { this.properties = properties; } public void showMessage() { System.out.println(properties.getMessage()); } } ``` 现在,你的 Spring Boot Starter 已经准备就绪了!你可以将其打并使用在其他 Spring Boot 项目中。在其他项目的 `pom.xml` 文件中,添加你的 Starter 依赖: ```xml <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>my-starter</artifactId> <version>1.0.0</version> </dependency> </dependencies> ``` 然后,在你的应用程序中使用 `MyStarterService`: ```java @SpringBootApplication public class MyApplication implements CommandLineRunner { private final MyStarterService myStarterService; public MyApplication(MyStarterService myStarterService) { this.myStarterService = myStarterService; } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { myStarterService.showMessage(); } } ``` 这样,你就成功地创建了一个简单的 Spring Boot Starter!当其他项目引入你的 Starter 并运行时,将会输出预定义的消息。 当然,这只是一个简单的示例,真实的 Starter 可能含更多的配置和功能。你可以根据自己的需求进行扩展和定制。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值