springboot实现自定义starter组件开发

引言

springboot 在java领域的应用可谓是独领风骚,细心的小伙伴都会发现springboot都是由一个个starter组件组成,无论是spring自带starter还是第三方集成starter都是如此,公司开发肯定会有一些是基础架构研发的,业务项目大多都需要依赖基础架构来进行业务开发,一般通用的工具、加解密、通信之类的都属于基础架构。如何通过自定义starter实现自己的组件开发。

springboot自动注入核心

spring-boot-autoconfiguration 包下面的spring.factories文件里面默认spring给配置了许多 EnableAutoConfiguration相关的配置注解类,这些都是springboot默认添加的配置类,如果我们要实现相应的starter也是如此即可。

自定义starter实现

idea创建empty project

分别新增pdf-spring-boot-starter和pdf-spring-boot-starter-autoconfigurue模块

注意:这里为啥默不是spring-boot-starter-xxx而是xxx-spring-boot-starter,是因为spring自建的starter是spring-boot-starter-xxx这样的,如果是第三方的建议xxx-spring-boot-starter,并不是什么强制规定,这是便于区分的写法而已。

pdf-spring-boot-starter-autoconfigurue新增配置

pom文件配置

xml 复制代码
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.6.5</version>
</parent>

<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.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.29</version>
  </dependency>
</dependencies>

编写PdfProperties

java 复制代码
@ConfigurationProperties(prefix = "wh.pdf")
public class PdfProperties {
    private int maxSize;
    private String fileDir;
    private String imageType;

    public int getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    public String getFileDir() {
        return fileDir;
    }

    public void setFileDir(String fileDir) {
        this.fileDir = fileDir;
    }

    public String getImageType() {
        return imageType;
    }

    public void setImageType(String imageType) {
        this.imageType = imageType;
    }
}

编写PdfHandlerService业务实现

java 复制代码
public class PdfHandlerService {
    private PdfProperties properties;
    public PdfProperties getProperties() {
        return properties;
    }
    public void setProperties(PdfProperties properties) {
        this.properties = properties;
    }
    /**
     * pdf转图片
     * @param path
     * @return
     * @throws IOException
     */
    public List<String> pdfToImage(String path) throws IOException {
        List<String> filePaths = new ArrayList<>();
        PDDocument pdDocument = PDDocument.load(new File(path));
        int pages = pdDocument.getNumberOfPages();
        PDFRenderer renderer = new PDFRenderer(pdDocument);
        for (int i = 0; i < pages; i++) {
            BufferedImage renderImage = renderer.renderImage(i);
            String filePath =  properties.getFileDir() + (i+1)+"."+properties.getImageType();
            ImageIO.write(renderImage, properties.getImageType(), new File(filePath));
            filePaths.add(filePath);
        }
        // 关闭文档
        pdDocument.close();
        return filePaths;
    }
}

编写PdfConfiguration配置类

less 复制代码
@Configuration
@ConditionalOnClass({PdfHandlerService.class})
@EnableConfigurationProperties(PdfProperties.class)
public class PdfConfiguration {
    // spring容器会接管PdfHandlerService,这样在引入自定义starter后直接使用@Autowired就可以直接使用
    @Bean
    public PdfHandlerService pdfHandlerService(PdfProperties pdfProperties){
        PdfHandlerService pdfHandlerService = new PdfHandlerService();
        pdfHandlerService.setProperties(pdfProperties);
        return pdfHandlerService;
    }
}

创建META-INF/spring.factories添加自动注入配置类

pdf-spring-boot-starter配置

pom.xml配置

xml 复制代码
<dependencies>
  <dependency>
    <groupId>org.wh</groupId>
    <artifactId>pdf-spring-boot-starter-autoconfigurer</artifactId>
    <version>1.1.0</version>
  </dependency>
</dependencies>

starer可以写相关组件的代码,我这里演示没有写

本地创建jar文件

实际生产开发自然是加入公司自己的仓库中去,以便项目引用

验证测试

本地项目引入

application.yml文件添加配置

编写单元测试

相关推荐
ai小鬼头1 分钟前
AIStarter最新版怎么卸载AI项目?一键删除操作指南(附路径设置技巧)
前端·后端·github
Touper.7 分钟前
SpringBoot -- 自动配置原理
java·spring boot·后端
Alfred king10 分钟前
面试150 生命游戏
leetcode·游戏·面试·数组
一只叫煤球的猫39 分钟前
普通程序员,从开发到管理岗,为什么我越升职越痛苦?
前端·后端·全栈
一只鹿鹿鹿44 分钟前
信息化项目验收,软件工程评审和检查表单
大数据·人工智能·后端·智慧城市·软件工程
专注VB编程开发20年1 小时前
开机自动后台运行,在Windows服务中托管ASP.NET Core
windows·后端·asp.net
程序员岳焱1 小时前
Java 与 MySQL 性能优化:MySQL全文检索查询优化实践
后端·mysql·性能优化
一只叫煤球的猫2 小时前
手撕@Transactional!别再问事务为什么失效了!Spring-tx源码全面解析!
后端·spring·面试
旷世奇才李先生2 小时前
Ruby 安装使用教程
开发语言·后端·ruby
海的诗篇_5 小时前
前端开发面试题总结-原生小程序部分
前端·javascript·面试·小程序·vue·html