首页 > 其他分享 >SpringBoot自定义starter

SpringBoot自定义starter

时间:2023-06-27 22:56:32浏览次数:41  
标签:SpringBoot 自定义 demo org import com starter ly

1、先来一个简单的案例

非常简单的工程结构
image

controller

package com.ly.demo.controller;

import com.ly.demo.service.MyStarterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ly (个人博客:https://www.cnblogs.com/ybbit)
 * @date 2023-06-27  21:37
 * @tags 喜欢就去努力的争取
 */
@RestController
public class MyStarterController {

    @Autowired
    private MyStarterService myStarterService;

    @GetMapping("/hello")
    public String hello(){
        return myStarterService.hello();
    }

}

service

package com.ly.demo.service;

/**
 * @author ly (个人博客:https://www.cnblogs.com/ybbit)
 * @date 2023-06-27  21:38
 * @tags 喜欢就去努力的争取
 */
public interface MyStarterService {

    String hello();
}


package com.ly.demo.service.impl;

import com.ly.demo.prop.MyStarterProperties;
import com.ly.demo.service.MyStarterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ly (个人博客:https://www.cnblogs.com/ybbit)
 * @date 2023-06-27  21:39
 * @tags 喜欢就去努力的争取
 */
@Service
public class MyStarterServiceImpl implements MyStarterService {

    @Autowired
    private MyStarterProperties myStarterProperties;

    @Override
    public String hello() {
        return "Hello, my name is " + myStarterProperties.getName() + " from Wuhan, Hubei Province. I am " + myStarterProperties.getAddress() + " years old";
    }
}

properties

package com.ly.demo.prop;

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

/**
 * @author ly (个人博客:https://www.cnblogs.com/ybbit)
 * @date 2023-06-27  21:40
 * @tags 喜欢就去努力的争取
 */
@ConfigurationProperties(prefix = "ly")
@Configuration
@Data
public class MyStarterProperties {

    private String name;
    private Integer age;
    private String address;

}

yml

ly:
  name: ly
  age: 25
  address: 湖北武汉

image

2、简单抽取

我们再创建一个新的工程(注意:不要继承原来的工程),把前面旧工程中的demo文件夹全部copy过来
image

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ly</groupId>
    <artifactId>my-springboot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.0</version>
    </parent>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
        </dependency>
    </dependencies>

</project>

别忘了install一下
image

然后把旧工程的demo文件夹全部删除
image

在pom.xml导入我们自定义的starter依赖

 <dependency>
	 <groupId>com.ly</groupId>
	 <artifactId>my-springboot-starter</artifactId>
	 <version>1.0-SNAPSHOT</version>
</dependency>

image

再来启动老项目测试一下
image

意料之中:注意我项目的目录结构
旧项目:com.ly.xxx
新项目:com.starter.demo.xxx
springboot默认扫描的是入口文件的同目录及子目录

所以,前面出现404情理之中

解决办法1:我们指定一下basePackages就好了

// 方式一:
@ComponentScans({
        @ComponentScan(basePackages = "com.starter.demo")
})

image

解决办法2:自定义一个xxxAutoConfiguration,然后导入我们的组件

package com.starter.demo.config;

import com.starter.demo.controller.MyStarterController;
import com.starter.demo.prop.MyStarterProperties;
import com.starter.demo.service.impl.MyStarterServiceImpl;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author ly (个人博客:https://www.cnblogs.com/ybbit)
 * @date 2023-06-27  22:27
 * @tags 喜欢就去努力的争取
 */
@Import({MyStarterController.class, MyStarterProperties.class, MyStarterServiceImpl.class})
@Configuration
public class StarterAutoConfiguration {
}

在旧工程中导入我们的自动配置即可

// 方式二:
@Import(StarterAutoConfiguration.class)

image

3、使用@EnableXxx机制

我们也可以自定义一个注解实现开启关闭我们的starter

package com.starter.demo.anno;

import com.starter.demo.config.StarterAutoConfiguration;
import org.springframework.context.annotation.Import;

import java.lang.annotation.*;

/**
 * @author ly (个人博客:https://www.cnblogs.com/ybbit)
 * @date 2023-06-27  22:33
 * @tags 喜欢就去努力的争取
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(StarterAutoConfiguration.class)
public @interface EnableMyStarter {
}

然以我们再旧的工程中使用注解@EnableMyStarter导入即可

// 方式三:
@EnableMyStarter

image

4、使用SpringBoot的SPI机制

在resources下创建META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,SpringBoot项目启动会自动加载我们的自动配置类
image

文件内容就写我们的配置类

com.starter.demo.config.StarterAutoConfiguration

image

测试发现依旧是没问题的

image

标签:SpringBoot,自定义,demo,org,import,com,starter,ly
From: https://www.cnblogs.com/ybbit/p/17510113.html

相关文章

  • 自定义代码片段
    前言使用自定义代码片段可以快速生成代码片段,提升开发效率。使用在vscode中ctrl+shift+p,新建全局代码片段。写好模板,复制进这个网站https://snippet-generator.app/将生成的模板复制进文件中......
  • SpringBoot项目中功能集成的方式
    原文合集地址如下,有需要的朋友可以关注本文地址合集地址SpringBoot项目中功能集成的方式接口集成基于HTTP协议的集成方式协议和通信HTTP是一种基于客户端-服务器模型的协议。确定使用的HTTP版本(如HTTP/1.1或HTTP/2)以及通信过程中使用的其他协议和规范。在协议和通信阶段,需......
  • vue3自定义hook
    什么是hookshook是钩子的意思,看到“钩子”是不是就想到了钩子函数?事实上,hooks还真是函数的一种写法。vue3借鉴reacthooks开发出了CompositionAPI,所以也就意味着CompositionAPI也能进行自定义封装hooks。vue3中的hooks就是函数的一种写法,就是将文件的一些单独功......
  • 基于MySQL+SpringBoot+IDEA开放的绩效评估系统
    基于MySQL+SpringBoot+IDEA开放的绩效评估系统项目介绍......
  • springboot配置
    复习:配置SSMMAVEN==》依赖(一堆的)web.xml(servlet,filter,listener...)spring-mvc.xmlspring-mybatis.xmlmapper....缺点:忒麻烦(配置文件多)容易出错花费得时间长如果从0开始配置一个helloworld(url)ssm项目一,springboot最重要得2个特点1,自动配置spring自动管理......
  • 高德地图动态Marker和自定义弹框、交互事件、中心点跳转
    高德地图vue3使用下载NPM:npmi@amap/amap-jsapi-loader--save根据官网提示,VUE3需要改变监听模式下载npmi@vue/reactivity组件内配置初始化<scriptsetup>//开发技术vue3piniatsimport{ref}from"vue";importAMapLoaderfrom"@amap/amap-jsapi-loa......
  • 基于vue +Java+springboot+element-ui开发的智慧班牌系统源码
    电子班牌系统又称之为智慧班牌,是当前校园数字化信息化建设、文化建设的主流,是校园日常工作安排、校园信息发布、班级文化风采展示、课堂交流、家校互通的重要应用载体。在每个班级门口安装一台电子班牌终端,实现学校日常管理、校园信息化建设数据对接,为学生提供一个德智教育文化环境......
  • Go语言中的自定义函数类型
    函数类型的基本概念在Go语言中,函数类型是一种将函数作为值的数据类型。与其他类型一样,函数类型可以被声明、赋值给变量,作为参数传递和作为返回值返回。通过函数类型,我们可以将函数看作一种可执行的对象,对其进行操作和处理。自定义函数类型的语法在Go语言中,我们可以使用type......
  • elementUI中upload自定义上传行为 http-request属性
    需求是上传一个xlsx后台处理完再返回xlsx流upload请求需要添加responseType:'blob'属性所有要扩展一下若依项目扩展elementUI中upload自定义上传行为http-request属性<el-uploadref="upload1":limit="1"accept=".xlsx,.xls":headers="......
  • 如何高度优化适用于企业自定义的AI (一) 序言
    概述在当前信息时代的背景下,社会对AI的需求在不断增长.AI的快速发展得益于大数据、云计算和计算能力的提升,使得机器学习和深度学习等技术取得了重大突破.AI在图像识别、语音识别、自然语言处理等领域展现出惊人的能力,为企业带来了巨大的商机.然而,通用的AI解决方案无法......