springboot之自定义starter

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

官方命名:

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

自定义命名

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

2.编写启动器

  1. 在idea新建一个空项目:
  2. 在空项目中新建一个普通的maven模块:chen-spring-boot-starter2s
  3. 新建一个Springboot模块(用于自动配置):chen-spring-boot-starter-autoconfigurer
    在这里插入图片描述

点击apply即可

  1. 在启动器starter中导入autoconfigurer的依赖
    在这里插入图片描述
  2. 在autoconfigurer项目中的依赖都删除,只留下一个starter。
    在这里插入图片描述
    6.在autoconfigurer项目中编写自己的服务
package com.chen.chenspringbootstartreautoconfigure.service;

/**
 * Created on 2020/8/14.
 *
 * @author Chen_Peng
 */
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. 编写HelloProperties配置类
package com.chen.chenspringbootstartreautoconfigure.service;

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

/**
 * Created on 2020/8/14.
 *
 * @author Chen_Peng
 */
@ConfigurationProperties(prefix = "chen.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. 编写自动配置类注入bean
package com.chen.chenspringbootstartreautoconfigure.service;

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 //web应用生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    @Autowired
    HelloProperties helloProperties;

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

    }
}
  1. 在resources编写一个自己的 META-INF\spring.factories将配置类的路径写入
    在这里插入图片描述
  2. 完成之后,使用maven工具安装到maven仓库

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

  1. 新建一个SpringBoot 项目
  2. 导入我们自己写的启动器
		<dependency>
            <groupId>org.example</groupId>
            <artifactId>chen-spring-boot-starter2</artifactId>
        </dependency>
  1. 编写一个HelloController接口,
package com.chen.springboot.controller;


import com.chen.chenspringbootstartreautoconfigure.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("/hello")
    public String hello(){
        String hello = helloService.sayHello("张三");
        return hello;
    }
}
  1. 编写配置文件 application.properties
  2. 启动项目测试
    在这里插入图片描述

成功!

Spring Boot是一个快速开发微服务的框架,它提供了大量的自动化配置和快速开发的工具。在Spring Boot中,我们可以使用Starter来快速集成一些常用的功能,如数据库、缓存、web等。而自定义Starter则可以让我们将自己的功能快速集成到Spring Boot中,下面是自定义Starter的流程: 1. 创建Maven项目 首先,我们需要创建一个Maven项目作为我们自定义Starter的项目。在项目的pom.xml中添加Spring Boot的依赖,以及其他需要集成的依赖。 2. 编写自动配置类 自动配置类是自定义Starter的核心,它负责将我们自定义的功能集成到Spring Boot中。在自动配置类中,我们可以使用@Conditional注解来判断是否需要进行配置。 3. 编写Starter类 Starter类是我们自定义Starter的入口,它负责将自动配置类注入到Spring容器中。在Starter类中,我们需要使用@EnableAutoConfiguration注解来启用自动配置。 4. 打包和发布Starter 当我们完成了自动配置类和Starter类的编写后,我们需要将自定义Starter打包成jar包,并发布到Maven仓库中,以便其他项目可以通过Maven依赖的方式使用我们的Starter。 5. 在项目中使用自定义Starter 在其他项目中使用自定义Starter非常简单,只需要在项目中的pom.xml中添加我们自定义Starter的依赖即可。Spring Boot会自动将我们的自定义Starter集成到项目中,并进行自动配置。 以上就是自定义Starter的流程,通过自定义Starter,我们可以将自己的功能快速集成到Spring Boot中,提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值