自定义Spring boot Starter 入门简单步骤

101 阅读1分钟

1、前言

自定义Spring boot Starter的步骤

2、搭建自己的Starter

1、创建Maven工程

创建一个springboot工程

image.png

pom.xml 中删除包括 < build > 标签的所有子标签

以及删除全部依赖,只需保留必要依赖

<dependencies>
    <!--用于自动加载配置-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>

    <!--用于自动生成配置文件的自动提示-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <!--lombok-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

2、代码编写

定义接收 yml 配置参数的属性类

package com.xulyu.selfspringbootstarter;

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

/**
 *
 * @author xulongyu
 * @date 2022/12/7 14:06
 **/

@Data
@ConfigurationProperties(prefix = "com.xulyu")
public class UserInfo {

    private String userName;
    
    private String password;
    
    private String age;
    
}

定义具体的业务实现类

此类是用来在第三方工程中被@autowired注入的

具体的业务实现逻辑写在此类中

package com.xulyu.selfspringbootstarter;


import org.springframework.beans.factory.annotation.Autowired;

public class UserInfoService {

    /**
    * yml配置的属性类
    **/
    @Autowired
    private UserInfo userInfo;

    /**
     *  打印用户信息
     *
     * @author xulongyu
     * @date 2022/12/7 14:10
     **/
    public void printUserInfo(){
        String userName = userInfo.getUserName();
        String password = userInfo.getPassword();
        String age = userInfo.getAge();
        System.out.println("用户名--"+userName);
        System.out.println("密  码--"+password);
        System.out.println("年  龄--"+age);
    }
}

定义自动配置的类

此类主要是用来约定 注入的Bean什么时候生效

package com.xulyu.selfspringbootstarter;

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

@Configuration
@EnableConfigurationProperties(UserInfo.class)
public class UserInfoAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(UserInfoService.class)//当ioc中没有UserInfoService时生效
    public UserInfoService getService(){
        return new UserInfoService();
    }
}

3、编写spring.factories文件

在spring-boot-autoconfigure-2.2.1.RELEASE.jar看到有spring.factories文件

image.png

仿照着在resources下创建一个META-INF\spring.factories文件

仿照粘贴 Auto Configure的配置

image.png

3、第三方使用自定义Starter

将我们自己写好的starter使用maven工具进行install打包

在第三方引用我们的starter依赖

第三方项目编写配置yml

image.png

测试注入我们的service

package com.xuly.springboottest.controller;

import com.xulyu.selfspringbootstarter.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StarterTestController {

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/userInfo")
    public String userInfo(){
        userInfoService.printUserInfo();
        return "Hello Starter";
    }

}

肥肠的完美!!!

image.png