SringBoot自定义启动器

1、启动器命名规范

1、官方:

  1. 格式:spring-boot-starter-xxxx
  2. 样例:spring-boot-starter-web

2、自定义

  1. 格式:xxxx-spring-boot-starter
  2. 样例:myself-spring-boot-starter

2、启动器说明

1、启动器其实是两个相互依赖的项目,一个控制版本,一个提供服务。

3、开发步骤

1、建立一个空项目。

在这里插入图片描述
在这里插入图片描述

2、空项目里建立两个模块(一个自动启动器 一个自动配置类)

1、建立第一个module:myself-spring-boot-starter
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2、建立第二个module:myself-spring-boot-starter -autoconfuigure(spring-boot项目)
在这里插入图片描述

3、自动配置项目结构

在这里插入图片描述

4、开发步骤

1、自动配置类模块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">
    <modelVersion>4.0.0</modelVersion>

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

    <groupId>com.demo</groupId>
    <artifactId>myself-spring-boot-starter-autoconfigure</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>myself-spring-boot-starter-autoconfigure</name>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

</project>


2、编写具体业务——>HelloService

package com.demo;

public class HelloService {
    //注入配置类
    private HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    //具体业务
    public String sayHello(String name){

        return helloProperties.getPrefix()+name+helloProperties.getSubfix();
    }
}

3、编写xxxProperties——>HelloProperties

package com.demo;

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

@ConfigurationProperties(prefix = "demo.hello")//配置文件前缀
public class HelloProperties {
    //注入配置类
    private String prefix;
    private String subfix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSubfix() {
        return subfix;
    }

    public void setSubfix(String subfix) {
        this.subfix = subfix;
    }
}

4、编写自动配置类xxxAutoConfiguration——>HelloServiceAutoConfiguration

package com.demo;

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration//配置类
@EnableConfigurationProperties(HelloProperties.class)//指定配置文件
@ConditionalOnWebApplication//web应用才生效
public class HelloServiceAutoConfiguration {
    //注入绑定Properties配置文件
    @Autowired
    private HelloProperties helloProperties;
    @Bean
    public HelloService helloService(){

        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

5、将启动类放入META-INF下的spring.factories中
在这里插入图片描述
6、启动器里导入自定义的自动配置类
在这里插入图片描述
7、将启动器和自动配置安装到本地maven

在这里插入图片描述
在这里插入图片描述

5、测试自己编写的启动器

1、新建springboot项目

2、导入自定义的启动器
在这里插入图片描述
3、编写yaml

在这里插入图片描述

4、结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值