当前位置: 首页 > article >正文

自定义 SpringBoot Starter

原理部分已有说明:SpringBoot 自定义 Starter 实现,直接上效果图:

1、Starter项目

1.1、Starter项目结构

  1. 创建一个Demo Project,模拟一个需要被封装的DemoModule模块,其中核心方法为exeModuleMethod
  2. 通过starter封装可以直接初始化DemoModule的实例到Spring容器
  3. 在Maven中引入starter,且在yml中配置相应到参数即可直接初始化DemoModule的实例
  4. 在应用中注入DemoModule即可使用其exeModuleMethod方法
E:.
│  .gitignore
│  pom.xml
└─src
    └─main
        ├─java
        │  └─com
        │      └─tyron
        │          └─mystarterdemo
        │              │  MyStarterDemoApplication.java
        │              │
        │              ├─config
        │              │      DemoAutoConfiguration.java
        │              │      DemoProperties.java
        │              │
        │              └─module
        │                      DemoModule.java
        │
        └─resources
            └─META-INF
                    spring.factories

1.2、pom.xml

<?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.5.4</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.tyron</groupId>
  <artifactId>MyStarterDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo-springboot-starter</name>
  <description>Demo project for Spring Boot starter</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <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>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

1.3、MyStarterDemoApplication.java

package com.tyron.mystarterdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyStarterDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyStarterDemoApplication.class, args);
    }
}

1.4、DemoModule.java

package com.tyron.mystarterdemo.module;

public class DemoModule {

    private String version;

    private String name;

    public String exeModuleMethod() {
        return "Demo module, name = " + name + ", version = " + version;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

1.5、DemoProperties.java

package com.tyron.mystarterdemo.config;

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

@ConfigurationProperties(prefix = "com.tyron")
public class DemoProperties {
    private String version;
    private String name;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

1.6、DemoAutoConfiguration.java

package com.tyron.mystarterdemo.config;

import com.tyron.mystarterdemo.module.DemoModule;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(DemoProperties.class)
public class DemoAutoConfiguration {

    @Bean
    public DemoModule demoModule(DemoProperties properties){
        DemoModule demoModule = new DemoModule();
        demoModule.setName(properties.getName());
        demoModule.setVersion(properties.getVersion());
        return demoModule;
    }
}

1.7、spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tyron.mystarterdemo.config.DemoAutoConfiguration

1.8、打包

此为本地测试,如需项目使用 deploy 即可

mvn clean  install

至此,Starter已开发完成,下面进行测试:

2、Starter-test 项目

2.1、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.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tyron</groupId>
    <artifactId>starter-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>starter-test</name>
    <description>starter-test</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.tyron</groupId>
            <artifactId>demo-springboot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2、StarterTestApplication.java

package com.tyron.startertest;

import com.tyron.mystarterdemo.module.DemoModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class StarterTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(StarterTestApplication.class, args);
    }

    @Autowired
    private DemoModule demoModule;

    @GetMapping("/test")
    public String demo(){
        return demoModule.exeModuleMethod();
    }
}

2.3、application.yml

com:
    tyron:
        name: first starter
        version: v1

2.4、启动项目,验证

完结撒花!


http://www.kler.cn/a/452796.html

相关文章:

  • Vue.js 高级组件开发:设计模式与实践
  • Go快速开发框架2.6.0版本更新内容快速了解
  • C++ —— 模板类与函数
  • c++ 类似与c# 线程 AutoResetEvent 和 ManualResetEvent的实现
  • kubernetes Gateway API-部署和基础配置
  • 4.3 数据库HAVING语句
  • 联通软研院:基于OceanBase落地检索增强生成 (RAG) 的应用实践
  • 基于Spring Boot的工商局商家管理系统
  • 不在广东想把自己的IP变成广东怎么办
  • 配置搜索无人机
  • 游戏引擎学习第56天
  • 软件需求建模方法
  • CSS @property 属性
  • 分布式调度框架学习笔记
  • 大模型推理引擎国产化思考和实践
  • 学习solid works第七课------装配体
  • ip归属地是什么意思?ip归属地是实时定位吗
  • Oracle数据库高效优化与实战案例解析
  • 走进 Web3:探索分布式网络的未来
  • Android 开发中自定义吐司(二)
  • Redis基础(1)--基本全局指令与架构
  • Charles安装证书过程(手机)
  • CSS系列(35)-- Subgrid详解
  • uni-app开发订单详情页面
  • Vue3知识弥补漏洞——性能优化篇
  • SSH无法启动问题:OpenSSL version mismatch. Built against 30000070, you have 30200020