Springboot自定义启动器(starter)

1.步骤总结

springboot自定义启动器(starter)总共分3步

  1. 引入自动配置依赖
  2. 编写配置类注入自定义Bean
  3. 编写spring.factories文件开启自动配置

2.实战

首先看一下完整的目录结构

在这里插入图片描述

接下来,就开始创建一个自己的启动器。

第一步:引入自动配置依赖

创建一个maven工程,pom文件添加依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/>
    </parent>
    <modelVersion>4.0.0</modelVersion>

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

    <properties>
        <skipTests>true</skipTests>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional><!--表示依赖不传递-->
        </dependency>
    </dependencies>

第二步:编写配置类注入自定义Bean

  1. 创建配置对象,@ConfigurationProperties声明这个是一个配置对象;这里简单声明了2个属性,配置文件中的配置就是:hello.starter.start=xxxx(写上你的配置属性)
@ConfigurationProperties(prefix = "hello.starter")
public class HelloProperties {
    private String start;
    private String end;

    public String getStart() {
        return start;
    }

    public void setStart(String start) {
        this.start = start;
    }

    public String getEnd() {
        return end;
    }

    public void setEnd(String end) {
        this.end = end;
    }

    @Override
    public String toString() {
        return "HelloProperties{" +
                "start='" + start + '\'' +
                ", end='" + end + '\'' +
                '}';
    }
}

2.创建一个自定义的Bean对象,将配置类使用构造方法注入到自定义的Bean实体中

public class HelloService {
    //读取配置类里面的配置信息
    private HelloProperties helloProperties;

    public HelloService(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public void sayHello(){
        System.out.println("Hello Starter!");
        System.out.println(helloProperties);
    }
}

3.创建HelloAutoConfiguration,名称自定义,声明一个配置类,使用 @Bean注入到Spring容器

@Configuration//申明这是一个配置类
@ConditionalOnWebApplication//引用启动器的项目是web应用此自动配置模块才生效
@EnableConfigurationProperties(HelloProperties.class)//加载配置对象到容器
public class HelloAutoConfiguration {
    //注入配置对象
    @Autowired
    HelloProperties helloProperties;

    @Bean//方法返回结果对象加载到容器
    public HelloService helloService() {
        //新建业务逻辑处理对象,并返回加载到容器中,
        // 这样引用启动器的项目就可以 @Autowired  HelloService 对象直接使用了
        HelloService helloService = new HelloService(helloProperties);
        return helloService;
    }

}

第三步:编写spring.factories文件开启自动配置

在resources目录下创建META-INF \spring.factories ,此处配置的是配置类(HelloAutoConfiguration)的全类名路径,内容如下

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.hello.config.HelloAutoConfiguration

至此,我们的启动器就编写完成了。接下来测试一下

3.测试

同样创建一个空的maven工程,目录结构

在这里插入图片描述

1.引入自定义启动器的maven坐标,pom文件如下

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hello</groupId>
    <artifactId>test-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--这个依赖就是自定义的启动器-->
        <dependency>
            <groupId>com.hello</groupId>
            <artifactId>hello-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.配置文件application.properties,配置自定义Bean的属性,当然也可以直接在制作启动器的时候给出默认值。
在这里插入图片描述

hello.starter.start=start
hello.starter.end=end

3.启动类

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

4.写个测试类测试

import com.hello.config.HelloService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class TestMyStarter {

    @Autowired
    private HelloService helloService;

    @Test
    public void TestMyStarter(){
        helloService.sayHello();
    }
}

运行
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值