SpringBoot实现自定义starter

本文介绍了如何创建自定义的SpringBoot Starter。首先创建启动器模块,仅依赖自动配置模块;接着创建自动配置模块,包含properties类、服务组件及自动配置类;然后在spring.factories中注册自动配置类;最后通过maven安装到本地仓库,并在测试项目中验证其功能。

步骤:

目录结构:

Step 1、启动器模块(haha-spring-boot-starter)
启动器模块只需依赖于自动配置模块,无需做任何配置
新建Module,maven
在这里插入图片描述
其中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.haha.starter</groupId>
    <artifactId>haha-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--启动器-->
    <dependencyManagement>
        <dependencies>
        <!--引入自动配置模块-->
            <dependency>
                <groupId>com.haha.starter</groupId>
                <artifactId>haha-spring-boot-starter-autoconfigurer</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>     


</project>

Step 2、自动配置模块(haha-spring-boot-autoconfigurer)
(1)新建Module,Spring Initializr(引入web模块)
在这里插入图片描述
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 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.4.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.haha.starter</groupId>
	<artifactId>haha-spring-boot-starter-autoconfigurer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>haha-spring-boot-starter-autoconfigurer</name>
	<description>Demo project for Spring Boot</description>

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

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

		<!--            所有starter的基本配置:spring-boot-starter-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
			<version>2.4.0</version>
		</dependency>
	</dependencies>
</project>

(2)创建于自动配置映射的properties类(HelloProperties)

package com.haha.starter;

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

@Component
@ConfigurationProperties(prefix = "haha.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;
    }
}

(3)声明组件(HelloService)
该类从HelloProperties类读取字符串的头、尾,一起和name组成新的字符串并返回

package com.haha.starter;

public class HelloService {
    HelloProperties helloProperties;

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

    public String sayHelloToHaha(String name){
        return helloProperties.getPrefix()+"-"+ name+"-"+helloProperties.getSuffix();
    }
}

(4)创建自动配置类(HelloServiceAutoConfiguration)

package com.haha.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
@ConditionalOnWebApplication//只有web应用才生效
@EnableConfigurationProperties(HelloProperties.class)//使HelloProperties配置类生效
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean//将HelloService注入容器中
    public HelloService HelloService(){
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }
}

Step 3、在resources目录下,新建 META-INF文件夹,新建spring.factories 文件
在这里插入图片描述

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.haha.starter.HelloServiceAutoConfiguration

com.haha.starter.HelloServiceAutoConfiguration是HelloServiceAutoConfiguration自动配置类的位置,写入spring.factories 中,系统启动后才可以加载自动配置类
Step 4、将上面两个模块下载到maven仓库中,供以后使用,先install自动配置模块haha-spring-boot-autoconfigurer(因为启动器中依赖自动配置模块),后installhaha-spring-boot-starter启动器模块
在这里插入图片描述

Step 5、测试
新建Spring Initializr(引入web应用),pom引入启动器模块

        <dependency>
            <groupId>com.haha.starter</groupId>
            <artifactId>haha-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

从maven中下载自定义starter
在这里插入图片描述
在这里插入图片描述
如果pom文件显示引入成功,但在External Libraries中没有找到,则file-Project Structure手动引入即可。
新建controller

package com.starter.test.controller;

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

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String sayHello(){
        String selina = helloService.sayHelloToHaha("Selina");
        return selina;
    }
}

自定义配置application.properties

haha.hello.prefix=12345
haha.hello.suffix=67890

访问界面
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值