springboot 自定义 starter 例子

一、背景

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

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

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

二、实现过程

需要:下面我们自定义一个我们自己的 Starter,实现的功能是:为用户提供的字符串添加前辍
与后辍,而前辍与后辍定义在 yml 或 properties 配置文件中。例如,用户输入的字符串为 China,
application.yml 配置文件中配置的前辍为-前缀-,后辍为-后缀-,则最终生成的字符串为
-前缀-China-后缀-。
1、创建一个springboot工程目录结构,注意不要有主启动类,否则其他项目引入starter工程后无法获取到starter中的bean。
在这里插入图片描述
1.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.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zls</groupId>
    <artifactId>wrap-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>wrap-spring-boot-starter</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</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

1.2、创建SomeService

@AllArgsConstructor
public class SomeService {
    private String before;
    private String after;
    public String wrap(String word)
    {
        return before+word+after;
    }
}

1.3、创建SomeServiceProperties.java

@Data
@ConfigurationProperties("some.service")
public class SomeServiceProperties {
    private String before;
    private String after;
}

1.4、创建SomeServiceAutoConfiguration.java

@Configuration
@ConditionalOnClass(SomeService.class)
@EnableConfigurationProperties(SomeServiceProperties.class)
public class SomeServiceAutoConfiguration {
    @Autowired
    private SomeServiceProperties properties;

    @Bean
 @ConditionalOnProperty(name="some.service.enable",havingValue = "true",matchIfMissing = true)
    public SomeService someService(){
        return new SomeService(properties.getBefore(),properties.getAfter());
    }
    @Bean
    @ConditionalOnMissingBean
    public SomeService someService2(){
        return new SomeService("","");
    }
}

1.5、创建spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zls.config.SomeServiceAutoConfiguration

最后maven clean ,maven install后,starter完成。

2.创建springboot工程,只加入web模块,引入自定义starter。结构如下:
在这里插入图片描述
2.1 pom.xml
引入自定义starter

 <dependency>
            <groupId>com.zls</groupId>
            <artifactId>wrap-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

下面是完整内容

<?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.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</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>
        <dependency>
            <groupId>com.zls</groupId>
            <artifactId>wrap-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2 application.yml

some:
  service:
    enable: true
    after: -后缀-
    before: -前缀-

2.3 UserController

@RestController
public class UserController {
    @Resource
    private SomeService someService;

   @RequestMapping("test/{word}")
    public String test(@PathVariable("word")String word){
        return  someService.wrap(word);
    }
}

最后 maven clean ,maven install ,启动测试项目。
请求:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值