SpringBoot中自定义starter的相关知识,以及流程

本文介绍了SpringBoot中自定义starter的背景、实现步骤及测试方法。通过理解starter的自动配置原理,创建并配置自动配置类,定义@ConfigurationProperties,最后打包并在web工程中测试验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、starter的概述

SpringBoot将我们需要的一些场景抽取成了一些starter,我们只需要引入这些场景启动器,就可以使用很多功能,比如AOP、jpa等等,而且他们已经自动配置了很多功能,我们只需要进行少量自定义配置,就可以满足我们的需求,但是开发的时候,我们需要的功能是千变万化的,spring不能囊括我们需要的所有需求,所以我们就可以自定义starter,制作一个场景,方便我们在发开时使用

2、如何自定义Starter

我们需要考虑:
1)、我们这个场景需要的依赖是什么?
2)、如何编写自动配置?

前人栽树后人乘凉,我们可以看一下已经有的starter是怎么配置的。

这里我们就以WebMvcAutoConfiguration为例

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
  • @Configuration指定它是一个配置类
  • @ConditionalOnxxx 指定条件,在xxx环境下可以生效
  • @AutoConfigureOrder、@AutoConfigureAfter 指定自动配置类的顺序

其他需要用到的

  • @Bean 给容器中添加组件
  • @ConfigurationProperties结合相关的properties类 ,绑定相关的配置
  • @EnableConfigurationProperties 让ConfigurationProperties的类生效,并且加入到容器中

自动配置类要想加载成功,需要将标注@Configuration的自动配置类,放在classpath下的META-INF/spring.factories文件中,比如:
在这里插入图片描述

3、模式:

在这里插入图片描述
我们可以看到这个starter的jar包中没有任何的java代码,它只是用来做依赖引入的,这些依赖可能用于自动装配或者其他类库在这里插入图片描述
因此,我们可以先写一个xxx-starter,它只用于引入我们的xxx-start-autoconfigurer的依赖,我们的主要代码放在start-xxx-autoconfigurer中

为了规范呢,我们推荐使用以下明明规约

官方命名空间

  • 前缀:“spring-boot-starter-”
  • 模式: spring-boot-starter-模块名
  • 举例:spring-boot-starterweb…
    自定义明明空间
  • 后缀:“-spring-boot-starter”
  • 模式:模块-spring-boot-starter
  • 举例:mybatis-spring-boot-starter

4、实现步骤

我们写一个简单的starter,引入此自动配置模块,实现在应用中,可以直接调用自动配置模块的service中的方法,并且此方法的一些参数配置,可以在我们的配置文件中配置

1、启动器模块:
我们随便创建一个MAVEN,它只用于添加一个依赖

<?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.example</groupId>
    <artifactId>atlizy-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.atlizy.starter</groupId>
            <artifactId>atlizy-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

2、自动配置模块

<?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.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.atlizy.starter</groupId>
    <artifactId>atlizy-spring-boot-starter-autoconfigurer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>atlizy-spring-boot-starter-autoconfigurer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
<!--引入springboot的starter;所有starter的基本配置-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
</project>

3、实现简单的自动配置的功能
①、自动配置类

@Configuration
@ConditionalOnWebApplication//定义只有web应用才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    @Autowired
    HelloProperties helloProperties;
    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return  helloService;
    }
}

②、实现功能的service方法

public class HelloService {

    HelloProperties helloProperties;
        public  String sayHello(String name){
            return  helloProperties.getPrefix()+"-"+name+helloProperties.getSuffix();
        }

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
}

③、properties配置类,可以在后续的开发中,在配置文件中添加自定义属性,并将属性用于service

@ConfigurationProperties(prefix = "atlizy.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;
    }
}

4、打包
在这里插入图片描述
我们将这两个方法install之后,就将他们放入了MAVEN仓库,后续的开发就可以导入这个依赖。

5、测试:

我们刚才的自定义starter中用注解声明为只能在web环境中使用,我们创建一个web工程。
1、导入依赖

 <!--引入自定义的starter-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>atlizy-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

2、编写一个controller测试starter中的service

@RestController
public class MyController {
    @Autowired
    HelloService helloService;
    @GetMapping("/hello")
    public String sayhellotest(){
        String sayHello = helloService.sayHello("张三");
        return sayHello;
    }
}

3、配置我们需要的属性
因为我们在starter中使用了@ConfigurationProperties(prefix = “atlizy.hello”)注解,所以我们的相关配置需要以atlizy.hello开头

atlizy.hello.prefix=ATLIZY
atlizy.hello.suffix=HELLO WORLD

4、运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值