自定义Springboot-starter

如何自定义一个Springboot-starter

1.首先在idea中创建一个Empty_project

2.然后在里面添加2个模块

1)starter模块(普通maven)

2)autoConfiguration模块(spring initializer)

3.在starter的pom文件中引入autoconfiguration模块的dependency


4.在autoConfiguration模块中添加模块的服务类

//这个服务的方法里面的helloProperties属性是可以通过配置文件进行配置的
package com.shufang.starter.shufangspringbootstarterautoconfigurer;

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();
    }
}

//创建一个属性类,输出化get set方法、然后@ConfigurationProperties(....)
package com.shufang.starter.shufangspringbootstarterautoconfigurer;

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

//与配置文件中的配置属性进行绑定
@ConfigurationProperties(prefix = "shufang.hello")
public class HelloProperties {

    //添加可配置的前缀属性
    private String prefix;
    //添加可配置的后缀属性
    private String suffix;

    public HelloProperties() {
    }

    public HelloProperties(String prefix, String suffix) {
        this.prefix = prefix;
        this.suffix = 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;
    }
}

//创建自动配置类,将服务的bean通过@Bean添加到容器中,让其自动加载
//同时在HelloService中注入属性类
package com.shufang.starter.shufangspringbootstarterautoconfigurer;


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
@EnableConfigurationProperties(HelloProperties.class) //允许从配置文件中获取属性,而且这个类可以直接在本类中使用
@ConditionalOnWebApplication //必须是Web应用,这个组件的引入才生效
public class HelloServiceAutoConfiguration {

    //注入这个类
    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService() {

        //首先创建一个实例
        HelloService helloService = new HelloService();

        //添加这个实例到服务类中
        helloService.setHelloProperties(helloProperties);

        //返回出去,添加到容器中
        return helloService;
    }

}


#最后,得在AutoConfiguration中添加com.shufang.starter.shufangspringbootstarterautoconfigurer.HelloServiceAutoConfiguration
 
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.shufang.starter.shufangspringbootstarterautoconfigurer.HelloServiceAutoConfiguration

5.通过maven的生命周期管理,install2个不同的模块到maven仓库中

结束语:届时完成starter的创建,然后在其他项目中可以引用了

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值