自定义spring boot starter

一.spring boot实现自定义配置的三种方式

  1. 最简单的方法就是通过@Configuration配置相关类,并且用@ComponentScan注解设置包含配置类的路径,spring boot启动时会将配置类装载进容器统一管理,从而实现自定义配置的加载
  2. 通过@Import注解直接导入相关配置类,可导入的类型有:使用了@Configuration注解的类,ImportSelector的子类,ImportBeanDefinitionRegistrar的子类。
  3. 用提供给第三方调用的starter形式,实现自定义配置的加载,让所有引入此module的应用都能实现配置加载,并且使用方可自定义相关配置参数,例如spring-boot-starter-web

二.自定义的spring boot starter的意义

  1. 通过把通用配置和底层处理代码包装成starter,让所有的业务模块通过引用该starter模块,可以实现业务代码解耦,集中控制与业务无关的通用处理
  2. starter具有可插拔的特性,即插即用,对业务代码零入侵,使用方可自定义相关配置来获取不同特性

三.怎样实现spring boot starter

  1. 准备一个Propertites文件,用于读取使用方配置的参数,用到的注解有@ConfigurationProperties(prefix=“xxx”)等
  2. 准备一个Configuration文件,用于通过Propertites文件获取相应参数,在Configuration中可以新建一些bean,新建的bean就是我们的业务处理类,交给spring容器管理
  3. 在resources文件夹下新建META-INF文件夹,在META-INF下新建spring.factories文件,在文件中配置org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.study.CustomerConfiguration,也即是设置需要自动配置的目标类,可以设置多个目标类

Propertites文件

package com.study;

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

@ConfigurationProperties(prefix="person")
@Data
public class CustomerPropertites {
    private String id;
    private String name;
    private String gender;
    private String address;
}

Configuration文件

@Configuration
@EnableConfigurationProperties(CustomerPropertites.class)
@ConditionalOnProperty(prefix="person", name="enabled", havingValue = "true")
@Data
public class CustomerConfiguration {

    @Autowired
    private CustomerPropertites customerPropertites;

    @Bean
    @ConditionalOnClass(CustomerService.class)
    @ConditionalOnMissingBean(CustomerService.class)
    public CustomerService customerService(){
        return CustomerService.builder()
                .id(customerPropertites.getId())
                .name(customerPropertites.getName())
                .gender(customerPropertites.getGender())
                .address(customerPropertites.getAddress())
                .build();
    }
}

Configuration文件中新建的bean,业务处理类

package com.study;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class CustomerService {

    private String id;
    private String name;
    private String gender;
    private String address;

    public String getInfo(){
        return id + "-" + name + "-" + gender + "-" + address;
    }

}

四.其他project引用starter

<dependency>
            <groupId>com.study</groupId>
            <artifactId>personal-code-learning-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
</dependency>
package com.personal.code.springboot;

import com.study.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping("/getInfo")
    @ResponseBody
    public String getInfo(){
        return customerService.getInfo();
    }

}

自定义spring boot starter demo:https://github.com/BoyQiang/personal-code-learning

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值