springboot自定义starter

自定义springboot starter

一 、简介

1、SpringBoot starter机制

SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。starter让我们摆脱了各种依赖库的处理,需要配置各种信息的困扰。SpringBoot会自动通过classpath路径下的类发现需要的Bean,并注册进IOC容器。SpringBoot提供了针对日常企业应用研发各种场景的spring-boot-starter依赖模块。所有这些依赖模块都遵循着约定成俗的默认配置,并允许我们调整这些配置,即遵循“约定大于配置”的理念。

2、为什么要自定义starter

在我们的日常开发工作中,经常会有一些独立于业务之外的配置模块,我们经常将其放到一个特定的包下,然后如果另一个工程需要复用这块功能的时候,需要将代码硬拷贝到另一个工程,重新集成一遍,麻烦至极。如果我们将这些可独立于业务代码之外的功配置模块封装成一个个starter,复用的时候只需要将其在pom中引用依赖即可,SpringBoot为我们完成自动装配,简直不要太爽。

3、自定义starter的命名规则

SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。

二、案例

1.依赖
        <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>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
2.代码结构

在这里插入图片描述

5. 代码

MyProperties

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

/**
 * @Auther: wxy
 * @Date: 2020/12/5 11:41
 * @Description:
 */
@Data
@ConfigurationProperties("my")
public class MyProperties {

    private String name;

    private Integer age;
}

MyAutoConfiguration

import com.example.demospringbootstarter.properties.MyProperties;
import com.example.demospringbootstarter.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * @Auther: wxy
 * @Date: 2020/12/5 11:44
 * @Description:
 */
@EnableConfigurationProperties(MyProperties.class)
public class MyAutoConfiguration {

    @Autowired
    private  MyProperties myProperties;



    @Bean
    public MyService myService(){
        return new MyService(myProperties.getName(),myProperties.getAge());
    }
}

MyService

import lombok.AllArgsConstructor;

/**
 * @Auther: wxy
 * @Date: 2020/12/5 11:49
 * @Description:
 */
@AllArgsConstructor
public class MyService {


    public String name;

    public Integer age;


    public void showInfo(){
        System.out.println(String.format("姓名:%s,年龄:%d",name,age));
    }
}

最最重要的地方:spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.example.demospringbootstarter.config.MyAutoConfiguration

这样springboot在启动的时候,会将MyAutoConfiguration交给容器管理

测试

其他项目,pom文件依赖这个starter,

在这里插入图片描述

测试类

import com.example.demospringbootstarter.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: wxy
 * @Date: 2020/12/5 11:07
 * @Description:
 */
@RestController
@RequestMapping("/my")
public class MyStarterController {



    @Autowired
    private MyService myService;


    @RequestMapping("/stater")
    public void test(){
        myService.showInfo();
    }
}

yml配置:

my:
  name: aaa
  age: 111

输出结果:

姓名:aaa,年龄:111
备注:

@EnableConfigurationProperties的作用是使@ConfigurationProperties注解生效交给容器管理

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值