spring boot专题(三)——自定义starter

系列文章目录

spring boot专题(一)——spring boot快速上手
spring boot专题(二)——spring boot整合第三方插件
spring boot专题(三)——自定义starter
spring boot专题(四)——spring boot源码深入分析


什么是spring boot starter

SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。starter让我们摆脱了各种依赖库的处理,需要配置各种信息的困扰。SpringBoot会自动通过classpath路径下的类发现需要的Bean,并注册进IOC容器。SpringBoot提供了针对日常企业应用研发各种场景的spring-boot-starter依赖模块。所有这些依赖模块都遵循着约定成俗的默认配置,并允许我们调整这些配置,即遵循“约定大于配置”的理念

为什么要自定义starter

在我们的日常开发工作中,经常会有一些独立于业务之外的配置模块,我们经常将其放到一个特定的包下,然后如果另一个工程需要复用这块功能的时候,需要将代码硬拷贝到另一个工程,重新集成一遍,麻烦至极。如果我们将这些可独立于业务代码之外的功配置模块封装成一个个starter,复用的时候只需要将其在pom中引用依赖即可,SpringBoot为我们完成自动装配。

自定义starter的命名规则

SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。

案例

1.创建并配置自定义starter的项目

1.1项目结构如下

在这里插入图片描述

1.2引入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">
    <parent>
        <artifactId>springboottest</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yemuxia</groupId>
    <artifactId>demo-spring-boot-starter</artifactId>
    <version>0.0.1-RELEASE</version>
    <name>demo-spring-boot-starter</name>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
         </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
            <!--注意,打普通jar包的时候一定得排除父pom中引入的插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

1.3定义一个实体类映射配置信息

@ConfigurationProperties(prefix = “demo”) 它可以把相同前缀的配置信息通过配置项名称映射成实体类,比如我们这里指定 prefix = “demo” 这样,我们就能将以demo为前缀的配置项拿到了。
其实这个注解很强大,它不但能映射成String或基本类型的变量。还可以映射为List,Map等数据结构。

package com.yemuxia.properties;


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

/**
 * @author 史凯强
 * @date 2021/09/28 14:40
 * @desc
 **/
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
    
    
    private String sayWhat;
    private String toWho;

    public String getSayWhat() {
    
    
        return sayWhat;
    }

    public void setSayWhat(String sayWhat) {
    
    
        this.sayWhat = sayWhat;
    }

    public String getToWho() {
    
    
        return toWho;
    }

    public void setToWho(String toWho) {
    
    
        this.toWho = toWho;
    }
}

1.4定义一个Service

package com.yemuxia.service;

/**
 * @author 史凯强
 * @date 2021/09/28 14:43
 * @desc
 **/
public class DemoService {
    
    
    public String sayWhat;
    public String toWho;
    public DemoService(String sayWhat, String toWho){
    
    
        this.sayWhat = sayWhat;
        this.toWho = toWho;
    }
    public String say(){
    
    
        return this.sayWhat + "!  " + toWho;
    }
}

1.5定义一个配置类

这里,我们将DemoService类定义为一个Bean,交给Ioc容器。
▲ @Configuration 注解就不多说了。
▲ @EnableConfigurationProperties 注解。该注解是用来开启@ConfigurationProperties 注解配置Bean的支持。也就是@EnableConfigurationProperties注解告诉Spring Boot 能支持@ConfigurationProperties。
当然了,也可以在 @ConfigurationProperties 注解的类上添加 @Configuration 或者 @Component 注解
▲ @ConditionalOnProperty 注解控制 @Configuration 是否生效。简单来说也就是我们可以通过在yml配置文件中控制 @Configuration 注解的配置类是否生效。

package com.yemuxia.autoconfig;

import com.yemuxia.properties.DemoProperties;
import com.yemuxia.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
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 史凯强
 * @date 2021/09/28 14:44
 * @desc 配置类
 **/
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
@ConditionalOnProperty(
        prefix = "demo",
        name = "isopen",
        havingValue = "true"
)
public class DemoAutoConfig {
    
    
    @Autowired
    private DemoProperties demoProperties;

    @Bean(name = "demo")
    public DemoService demoService(){
    
    
        return new DemoService(demoProperties.getSayWhat(), demoProperties.getToWho());
    }
}

1.6新建META-INF文件夹,然后创建spring.factories文件,

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.yemuxia.autoconfig.DemoAutoConfig

2.打成无需执行的jar包,将其安装至maven仓库中

2.1执行maven clean install

在这里插入图片描述

在这里插入图片描述

2.2去仓库中找

在这里插入图片描述

3.创建测试项目去使用自定义的starter

3.1测试项目springboot_test_autoconfig目录结构

在这里插入图片描述

3.2pom依赖文件如下

<?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">
    <parent>
        <artifactId>springboottest</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot_test_autoconfig</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.yemuxia</groupId>
            <artifactId>demo-spring-boot-starter</artifactId>
            <version>0.0.1-RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

3.3application.yaml内容如下

demo:
  isopen: true
  say-what: hello
  to-who: yemuxia
server:
  port: 9000

3.4controller文件内容

package com.yemuxia.com.yemuxia.controller;

import com.yemuxia.service.DemoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 史凯强
 * @date 2021/09/28 15:24
 * @desc
 **/
@RestController
public class TestController {
    
    

    @Resource(name = "demo")
    private DemoService demoService;

    @GetMapping("/say")
    public String sayWhat(){
    
    
        return demoService.say();
    }

}

3.5启动项目并访问

在这里插入图片描述

Guess you like

Origin blog.csdn.net/yemuxiaweiliang/article/details/120530425