使用SpringBoot自定义一个starter启动包,超详细

1、准备好需要生成starter的环境

Maven:

引入需要用的基本jar测试

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

命名方式

根据spring官网可知starter命名方式

官网命名方式
spring-boot-starter-模块名----- 例:spring-boot-starter-web
自定义命名方式
模块-spring-boot-starter-----例:user-spring-boot-startera-b-spring-boot-starter

首先创建一个SpringBoot基础工程

本次测试生成user-spring-boot-starter自定义启动starter
在这里插入图片描述

生成启动类pom坐标可以如图位置修改

<!-- 根据你想生成的starter自定义命名 -->
	<groupId>com.xjt</groupId>
    <artifactId>user-spring-boot-starter</artifactId>		
    <version>0.0.1-SNAPSHOT</version>

2、开始创建自定义的starter

1、了解需要用到的注解

注解详情注意点
@ConfigurationProperties (prefix=“user”)自动获取配置文件与字段相对应的信息赋值、prefix(配置文件必须已user开头) 使用的宽松的绑定属性规则 user.username 、user.userName、user.user_name、user.user-name、user.USER_NAME以上都可以完成映射绑定1、配置必须已prefix中的值开头2、字段必须有setter方法、3、如果字段映射绑定失败类型不对,可以在注解后增加属性ignoreInvalidFields=true(默认为false)
@EnableConfigurationProperties(需要注入的类:如UserProperties.class)开启配置文件配置注入,让@ConfigurationProperties注解生效注解生效
@ConditionalOnClass某个class位于类路径上,才会实例化一个Bean单例设计模式
@ConditionalOnMissingBean仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean单例设计模式

2、创建一个配置映射类,获取对应的配置文件信息

在这里插入图片描述
该类会自动获取配置文件中的user下对应的信息装配

user:
  userName: xuajingtaoxxx
  hobby: swimmingssss

核心类

UserProperties

自动获取配置文件与字段相对应的信息赋值、prefix(配置文件必须已user开头)

package com.xjt.demo.conf;

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

@Configuration
@ConfigurationProperties(prefix = "user")
@Data
public class UserProperties {
	String userName;
	String hobby;
}

UserAutoConfiguration

注入userProperties,获取对应配置文件的具体值
注入需要注入的类,注入的类需加注解@Repository或@Component、@Service、@Controller

package com.xjt.demo.conf;

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

@Configuration
@ConditionalOnClass(UserService.class)
@EnableConfigurationProperties(UserProperties.class)
public class UserAutoConfiguration {

	@Autowired
	UserProperties userProperties;


	@Bean
	@ConditionalOnMissingBean(UserService.class)
	public UserService userService(){
	//将userService的字段赋值并注入到ioc容器
		return new UserService(userProperties.getUserName(), userProperties.getHobby());
	}
}

UserService

测试类,从新项目导入注入UserService测试类方法

package com.xjt.demo.conf;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserService {

	String name;
	String hobby;

	public String test(){
		System.out.println("name"+name+"\t\thobby"+hobby);
		return "this is test method";
	}
}

目录接口如下

在这里插入图片描述

生成jar包前准备

在resources资源目录下创建META-INF目录,并在下面创建spring.factories文件

spring.factories文件编写

#第二行为你的自动注入配置类的全类名路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xjt.demo.conf.UserAutoConfiguration

如果想要在输入配置信息的时候有提示信息。可以在spring.factories的统计目录创建additional-spring-configuration-metadata.json文件,并编写提示信息。

{
  "properties": [
    {
      "name": "user.userName",
      "type": "java.lang.String",
      "description": "用户的名称.",
      "defaultValue": "xujiangtao"
  },{
      "name": "user.hobby",
      "type": "java.lang.String",
      "description": "用户的爱好.",
      "defaultValue": "play game"
  }
] }

在这里插入图片描述

效果如下:
在这里插入图片描述

开始打包

在这里插入图片描述
在这里插入图片描述

出现以上标识打包完成,并将自定义jar包保存值maven本地仓库

本地maven仓库默认位置C:\用户\用户名\.m2\repository
在这里插入图片描述

创建新项目导入自定义starter包测试

        <dependency>
            <groupId>com.xjt</groupId>
            <artifactId>user-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

编写配置文件
在这里插入图片描述

新项目测试环境注入userService运行测试方法
在这里插入图片描述
测试成功。自定义Starter包完成!

如有疑问,欢迎大家评论交流❤️❤️❤️❤️

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
自定义Spring Boot Starter是一种用于简化Spring Boot应用程序配置和依赖管理的机制。它由几个组件组成,括starter、autoconfiguration和配置文件。 首先,你需要创建一个Maven工程,并创建三个模块。其中,starter负责引入依赖,autoconfiguration负责实现装配。在autoconfiguration中,你需要定义对应的properties类、configuration类和核心业务类。此外,你还需要在autoconfiguration的/resources/META-INF/目录下添加spring.factories文件,并配置org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.qiejk.demo.springboot.autoconfiguration.DemoAutoConfiguration。这一步是Spring Boot装配的核心,只有添加了这个内容,Spring Boot才会进行自动装配。\[1\] 在父模块的pom文件中,你需要引入Spring Boot的依赖,例如spring-boot-starter-web,以及指定Spring Boot的版本。这样,你的自定义starter模块就可以基于Spring Boot进行开发和使用。\[2\] 最后,你需要创建一个配置文件,用于告诉Spring Boot启动时扫描该配置文件,并将其中的配置类加载到Spring容器中。这个配置文件的作用是指定自动配置类的位置。\[3\] 通过以上步骤,你就可以创建自定义Spring Boot Starter,并在应用程序中使用它来简化配置和依赖管理。 #### 引用[.reference_title] - *1* [如何自定义springboot-starter](https://blog.csdn.net/sinat_29434159/article/details/123995794)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringBoot自定义Starter篇](https://blog.csdn.net/m0_46571920/article/details/122910726)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

languageStudents

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值