SpringBoot如何自定义starters

一、starter :场景启动器

SpringBoot将每个场景都抽取成一个start,通过引入starts,就能使用到相应的场景功能。即使是这样,SpringBoot也不能囊括开发中所有的场景,我们往往需要自定义starters,来简化对SpringBoot的使用。

当我们写好某一个场景的starter,其他开发人员可以直接引用我们写好的starter,而不需要进行过多的配置。

 

二、自定义starters

1、准备

(1)这个场景需要使用到的依赖是什么?
(2)如何编写自动配置

我们参考 WebMvcAutoConfiguration

源码:

 

注意:@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class }) 注解的作用:

是使加了@ConfigurationProperties(prefix = "spring.mvc") 注解生效

 

 

 

2、编写自动配置遵循的规范

其中,@ConditionalOnXXX 表示符合这个条件,就会去实例化该配置类

@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中


自动配置类要能加载
将需要启动就加载的自动配置类,配置在META‐INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\

 

3、编写自动配置的模式

(1)图

 

(2) 分析

(3)例如 spring-boot-starter-web

 

 

四、自定义starters项目搭建

1、创建一个空项目

2、新建一个Modules : Maven工程 作为 启动器

3、新建一个SpringBoot快速启动模块 作为 自动配置类

 

4、修改两个项目的pom依赖文件

(1) dhu-spring-boot-starter

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

    <!--启动器-->
    <dependencies>
        <!--引入自动配置模块,其他开发人员只需要引入我们的启动器就可以-->
        <dependency>
            <groupId>com.dhu.starter</groupId>
            <artifactId>dhu-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

(2)dhu-spring-boot-starter-autoconfigurer

    <groupId>com.dhu.starter</groupId>
    <artifactId>dhu-spring-boot-starter-autoconfigurer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dhu-spring-boot-starter-autoconfigurer</name>
    <description>Demo project for Spring Boot</description>

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

    <!--引入spring-boot-starter;所有starter的基本配置-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

 

5、编写代码:

创建了一个自动配置类 HelloServiceAutoConfiguration,它会给容器中添加 HelloService 组件,

HelloService 中用到的属性跟 HelloProperties 绑定。

同时,也配置了:自动配置类要能加载,将需要启动就加载的自动配置类,配置在META‐INF/spring.factories

(1)HelloProperties 

package com.dhu.starter;

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

/**
 * @author zhou
 * @create 2020/6/1
 */
@ConfigurationProperties(prefix = "dhu.hello")
public class HelloProperties {
    private String prefix;
    private String suffix;

    public String getPrefix() {
        return prefix;
    }

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

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

 

(2)HelloService

package com.dhu.starter;

/**
 * @author zhou
 * @create 2020/6/1
 */
public class HelloService {
    private HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

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

    public String sayHelloDhu(String name){
        return helloProperties.getPrefix() + "-" + name + helloProperties.getSuffix();
    }
}

 

(3)HelloServiceAutoConfiguration

package com.dhu.starter;

import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * @author zhou
 * @create 2020/6/1
 */
@Configuration
@ConditionalOnWebApplication //web应用才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    private HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }
}

 

(4)META-INF/spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.dhu.starter.HelloServiceAutoConfiguration

 

6、将这两个模块安装到Maven仓库

 

五、编写SpringBoot项目进行测试

1、pom.xml

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入自定义的starter-->
        <dependency>
            <groupId>com.dhu.starter</groupId>
            <artifactId>dhu-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

2、application.properties

dhu.hello.prefix=DHU
dhu.hello.suffix=WELCOME

 

3、HelloController

 

4、运行结果

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值