springboot自定义starter

springboot自定义starter

概述

适用场景

starter即“场景”,可以将某一应用场景的模块整体封装起来,方便复用。

starter的命名规则

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

代码地址

https://gitee.com/dev-liufq/starter-demo-starter.git

实现

新建工程

目录结构

屏幕快照 2021-09-26 下午7.19.00

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>starter-demo-starter</artifactId>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.18.RELEASE</version>
    </parent>

    <dependencies>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

属性对象

DemoProperties

package com.demo.properties;

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

/**
 * @author liufq
 * @description:
 * @date 2021/9/26
 */
@ConfigurationProperties(prefix = DemoProperties.PREFIX)
public class DemoProperties {

    public static final String PREFIX = "demo";

    private boolean enabled;
    private String name;

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getName() {
        return name;
    }

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

业务逻辑

DemoService.java

package com.demo.service;

/**
 * @author liufq
 * @description:
 * @date 2021/9/26
 */
public class DemoService {

    private String name;

    public DemoService(String name) {
        this.name = name;
    }

    public void sayHello() {
        System.out.println("hello," + name);
    }
}

配置类

/**
 * @author liufq
 * @description:
 * @date 2021/9/26
 */
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
@ConditionalOnProperty(prefix = DemoProperties.PREFIX, value = "enabled", havingValue = "true")
public class DemoAutoConfiguration {
    @Autowired
    private DemoProperties demoProperties;

    @Bean
    public DemoService demoService() {
        return new DemoService(demoProperties.getName());
    }
}

spring.factories

新建META-INF文件夹,创建spring.factories文件。springBoot通过spring.factories加载Starter。

img

测试

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

test/resources/application.yml

demo:
  enabled: true
  name: demo

测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class DemoStarterTest {
    @Autowired
    private DemoService demoService;

    @Test
    public void test() {
        demoService.sayHello();
    }
}

测试不启用的情况

demo:
  enabled: false

使用

打包

application.yml不要放在main/resources下,会被打入jar包。

install到本地仓库,如果需要提供给别人使用,可以放入公共仓库

引入

在其他项目中引入starter

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

    <artifactId>ch02</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>starter-demo-starter</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

</project>

测试

@SpringBootApplication
public class Ch02Application {
    public static void main(String[] args) {
        SpringApplication.run(Ch02Application.class);
    }
}

src/main/resources/application.yml

demo:
  enabled: true
  name: hahaha

测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Ch02Application.class)
public class DemoServiceTest {
    @Autowired
    private DemoService demoService;

    @Test
    public void test() {
        demoService.sayHello();
    }
}
posted @ 2021-09-27 16:26  dev_liufq  阅读(425)  评论(0编辑  收藏  举报