SpringBoot06:自定义start

我们分析完毕了源码以及自动装配的过程,我们可以尝试自定义一个启动器来玩玩!

说明

启动器模块是一个 空 jar 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库;

命名归约:

官方命名:

  • 前缀:spring-boot-starter-xxx
  • 比如:spring-boot-starter-web....

自定义命名:

  • xxx-spring-boot-starter
  • 比如:mybatis-spring-boot-starter

编写启动器

  1. 在IDEA中新建一个空项目 spring-boot-starter-diy

  2. 新建一个普通Maven模块:godfrey-spring-boot-starter

  3. 新建一个Springboot模块:godfrey-spring-boot-starter-autoconfigure

  1. 点击apply即可,基本结构

image-20200528103104503

  1. 在我们的 starter 中 导入 autoconfigure 的依赖!
<!--启动器-->
<dependencies>
    <!--引入自动配置模块-->
    <dependency>
        <groupId>com.godfrey</groupId>
        <artifactId>godfrey-spring-boot-starter-autoconfigure</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
  1. 将 autoconfigure 项目下多余的文件都删掉,pom中只留下一个 starter,这是所有的启动器基本配置!

  1. 编写HelloProperties 配置类
package com.godfrey;

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

/**
 * description : 服务properties配置类
 *
 * @author godfrey
 * @since 2020-05-28
 */
//前缀 godfrey.hello
@ConfigurationProperties(prefix = "godfrey.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;
    }
}
  1. 编写一个自己的服务
package com.godfrey;

/**
 * description : 自定义服务
 *
 * @author godfrey
 * @since 2020-05-28
 */
public class HelloService {

    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.getSuffix();
    }
}
  1. 编写我们的自动配置类并注入bean,测试!
package com.godfrey;

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;

/**
 * description : 配置类
 *
 * @author godfrey
 * @since 2020-05-28
 */
@Configuration
@ConditionalOnWebApplication //web应用生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService() {
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }
}
  1. 在resources编写一个自己的 META-INF\spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.godfrey.HelloServiceAutoConfiguration
  1. 编写完成后,可以安装到maven仓库中!

新建项目测试我们自己写的启动器

  1. 新建一个SpringBoot 模块

  1. 导入我们自己写的启动器
<dependency>
    <groupId>com.godfrey</groupId>
    <artifactId>godfrey-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
  1. 编写一个Controller 进行测试我们自己的写的接口!
package com.godfrey.controller;

import com.godfrey.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * description : HelloController
 *
 * @author godfrey
 * @since 2020-05-28
 */
@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping("/hello")
    public String hello() {
        return helloService.sayHello("Hello godfrey spring boot start");
    }
}
  1. 编写配置文件 application.yml
godfrey:
  hello:
    prefix: "Printprefix "
    suffix: " Printsuffix"
  1. 启动项目进行测试,结果成功 !

.

posted @ 2020-05-28 11:24  对弈  阅读(349)  评论(0编辑  收藏  举报