使用 Spring Boot 自定义 Starter

本文详细介绍了如何创建一个自定义的 Spring Boot Starter,包括配置 Maven 项目、定义属性类、创建配置类和自动配置类,以及在 META-INF 中注册自动配置类。通过这种方式,可以实现对应用的自动配置,并在其他 Spring Boot 项目中复用。最后,文章展示了如何在新的 Spring Boot 项目中引入该 Starter,并进行测试。
摘要由CSDN通过智能技术生成
1、创建一个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.yyq</groupId>
    <artifactId>hello-spring-boot-starter-autoconfigure</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
    
		<!-- spring-boot-autoconfigure此jar包中包含大量核心注解,包含条件注解等 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

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

    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
</project>
2、创建properties属性类读取属性:
package com.yyq;

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

/**
 * @author YangYuQian
 * @date 2021/8/30 9:23
 * @description: 属性类
 */

/**
 * @ConfigurationProperties
 * 配置此注解可以自动导入application.properties配置文件中的属性,
 * 前提需要指定属性前缀prefix。
 * 如果application.properties文件中未指定相应属性,
 * 便使用默认的,如上name=“wisdom”,hobby=“basketball”.
 */
@ConfigurationProperties(prefix = "com.yyq")
public class HelloServiceProperties {

    private String name="yuzi";
    private String hobby="playgame";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
}

3、创建配置类
package com.yyq;

/**
 * @author YangYuQian
 * @date 2021/8/30 9:32
 * @description: 配置类
 */
public class HelloServiceConfiguration {
    private String name;
    private String hobby;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

}

3、创建自动配置类
package com.yyq;

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.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author YangYuQian
 * @date 2021/8/30 9:33
 * @description: 自动配置类
 */

/**
 * @Configuration: 表明此类是一个配置类,将变为一个bean被spring进行管理。
 * @EnableConfigurationProperties: 启用属性配置,将读取HelloServiceProperties里面的属性。
 * @ConditionalOnClass: 当类路径下面有HelloServiceConfiguration此类时,自动配置。
 * @ConditionalOnProperty: 判断指定的属性是否具备指定的值。
 * @ConditionalOnMissingBean: 当容器中没有指定bean是,创建此bean。
 */
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloServiceConfiguration.class)
@ConditionalOnProperty(prefix = "com.yyq", value = "enabled", matchIfMissing = true)
public class HelloServiceAutoConfiguration {
    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    @ConditionalOnMissingBean(HelloServiceConfiguration.class)
    public HelloServiceConfiguration helloServiceConfiguration() {
        HelloServiceConfiguration helloService = new HelloServiceConfiguration();
        helloService.setName(helloServiceProperties.getName());
        helloService.setHobby(helloServiceProperties.getHobby());
        return helloService;
    }
}

5、在resources文件夹中新建一个META-INF文件,在里面创建spring.factories文件,注册自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.yyq.HelloServiceAutoConfiguration
项目结构如下:

在这里插入图片描述

自定义的hello-spring-boot-starter-autoconfigure编写完毕,springboot官方建议对于非官方的starter命名方式为xxx-spring-boot-starter。执行mvn clean install将项目打成一个jar包。
6、新建一个springboot项目,在pom文件中添加刚刚打包的jar:
<dependency>
	  <groupId>com.yyq</groupId>
	  <artifactId>hello-spring-boot-starter-autoconfigure</artifactId>
	  <version>1.0-SNAPSHOT</version>
	  <scope>compile</scope>
</dependency>
7、编写测试类
package com.yyq.springboot_test;

import com.yyq.HelloServiceConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author YangYuQian
 * @date 2021/8/30 14:52
 * @description:
 */
@RestController
@RequestMapping("/my")
public class TestController {
    @Resource
    private HelloServiceConfiguration HelloServiceConfiguration;

    @RequestMapping("/get")
    public void getUser() {
        String hobby = HelloServiceConfiguration.getHobby();
        String name = HelloServiceConfiguration.getName();
        System.out.println(name + "\t" + hobby);
    }
}

8、在application.properties修改属性信息,不修改就是原来默认的属性值
com.yyq.hobby=study
com.yyq.name=yyq

9、启动项目访问接口 http://localhost:8080/my/get( 写自己的地址)

在这里插入图片描述

项目结构如下:

在这里插入图片描述

编译后的starter:

在这里插入图片描述

里面的json文件

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值