使用SpringBoot自定义starter详解

 更新时间:2021年05月10日 11:15:34   作者:天涯不归客  
这篇文章主要介绍了使用Spring Boot自定义starter详解,文中有非常详细的代码示例,对正在学习java的小伙伴们有很好地帮助哟,需要的朋友可以参考下

一、新建一个工程

工程由xxx-sprig-boot-starterxxx-sprig-boot-starter-configure两个模块组成;

在这里插入图片描述

xxx-sprig-boot-starter模块

  • 只用来做依赖导入
  • 依赖于 xxx-sprig-boot-starter-configure模块,没有实际代码
<?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.ander</groupId>
    <artifactId>ander-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--依赖ander-spring-boot-starter-configure工程-->
    <dependencies>
        <dependency>
            <groupId>com.ander</groupId>
            <artifactId>ander-spring-boot-starter-configure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

在这里插入图片描述

xxx-sprig-boot-starter-configure模块

  • 专门自动配置模块
  • 依赖于spring-boot-starter-web
<?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.3.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.ander</groupId>
    <artifactId>ander-spring-boot-starter-configure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ander-spring-boot-starter-configure</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-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

在这里插入图片描述 

二、xxx-sprig-boot-starter-configure模块自动配置编码

2.1 服务层编码

/**
 * Service层
 *
 * @Author: Ander
 * @Date: 2021-05-04
 */
public class HelloService {

    private HelloServiceProperties helloServiceProperties;

    public String helloService(String name) {
        return helloServiceProperties.getPrefix() + " "+ name + " " + helloServiceProperties.getSuffix();
    }

    public HelloServiceProperties getHelloServiceProperties() {
        return helloServiceProperties;
    }

    public void setHelloServiceProperties(HelloServiceProperties helloServiceProperties) {
        this.helloServiceProperties = helloServiceProperties;
    }
}

2.2 属性配置类编码

/**
 * 属性配置类
 *
 * @Author: Ander
 * @Date: 2021-05-04
 */
@ConfigurationProperties(prefix = "com.ander")
public class HelloServiceProperties {

    private String prefix = "hi";
    private String suffix = "hello world";

    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;
    }
}

2.3 starter自动配置类编码

@EnableConfigurationProperties({HelloServiceProperties.class})作用:让xxxProperties生效加入到容器中

/**
 * 自定义starter自动配置类
 *
 * @Author: Ander
 * @Date: 2021-05-04
 */
@Configuration
@ConditionalOnWebApplication // 指定web应用才生效
@EnableConfigurationProperties({HelloServiceProperties.class})
public class HelloServiceAutoConfigure {

    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setHelloServiceProperties(helloServiceProperties);
        return helloService;
    }
}

2.4 添加自动配置类到META-INF路径下

在这里插入图片描述

2.5 将工程安装到本地

注意先安装xxx-spring-boot-starter-configure,再安装xxx-spring-boot-starter

在这里插入图片描述

三、新建一个工程测试自定义starter

3.1 编写controller层

/**
 * starter测试控制类
 *
 * @Author: Ander
 * @Date: 2021-05-05
 */
@RestController
public class StarterTestController {

    @Autowired
    private HelloService helloService;

    @GetMapping("hello")
    public String hello(String name) {
        return helloService.helloService(name);
    }
}

3.2 编写配置文件

server.port=8888
com.ander.prefix=HI
com.ander.suffix=HELLO WORLD

四、测试结果

4.1 使用starter默认配置

在这里插入图片描述

4.2 使用自定义配置

在这里插入图片描述

到此这篇关于使用Spring Boot自定义starter详解的文章就介绍到这了,更多相关Spring Boot自定义starter内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring Boot 异步框架的使用详解

    Spring Boot 异步框架的使用详解

    这篇文章主要介绍了Spring Boot 异步框架的使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • Java 普通代码块静态代码块执行顺序(实例讲解)

    Java 普通代码块静态代码块执行顺序(实例讲解)

    下面小编就为大家带来一篇Java 普通代码块静态代码块执行顺序(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • mybatis-plus QueryWrapper 添加limit方式

    mybatis-plus QueryWrapper 添加limit方式

    这篇文章主要介绍了mybatis-plus QueryWrapper 添加limit方式,具有很好的参考价值,希望对大家有所
    2022-01-01
  • 教你用Java SpringBoot如何解决跨域

    教你用Java SpringBoot如何解决跨域

    在项目开发中,时常会遇到跨域问题,本文主要介绍了五种解决跨域的方法,使用最多的是第三种,需要的朋友们下面随着小编来一起学习学习吧
    2021-09-09
  • SpringBoot多模块项目框架搭建过程解析

    SpringBoot多模块项目框架搭建过程解析

    这篇文章主要介绍了SpringBoot多模块项目框架搭建过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • 详解mybatis-plus实体类中字段和数据库中字段名不对应解决办法

    详解mybatis-plus实体类中字段和数据库中字段名不对应解决办法

    这篇文章主要介绍了详解mybatis-plus实体类中字段和数据库中字段名不对应解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Spring Boot中使用Spring-data-jpa的配置方法详解

    Spring Boot中使用Spring-data-jpa的配置方法详解

    今天小编就为大家分享一篇关于Spring Boot中使用Spring-data-jpa的配置方法详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Java详解数据类型的定义与使用

    Java详解数据类型的定义与使用

    Java 是一种类型安全语言,编译器存储在变量中的数值具有适当的数据类型。学习任何一种编程语言都要了解其数据类型,本文将详细介绍 Java 中的数据类型
    2022-04-04
  • java模拟客户端向服务器上传文件

    java模拟客户端向服务器上传文件

    这篇文章主要为大家详细介绍了java模拟客户端向服务器上传文件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • Java通过动态规划设计股票买卖最佳时机

    Java通过动态规划设计股票买卖最佳时机

    动态规划可谓是大名鼎鼎,笔试面试中的高频考点,也是重点难点,动态规划类型题目灵活多变,难度系数也相对较高,往往我们做不好动态规划的题目就会与心仪的offer失之交臂,本篇文章我们就一起来研究一下动态规划设计股票买卖最佳时机
    2022-10-10

最新评论