SpringBoot自定义starter

1.自定义starter的命名规范

官方命名如下图:一般都是spring-boot-starter-*,外部的自定义starter推荐使用*-spring-boot-starter

2.项目结构

如果我们使用就只需要引入下面的就可以了

dependency>
    <groupId>com.custom.starter</groupId>
    <artifactId>custom-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

custom-spring-boot-starter  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>com.custom.starter</groupId>
    <artifactId>custom-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.test.custom.starter</groupId>
            <artifactId>spring-boot-customstarter-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

spring-boot-customstarter-autoconfigure 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>com.test.custom.starter</groupId>
    <artifactId>spring-boot-customstarter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-customstarter-autoconfigure</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

3.编写

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(CustomStarterProperties.class)
public class CustomStarterAutoConfiguration {

    @Autowired
    CustomStarterProperties customStarterProperties;

    @Bean
    public CustomStarterService getCustomStarterService() {
        CustomStarterService customStarterService = new CustomStarterService();
        customStarterService.setCustomStarterProperties(customStarterProperties);
        return customStarterService;
    }
}
@ConfigurationProperties(prefix = "spring.boot.custom.starter")
public class CustomStarterProperties {

    private String param;

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }
}
@Configuration
@ConditionalOnWebApplication //只有在web应用下才可以使用,不懂的可以看spring-boot-autoconfigure中的XXXXXAutoConfiguration中有很多@ConditionalOnXXXXX
@EnableConfigurationProperties(CustomStarterProperties.class)
public class CustomStarterAutoConfiguration {

    @Autowired
    CustomStarterProperties customStarterProperties;

    @Bean
    public CustomStarterService getCustomStarterService() {
        CustomStarterService customStarterService = new CustomStarterService();
        customStarterService.setCustomStarterProperties(customStarterProperties);
        return customStarterService;
    }
}
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.test.custom.starter.CustomStarterAutoConfiguration

按照以上编写,一个最简单的自定义starter就写好了,在其他项目导入依赖,然后注入XXXService就可以使用了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值