SpringBoot自动配置:自定义一个启动器Stater

创建一个自定义的Starter:

Springboot的出现极大的简化了开发人员的配置,而这之中的一大利器便是springboot的starter,starter是springboot的核心组成部分,springboot官方同时也为开发人员封装了各种各样方便好用的starter模块,例如:

  • spring-boot-starter-web//spring MVC相关
  • spring-boot-starter-aop //切面编程相关
  • spring-boot-starter-cache //缓存相关

(1)自定义的Starter的要求:
官网要求创建两个module ,一个是autoconfigure module 一个是 starter module ,其中 starter module 依赖 autoconfigure module,主要起到一个传递依赖的作用(可以省略)。

(2)命名规范:
官方推出的starter 以spring-boot-starter-xxx的格式来命名,第三方开发者自定义的starter则以xxxx-spring-boot-starter的规则来命名。

1 创建一个autoconfiguration模块

首先创建一个空项目,然后再创建一个SpringBoot模块,将该类中的启动类以及测试类都删除掉。
在这里插入图片描述

(1)pom配置

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


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

pom文件中只保留这两个依赖,其中spring-boot-starter为启动器的基本依赖,而spring-boot-configuration-processor能在配置文件时进行提示:
在这里插入图片描述

(2)编写配置

package com.glp.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "diy.hello")
public class Properties {
    
    
    private String pre;
    private String suf;

    public String getPre() {
    
    
        return pre;
    }

    public void setPre(String pre) {
    
    
        this.pre = pre;
    }

    public String getSuf() {
    
    
        return suf;
    }

    public void setSuf(String suf) {
    
    
        this.suf = suf;
    }
}

@ConfigurationProperties:该配置类和SpringBoot中的application.properties/application.yaml配置文件相关联从而进行属性注入。

(3)编写服务

package com.glp.service;

import com.glp.properties.Properties;

public class HelloService {
    
    

    Properties properties;

    public Properties getProperties() {
    
    
        return properties;
    }

    public void setProperties(Properties properties) {
    
    
        this.properties = properties;
    }
    public String sayHello(){
    
    
        return properties.getPre()+"offer"+properties.getSuf();
    }
}

(4)自动配置
在自动配置类中对服务和配置类进行一个整合,相当于将配置好的类传入到服务类中去。

package com.glp.config;

import com.glp.properties.Properties;
import com.glp.service.HelloService;
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;


@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(Properties.class)
public class HelloServiceAutoConfiguration {
    
    
    @Autowired
    Properties properties;

    @Bean
    public HelloService getHelloService(){
    
    
        HelloService service = new HelloService();
        service.setProperties(properties);
        return service;
    }
}
  • @ConditionalOnXXXX:用来判断某些条件是否满足,以此来决定该自动配置是否生效
  • @Bean:将服务和配置类整合起来,配置成组件,注入到容器中,供用户调用。
  • @EnableConfigurationProperties :让xxxProperties生效并加入到容器中

(5)spring.factories

编写一个自己的META-INF文件,在项目启动扫描包时,会自动加载spring.factories文件下指定的配置类。

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.glp.config.HelloServiceAutoConfiguration

2 创建启动器stater模块

这个模块只是用来做传递依赖的,所以我们新建一个Maven模块就够用了。
在这里插入图片描述

只需要在pom中引入我们自定义的autoconfigure就可以了,起到一个依赖的作用。当然如果不想将 自动配置代码和依赖项管理 分离开来,也可以只用一个模块。

    <dependencies>
        <dependency>
            <groupId>com.glp</groupId>
            <artifactId>diy-spring-boot-autoconfiguration</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

注意:
引入自动配置类,直接复制自动配置类pom.xml的groupId,artifactId,version就好了。
在这里插入图片描述

3 将自动配置模块和启动类模块安装到Maven仓库中

在这里插入图片描述

4 编写测试类

下面我们新建一个SpringBoot模块测试类,测试类中引入Web模块就好了,不用引入太多。
在这里插入图片描述

(1)在测试类中引入依赖:

     <dependency>
            <groupId>com.glp</groupId>
            <artifactId>diy-spring-boot-stater</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

(2)applicatrion.properties配置

diy.hello.pre=where
diy.hello.suf=offer

(3)编写一个controller:

package com.glp.controller;


import com.glp.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    
    
    @Autowired
    HelloService helloService;

    @GetMapping("/say")
    public String helloController(){
    
    
        return helloService.sayHello();
    }
}

(4)运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/glpghz/article/details/108427941
今日推荐