自定义Starter

简介: 启动器模块是一个 空 jar 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库;

自定义Starter
分析完毕了源码以及自动装配的过程,我们可以尝试自定义一个启动器来玩玩!

9.1.说明

启动器模块是一个 空 jar 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库;

命名归约:

官方命名:

  • 前缀:spring-boot-starter-xxx
  • 比如:spring-boot-starter-web....

自定义命名:

  • xxx-spring-boot-starter
  • 比如:mybatis-spring-boot-starter

9.2.编写启动器

1、在IDEA中新建一个空项目 spring-boot-starter-diy

2、新建一个普通Maven模块:kuang-spring-boot-starter
1673577199162.png

3、新建一个Springboot模块:kuang-spring-boot-starter-autoconfigure
1673577205499.png

4、点击apply即可,基本结构

1673577211779.png

5、在我们的 starter 中 导入 autoconfigure 的依赖!


<!-- 启动器 -->
<dependencies>
    <!--  引入自动配置模块 -->
    <dependency>
        <groupId>com.kuang</groupId>
        <artifactId>kuang-spring-boot-starter-autoconfigure</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

6、将 autoconfigure 项目下多余的文件都删掉,Pom中只留下一个 starter,这是所有的启动器基本配置!
1673577211779.png

7、我们编写一个自己的服务


package com.syj;

public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

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

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

}

8、编写HelloProperties 配置类

package com.syj;

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

// 前缀 kuang.hello
@ConfigurationProperties(prefix = "syj.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;
    }
}

9、编写我们的自动配置类并注入bean,测试!


package com.syj;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnWebApplication //web应用生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }

}

10、在resources编写一个自己的 META-INF\spring.factories


# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.syj.HelloServiceAutoConfiguration

11、编写完成后,可以安装到maven仓库中!
1673577339579.png

9.3.新建项目测试我们自己写的启动器

1、新建一个SpringBoot 项目

2、导入我们自己写的启动器


<dependency>
    <groupId>com.kuang</groupId>
    <artifactId>syj-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

3、编写一个 HelloController 进行测试我们自己的写的接口!

package com.syj.controller;

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping("/hello")
    public String hello(){
        return helloService.sayHello("zxc");
    }

}

4、编写配置文件 application.properties


syj.hello.prefix="ppp"
syj.hello.suffix="sss"

5、启动项目进行测试,结果成功 !

1673577306122.png

相关文章
|
4月前
|
IDE Java Maven
SpringBoot自定义starter及自动配置
SpringBoot自定义starter及自动配置
|
3月前
|
Java Maven 开发者
Spring Boot中的自定义Starter开发
Spring Boot中的自定义Starter开发
|
4月前
|
Java Maven
6.自定义starter
6.自定义starter
23 0
|
开发框架 SpringCloudAlibaba Java
Spring Boot自动配置原理详解和自定义封装实现starter
我们一直在强调`Spring Boot`能成为当下主流首选开发框架的主要原因在于其核心思想:**约定大于配置,自动配置,条件装配**。基于这些特性使得`Spring Boot`集成其他框架非常简单快捷
213 1
 Spring Boot自动配置原理详解和自定义封装实现starter
|
NoSQL Java Redis
自定义Sprin-Boot-Starter
自定义Sprin-Boot-Starter
40 0
|
XML Java 数据格式
如何实现一个自定义的starter
SpringFactoriesLoader加载器加载指定ClassLoader下面的所有的META-INF/spring.factories文件,并将文件解析内容存在Map&lt;string,list&gt;中。然后通过loadFactoryNames传递过来的class的名称从map中获取该类的配置列表。通过Set集合进行去重操作。执行过滤组件操作,而这些操作都是在AutoConfigurationImportFilter接口下的组件实现的,也即FilterSpringBootCondition实现抽象类的。下面有OnBeanCondition、OnClassCondition、OnWebApplic
36817 5
如何实现一个自定义的starter
|
Java 容器
|
Java Maven 开发者
自定义 starter|学习笔记
快速学习自定义 starter
111 0
自定义 starter|学习笔记
|
NoSQL Java Redis
如何自定义一个SpringBoot中的starter
如何自定义一个SpringBoot中的starter
198 0
如何自定义一个SpringBoot中的starter
|
NoSQL Java 应用服务中间件
SpringBoot项目为什么需要引入大量的starter?如何自定义starter?
为什么我们在使用SpringBoot框架开发Java Web应用需要引入大量的starter?例如,我们引入Redis就在Maven中导入spring-boot-starter-data-redis。大家都知道SpringBoot的核心功能是自动装配,简化配置,我们通过starter实现SpringBoot自动装配的功能。那么我们如何去构建自己的starter呢?
292 0
下一篇
无影云桌面