springboot自动化配置之自定义一个starter

author author     2023-03-20     230

关键词:

大家好,我是小悟

Spring Boot官网各类启动器:

​https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-starter​

Spring

Spring Boot将所有的功能场景都抽取出来,做成一个个starter,只需要在项目里面引入这些starter,相关的依赖包都会导入进来,可以说是十分的方便了。在日常的开发中,我们也可以结合业务自定义需要的starter,供其他开发小伙伴调用。


1、创建一个新的空工程

Spring

2、创建两个module,austin-spring-boot-starter启动器是普通的maven工程,austin-spring-boot-starter-autoconfigurer自动配置模块是普通的springboot工程

Spring

austin-spring-boot-starter的pom文件

<?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.austin.starter</groupId>
<artifactId>austin-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>

<!--启动器-->
<dependencies>

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

</project>

austin-spring-boot-starter-autoconfigurer的pom文件

<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.austin.starter</groupId>
<artifactId>austin-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>austin-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>

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

<dependencies>
<!--引入spring‐boot‐starter;所有starter的基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

</dependencies>

</project>

3、在austin-spring-boot-starter的pom文件中引入自动配置模块,如下所示

<?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.austin.starter</groupId>
<artifactId>austin-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>

<!--启动器-->
<dependencies>

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

</project>

4、在austin-spring-boot-starter-autoconfigurer下新建HelloProperties、HelloService、HelloServiceAutoConfiguration

Spring

package com.austin.starter;

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

/**
* @ClassName HelloProperties
* @Description
*/
@ConfigurationProperties(prefix = "austin.hello")
@Data
public class HelloProperties

private String prefix;
private String suffix;
package com.austin.starter;

import lombok.Data;

/**
* @ClassName HelloService
* @Description
*/
@Data
public class HelloService

HelloProperties helloProperties;

public String sayHello(String name)
return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();

package com.austin.starter;

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;

/**
* @ClassName HelloServiceAutoConfiguration
* @Description
*/
@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;

5、resources下面新建META-INF-spring.factories文件

Spring

内容如下

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

因为starter是依赖autoconfigurer的,所以先把autoconfigurer安装到本地仓库,再安装starter

Spring

Spring


6、另外新建一个springboot工程来测试我们自定义的starter

Spring

Spring

Spring


7、新建一个controller来测试

package com.austin.custom.controller;

import com.austin.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @ClassName HelloController
* @Description
*/
@RestController
public class HelloController

@Autowired
HelloService helloService;

@GetMapping(value = "/sayHello")
public String sayHello()
return helloService.sayHello("你好");

在application.yml中传入我们在austin-spring-boot-starter-autoconfigurer定义的两个参数prefix和suffix

Spring

Spring


8、启动springboot测试

Spring


您的一键三连,是我更新的最大动力,谢谢

山水有相逢,来日皆可期,谢谢阅读,我们再会

我手中的金箍棒,上能通天,下能探海

玩转springboot原理篇(自动装配前凑之自定义stater)(代码片段)

的pom文件中可以看出,mybatis-spring-boot-starter包会自动引入mybatis-spring-boot-autoconfigure以及mybatis相关依赖包。SqlSessionFactoryExceptionlogger.debug(encoding=xmlns:xsi=xsi:schemaLocation=<modelVersion><groupI 查看详情

springboot整合springsecurity之自定义退出(代码片段)

一security默认的退出Springsecurity默认实现了logout退出,访问/logout: 实现逻辑:点击“LogOut”退出成功。退出后访问其它url判断是否成功退出。二自定义退出2.1配置文件中配置在WebSecurityConfifig的protectedvoidconfifigure(HttpSe... 查看详情

springboot整合springsecurity之自定义认证(代码片段)

一自定义认证页面1.1说明1.如果用户没有自定义登录页面,springsecurity默认会启动自身内部的登录页面,尽管自动生成的登录页面很方便快速启动和运行,但大多数应用程序都希望定义自己的登录页面。1.2自定义登录... 查看详情

springboot之自定义starter

1、创建一个EmptyProject2、在该工程中点击+,选择newmodule,新建一个maven工程点击确定。3、在该工程中点击+,选择newmodule,新建一个SpringInitializr工程 后面直接默认next,然后点击finishi。两个都创建完毕之后点击apply,点击OK。... 查看详情

springboot系列之自定义枚举类的数据校验注解

SpringBoot系列之自定义枚举类的数据校验注解业务场景:数据校验,需要对枚举类型的数据传参,进行数据校验,不能随便传参。拓展,支持多个参数的枚举数据校验在网上找到很多参考资料,所以本博客基于这些博客进行拓展... 查看详情

springboot之自定义查询query

  下面讲解下SpringBoot之自定义查询Query的实例SpringBoot之自定义查询Query有HQL语句查询(Hibernate),还可以采用sql语句本地查询BookDao类查询接口1packagecom.hik.dao;23importjava.util.List;45importorg.springframework.data.jpa.repository.J 查看详情

springcloud系列之自定义gatewayfilterfactory(代码片段)

SpringCloud系列之自定义GatewayFilterFactory学习目的:知道创建一个网关sample知道网关的基本配置知道自定义GatewayFilterFactory类环境准备:JDK1.8SpringBoot2.2.3SpringCloud(Hoxton.SR7)Maven3.2+开发工具IntelliJIDEAsmartGit新增SpringBootInitializer项目:N 查看详情

如何禁用 TomcatServletWebServerFactory 的 SpringBoot 自动配置以便自定义 spring-starter 提供它?

】如何禁用TomcatServletWebServerFactory的SpringBoot自动配置以便自定义spring-starter提供它?【英文标题】:HowtodisableSpringBootautoconfigurationforTomcatServletWebServerFactoryinorderforacustomspring-startertoprovideit?【发布时间】:2020-03-0807:46:49【问 查看详情

springboot系列之自定义枚举类的数据校验注解(代码片段)

SpringBoot系列之自定义枚举类的数据校验注解业务场景:数据校验,需要对枚举类型的数据传参,进行数据校验,不能随便传参。拓展,支持多个参数的枚举数据校验在网上找到很多参考资料,所以本博客... 查看详情

springcloud系列之自定义gatewayfilterfactory(代码片段)

...本配置知道自定义GatewayFilterFactory类环境准备:JDK1.8SpringBoot2.2.3SpringCloud(Hoxton.SR7)Maven3.2+开发工具IntelliJIDEAsmartGit新增SpringBootInitializer项目:NewModule->SpringInitializer,选择jdk版本,至少jdk8packaging选择jar,javav... 查看详情

210630:springboot自动配置-自定义start

参考技术ASpringBoot关于自动配置的源码在spring-boot-autoconfigure-x.x.x.x.jar中:当然,自动配置原理的相关描述,官方文档貌似是没有提及。不过我们不难猜出,SpringBoot的启动类上有一个@SpringBootApplication注解,这个注解是SpringBoot项目... 查看详情

阿里微服务专家手写springboot实现一个简单的自动配置模块

为了更好的理解SpringBoot的自动配置和工作原理,我们自己来实现一个简单的自动配置模块。假设,现在项目需要一个功能,需要自动记录项目发布者的相关信息,我们如何通过SpringBoot的自动配置,更好的实现功能呢?实战的开... 查看详情

springboot编写自定义的starter

在之前的文章中,我们分析过SpringBoot内部的自动化配置原理和自动化配置注解开关原理。我们先简单分析一下mybatisstarter的编写,然后再编写自定义的starter。mybatis中的autoconfigure模块中使用了一个叫做MybatisAutoConfiguration的自动化... 查看详情

struts2框架之自定义拦截器和配置

...存在拦截器,只不过系统自动调用。框架自带的拦截器的配置文件所在的位置为:  javaResources--->Libraries--->struts2-core-2.3.36.jar(核心包)--->struts-default.xml  这个配置文件中放置的是框架所有的拦截器,拦截器放置在拦截... 查看详情

webpack配置之自定义loader

...pi/loaders/简单案例1.创建一个替换源码中字符串的loader2.在配置文件中使用loader,这里用的绝对路径其他引入自定义loader方式,可参考另外一篇文章--webpack中resolveLoader的使用方法4.this.callback:如何返回多个信息,不止是处理好的源... 查看详情

容器指南之自定义confluence容器镜像

1.简介Confluence的安装配置操作步骤繁琐,有很多需要注意的地方。不同版本间的配置都存在差异。利用Docker的功能实现配置项目的整合容器指南之第一个容器    ​https://blog.51cto.com/waringid/5904849​​容器指南之容器... 查看详情

容器指南之自定义jira容器镜像

...括需求管理、进度管理、问题和风险管理等。Jira的安装配置操作步骤繁琐,有很多需要注意的地方。不同版本间的配置都存在差异。利用Docker的功能实现配置项目的整合容器指南之第一个容器    ​​https://blog.51ct... 查看详情

javaweb之自定义tag开发

在javaweb开发中我们可能经常会用到basePath这个参数,没错,当我们新建一个jsp页面的时候,myeclipse会自动帮我们生成下面这段代码:<%Stringpath=request.getContextPath();StringbasePath=request.getScheme()+":// 查看详情