SpringBoot中自定义Starter

SpringBoot中自定义Starter

1. 前置知识

在我们自定义starter的时候,先要了解 Springboot自动装配原理。
我们可以参照 mybatis-plus-boot-starter源码来实现我们的自定义starter功能。

1-1 什么是Starter?

Starter是一种用于简化依赖管理和配置的方式。
		它是一个预定义的依赖关系集合, 包含了一组常用的依赖和配置,
		以便于快速启动和构建特定类型的应用程序。

使用Starter可以大大简化项目的依赖管理和配置工作,
		提供了一种快速启动和构建特定类型应用程序的方式。
		例如,Spring Boot提供了spring-boot-starter-web用于快速构建Web应用程序,
		它包含了常用的Web依赖(如Spring MVC、Tomcat等)和相关的自动配置。

开发者也可以自定义自己的Starter,将常用的依赖和配置打包为一个Starter,
		方便在复用和共享。自定义Starter可以提供一组特定领域的依赖和配置,以满足开发需求。

1-2 常用的Condition注解说明

在加载自动配置类的时候,
		并不是将spring.factories的配置全部加载进来,
		而是通过@Conditional等注解的判断进行动态加载

@Conditional其实是spring底层注解,
		意思就是根据不同的条件,来进行自己不同的条件判断,
		如果满足指定的条件,那么配置类里边的配置才会生效。
常用的Conditional注解:

@ConditionalOnClass : classp ath中存在该类时起效
@ConditionalOnMissingClass : classpath中不存在该类时起效
@ConditionalOnBean : DI容器中存在该类型Bean时起效
@ConditionalOnMissingBean : DI容器中不存在该类型Bean时起效
@ConditionalOnSingleCandidate : DI容器中该类型Bean只有一个或 @Primary的只有一个时起效
@ConditionalOnExpression : SpEL表达式结果为true时
@ConditionalOnProperty : 参数设置或者值一致时起效
@ConditionalOnResource : 指定的文件存在时起效
@ConditionalOnJndi : 指定的JNDI存在时起效
@ConditionalOnJava : 指定的Java版本存在时起效
@ConditionalOnWebApplication : Web应用环境下起效
@ConditionalOnNotWebApplication : 非Web应用环境下起效

1-3 Starter的命名规范

Spring官方Starter通常命名为 spring-boot-starter-{name}
		如:spring-boot-starter-web
Spring官方建议非官方Starter命名应遵循 {name}-spring-boot-starter的格式:
		如 mybatis-spring-boot-starter。

2. 自定义自己的Starter示例

2-1 创建Starter项目

自定义一个starter,名字是demo-spring-boot-starter, 结构如下图:
在这里插入图片描述

2-2 Pom.Xml【示例引入了spring-boot-starter和lombok】

<?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.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zhaof</groupId>
    <artifactId>demo-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-spring-boot-starter</name>
    <description>demo-spring-boot-starter</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

2-3 几个Class内容如下:

2-3-1 配置类:PersonProperties
package com.zhaof.demospringbootstarter.config;

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

import java.io.Serializable;

@Data
//@Component   注:这里无需使用@Component
@ConfigurationProperties(prefix = "sea.person")
public class PersonProperties implements Serializable {
    private String name;
    private int age;
}
2-3-2 业务类:PersonService
package com.zhaof.demospringbootstarter.service;

import com.zhaof.demospringbootstarter.config.PersonProperties;
import org.springframework.beans.factory.annotation.Autowired;

//@Component   注:这里无需使用@Component
public class PersonService {

    @Autowired
    private PersonProperties personProperties;

    public String sayHello() {
        return "Hello, name is "+ personProperties.getName() + ", age is " + personProperties.getAge();
    }
}
2-3-3 自动配置类:PersonAutoConfiguration
package com.zhaof.demospringbootstarter.autoconfig;

import com.zhaof.demospringbootstarter.config.PersonProperties;
import com.zhaof.demospringbootstarter.service.PersonService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(PersonProperties.class)
@ConditionalOnClass(PersonService.class)
public class PersonAutoConfiguration {

    @Bean
    public PersonService personService(){
        return new PersonService();
    }
}

2-4 创建spring.factories文件

//	spring.factoryes文件如下:
//		如下内容中,反斜杠表示换行,太长了可以换到下一行。


org.springframework.beans.autoconfig.EnableAutoConfiguration=

com.zhaof.demospringbootstarter.autoconfig.PersonAutoConfiguration

在这里插入图片描述

1. 注意:

  • spring boot 2.7后不再推荐使用META-INF/spring.factories自动装配
  • 推荐使用META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports来自动装配。
    当然为了向后兼容,spring.factories还是会继续支持

2. 注意:
springboot 3.0之后自动配置文件,
只能使用META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
使用如下:

1. 在resources目录下建立
		META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件
2. 内容可参考如下,可发现
		可以明显的看到使用org.springframework.boot.autoconfigure.AutoConfiguration.imports
		的内容更加直观,
		不需要再去指定EnableAutoConfiguration这个固定的字符串
		然后多个配置之间也无需再用,来换行,直接换行即可
com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration
com.baomidou.mybatisplus.autoconfigure.MybatisPlusLanguageDriverAutoConfiguration
com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration	

在这里插入图片描述

2-5 项目打成jar,发布到maven仓库

直接使用idea的maven功能进行打包
在这里插入图片描述

2-6 其他项目使用自定义的Starter

2-6-1 创建一个SpringBoot项目,Pom.Xml中使用自定义的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.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zhaof</groupId>
    <artifactId>springboot-rundemo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-rundemo1</name>
    <description>springboot-rundemo1</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 自定义Starter工程 -->
        <dependency>
            <groupId>com.zhaof</groupId>
            <artifactId>demo-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
2-6-2 在配置文件application.yml中写入配置
spring:
  application:
    name: springboot-rundemo1

# 自定义Starter使用的配置信息
sea:
  person:
    name: 测试
    age: 11
2-6-3 使用自定义Starter
package com.zhaof.controller;

import com.zhaof.demospringbootstarter.service.PersonService;
import com.zhaof.dto.ResponseDto;
import com.zhaof.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/Api2/v1.0")
public class Demo2Controller {

    @Autowired
    private PersonService personService2;

	// 示例中用到的返回结果类、包装类代码在下方
    @GetMapping("/sayHello")
    public ResponseDto sayHello(){
        String said = personService2.sayHello();
        return ResponseUtil.success(said);
    }

}



package com.zhaof.dto;

import lombok.AllArgsConstructor;
import lombok.Data;

@AllArgsConstructor
@Data
public class ResponseDto<T> {
    private int code;
    private String message;
    private T data;
}


package com.zhaof.utils;

import com.zhaof.dto.ResponseDto;

public class ResponseUtil {

    public static  <T> ResponseDto success(T data){
        return new ResponseDto(200,"success",data);
    }

    public static <T> ResponseDto fail(T data){
        return new ResponseDto(-1,"fail",data);
    }
}
2-6-4 发起请求测试

在这里插入图片描述

Spring Boot是一个快速开发微服务的框架,它提供了大量的自动化配置和快速开发的工具。在Spring Boot中,我们可以使用Starter来快速集成一些常用的功能,如数据库、缓存、web等。而自定义Starter则可以让我们将自己的功能快速集成到Spring Boot中,下面是自定义Starter的流程: 1. 创建Maven项目 首先,我们需要创建一个Maven项目作为我们自定义Starter的项目。在项目的pom.xml中添加Spring Boot的依赖,以及其他需要集成的依赖。 2. 编写自动配置类 自动配置类是自定义Starter的核心,它负责将我们自定义的功能集成到Spring Boot中。在自动配置类中,我们可以使用@Conditional注解来判断是否需要进行配置。 3. 编写StarterStarter类是我们自定义Starter的入口,它负责将自动配置类注入到Spring容器中。在Starter类中,我们需要使用@EnableAutoConfiguration注解来启用自动配置。 4. 打包和发布Starter 当我们完成了自动配置类和Starter类的编写后,我们需要将自定义Starter打包成jar包,并发布到Maven仓库中,以便其他项目可以通过Maven依赖的方式使用我们的Starter。 5. 在项目中使用自定义Starter 在其他项目中使用自定义Starter非常简单,只需要在项目中的pom.xml中添加我们自定义Starter的依赖即可。Spring Boot会自动将我们的自定义Starter集成到项目中,并进行自动配置。 以上就是自定义Starter的流程,通过自定义Starter,我们可以将自己的功能快速集成到Spring Boot中,提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值