SpringBoot——手把手教你自定义starter_ManbaBryant的博客-程序员宅基地

技术标签: spring boot  # Spring+SpringMVC+SpringBoot  


SpringBoot是如何定义starter的

  1. 比如我们需要引入web模块
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. spring-boot-starter-web相当于一个启动器,而启动器模块只是一个空jar文件,只提供辅助性依赖管理,管理的这些依赖可能用于自动配置或其他功能

在这里插入图片描述

  1. 在pom.xml中可以看到spring-boot-starter-web是依赖spring-boot-starter的,而spring-boot-starter又包含了spring-boot-autoconfigure
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--包含-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
  1. 在spring-boot-autoconfigure-web中才包含java代码

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

  1. 设计思路
  • 自定义一个启动器,只进行依赖导入
  • 再专门写一个自动配置模块

如何定义我们自己的starter

  1. 命名规则
  • Spring官方提供的启动器为spring-boot-starter-starter名称的格式
  • 自定义的需要为starter名称-spring-boot-starter的格式
  1. 新建maven工程,用作启动器模块
  • Group Id:com.cc.starter
  • Artifact Id:hello-spring-boot-starter
  1. 新建springboot工程,用作自动配置模块
  • Group Id:com.cc.starter
  • Artifact Id:hello-spring-boot-start-autoconfigure
  1. 在启动器模块中引入自动配置模块的maven坐标
<dependency>
	<groupId>com.cc.starter</groupId>
	<artifactId>hello-spring-boot-start-autoconfigure</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>
  1. maven坐标如何看?打开需要被引入工程的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.2.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<!-- maven坐标 -->
	<groupId>com.cc.starter</groupId>
	<artifactId>hello-spring-boot-start-autoconfigure</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<!-- 省略-->
	
</project>
  1. 编写自动配置模块
  • 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.2.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.cc.starter</groupId>
	<artifactId>hello-spring-boot-starter-autoconfigure</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<!--不添加pom.xml会报错-->
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>

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


</project>
  • 新建 HelloProperties.java(在 src/main/java 下)
package com.cc.starter;

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

//从application.properties加载参数
@ConfigurationProperties(prefix = "cc.hello")
public class HelloProperties {
    

    private String prefix;
    private String suffix;

    public String getPrefix() {
    
        return prefix;
    }

    public void setPrefix(String prefix) {
    
        this.prefix = prefix;
    }

    public String getSuffix() {
    
        return suffix;
    }

    public void setSuffix(String suffix) {
    
        this.suffix = suffix;
    }
}
  • 新建 HelloService.java(在 src/main/java 下)
package com.cc.starter;

public class HelloService {
    
	HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
    
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
    
        this.helloProperties = helloProperties;
    }

    public String sayHello(String name){
    
        return helloProperties.getPrefix()+"-" +name +"-"+ helloProperties.getSuffix();
    }
}
  • 新建 HelloServiceAutoConfiguration.java(在 src/main/java 下)
package com.cc.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;

//定义配置类
@Configuration 
//指明是web应用才生效
//也可以自己指定别的生效条件 @ConditionalOnXXX
@ConditionalOnWebApplication 
//使 @ConfigurationProperties注解生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    

    @Autowired
    HelloProperties helloProperties;
    @Bean
    public HelloService helloService(){
    
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }
}
  • 类路径下(src/main/resources)新建 META-INF文件夹,在其中新建 spring.factories 文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.cc.starter.HelloServiceAutoConfiguration
  • 为什么这么写,参考 spring-boot-autoconfigure.jar 下的 META-INF 下的 spring.factories 文件,SpringBoot会自动加载通过逗号分隔的一系列 XXXAutoConfiguration
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
  1. 打包:maven -clean,maven -install,由于启动器模块依赖自动配置模块,需要先打包自动配置模块
  2. 测试
  • 新建SpringBoot项目
  • 在pom.xml引入自定义的starter(启动器,不是自动配置)
<dependecy>
	<groupId>com.cc.starter</groupId>
	<artifactId>hello-spring-boot-starter</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependecy>
  • 新建HelloController.java
@Controller
public class HelloController {
    
	@Autowired
	HelloService helloService;
	
	@ResponseBody
	@RequestMapping("/test")
	public String test() {
    
		return helloService.sayHello("cc");
	}
}
  • 修改 application.properties
cc.hello.prefix=a
cc.hello.suffix=b
  • 验证,访问 http://localhost:8080/test 请求,访问成功

在这里插入图片描述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_38938338/article/details/104806485

智能推荐

halcon_line_color_halcon中按设置线框的颜色和粗细-程序员宅基地

This example program shows how to use lines_color and the differencesbetween the output of lines_color and lines_gauss using an image in whichsome lines can only be extracted from a color image.dev_update_off ()dev_close_window ()dev_open_window (0,.._halcon中按设置线框的颜色和粗细

手撸源码系列 - go-cache_Seekload的博客-程序员宅基地

点击上方蓝色“Golang来啦”关注我哟加个“星标”,天天 15 分钟,掌握 Go 语言你好,我是小四,你情商高,也可以叫我四哥。什么是 go-cachego-cache 是一个轻量级的基于内存的 K-V 储存组件,内部实现了一个线程安全的 map[string]interface{},适用于单机应用。具备如下功能:线程安全,多 goroutine 并发安全访问;每个 item 可以设置过期时间(..._go-cache 保存

三招解决!电脑密码忘记了怎么办?新手都能学会_电脑密码忘3个最简单的方法-程序员宅基地

电脑密码忘记了,怎么重置密码呢?这个问题也许困扰了我们很多朋友。今天告诉大家一个最简单的方法,新手都能学会,无需重装系统,三招解决!U盘实战:可以到东方联盟论坛下载专版操作系统+老毛挑,制作好U盘引导,把U盘插入电脑。所需工具:东方联盟专版操作系统+引导盘具体方法如下:第一步:设置U盘或光盘优先引导,我们首先进入BIOS。本人利用虚拟机演习(光盘引导),按F2进入BIOS界面,设置引导顺序先。我们找到Boot项,进去后,我们会默认看到Hard Drive ,是硬盘引导的意思。把U盘和光盘引导_电脑密码忘3个最简单的方法

Cocos2d-x中触摸事件-程序员宅基地

理解一个触摸事件能够从时间和空间双方面考虑。1、触摸事件的时间方面触摸事件的在时间方面,例如以下图所看到的。能够有不同的“按下”、“移动”和“抬起”等阶段,表示触摸是否刚刚開始、是否正在移动或处于精巧状态,以及何时结束,也就是手指何时从屏幕抬起。此外。触摸事件的不同阶段都能够有单点触摸或多点触摸,是否支持多点触摸还要看设备和平台。 触摸事件有两个事件监听器:EventListenerTouch...

ftp、sftp、vsftp、vsftpd这四个的区别_vsftp和sftp的区别-程序员宅基地

ftp 是File Transfer Protocol的缩写,文件传输协议,用于在网络上进行文件传输的一套标准协议,使用客户/服务器模式。它属于网络传输协议的应用层。了解更多ftpsftp 是SSH File Transfer Protocol的缩写,安全文件传输协议;了解更多sftpvsftp 是一个基于GPL发布的类Unix系统上使用的ftp服务器软件,它的全称是Very ..._vsftp和sftp的区别

随便推点

关于小程序中给组件设置样式的方式_微信小程序 view 右上推荐样式-程序员宅基地

可通过class和style来设置二者区别是下面文字是转载的别人写的,链接是https://blog.csdn.net/qq_34596739/article/details/79792398因为在开发小程序时,只有部分在做毕业设计时候的前端经验,很多问题都是遇到后才了解到。在制作前端的过程中遇到了修改css样式不起作用的问题。因为代码是从网络上拷贝下来的,最开始如下:{{item.c..._微信小程序 view 右上推荐样式

mybatis全局配置&映射文件-程序员宅基地

全局配置文件全局配置文件结构properties属性可在外部配置(db.properties),且可动态替换,亦可在子元素property中配置内部配置:外部配置settings 设置改变mybatis的运行时行为<settings> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/>

【JAVA代码审计】————4、XSS_java 代码扫描反射型xss-程序员宅基地

前言最近几天比较忙,没有连载文章,今天正好有时间就写写吧,连载的都是基础的漏洞,大神路过留个脚印就可以撤了,哈哈,俗话说不喜勿喷,那咱们就开干!跨站脚本(XSS)原理先看下百度百科对XSS的介绍:跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS。恶意攻击者往W..._java 代码扫描反射型xss

JLU -acm 代码库(校园版)----------Graph 图论-程序员宅基地

================================================DAG的深度优先搜索标记INIT:edge[ ] [ ]邻接矩阵;pre[ ],post[ ],tag置为0;CALL:dfstag(i,n);pre/post:开始/结束时间================================================in

Notepad++-程序员宅基地

常用功能: 1.列编辑 ALT+鼠标选中 2.代码提示 默认的代码自动完成快捷键是Ctrl+Enter。可以在首选项中的备份与自动完成选项卡选中所有的输入均启用自动完成选项和输入时提示函数参数选项,当然这些都可以按照自己的习惯配置。 3.颜色标记功能 颜色标记还很多情况还是比较方便的,使用方法是选中需要标记的文本,右键选择标记的格式。同样方法也可以清除格式。按Ct

小猫统计画股票K线图_leaflet 画线-程序员宅基地

1、首先导入融易汇数据,比如导入000777,中核科技。 2、工具——金融分析。下拉列表中选择SZ000777_leaflet 画线

推荐文章

热门文章

相关标签