SpringBoot——手把手教你自定义starter


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 请求,访问成功

在这里插入图片描述

发布了15 篇原创文章 · 获赞 4 · 访问量 726

猜你喜欢

转载自blog.csdn.net/weixin_38938338/article/details/104806485