springboot自定义和使用starter(附源码下载)

    前些日子写了些自己学习若依框架的文章,慢慢的就懒下来了。看到还获得了N多关注和点赞(N是个很小的整数),所以决定继续坚持写一些学习的文章。这里参考了网上的一些文章,如有雷同,纯属我copy,还望见谅。下面文章正式开始。
    springboot默认定义了很多的starter(spring-boot-starter-xxx),这些starter的简化了很多烦琐的配置,这对于开发人员来说是一个福音,通过引入各种 Spring Boot Starter 包可以快速搭建出一个项目的脚手架。不过也是不可能应付所有需求的,根据我们自己的业务,我们可以自定义 starter 来实现项目中复用度高的业务,让别的模块能很方便的引入使用。

1、创建自定已starter

    自定义starter的命名规则为xxx-spring-boot-starter。这里我们自定义一个,命名为lzx-spring-boot-starter。
    首先使用IDEA创建一个maven工程,命名为lzx-spring-boot-starter。
在这里插入图片描述
    引入相关依赖 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lzx</groupId>
    <artifactId>lzx-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.4.4</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

    写一个配置类,获取配置文件中以 custom 开头的配置,绑定到该类的对应属性中。即该类属性可通过配置文件更改。

@ConfigurationProperties(prefix = "custom")
public class AuthorProperties {
    public static final String DEFAULT_AUTHOR = "lzx";
    public String author = DEFAULT_AUTHOR;
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}

    service 类,写自定义 starter 的实现功能,里面写的方法供使用时调用获取到配置文件的属性值,作为业务逻辑的参数实现相关功能。

public class AuthorServer {
    public String author;
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}

    自动配置类,使配置生效,并且把服务注入到容器中

@Configuration
@ConditionalOnClass({ AuthorServer.class })
@EnableConfigurationProperties(AuthorProperties.class)
public class AuthorAutoConfiguration {
    @Resource
    private AuthorProperties authorProperties;
    @Bean
    @ConditionalOnMissingBean(AuthorServer.class)
    @ConditionalOnProperty(name = "custom.author.enabled", matchIfMissing = true)
    public AuthorServer authorResolver() {
        AuthorServer authorServer = new AuthorServer();
        authorServer.setAuthor(authorProperties.getAuthor());
        return authorServer;
    }
}

    创建 spring.factories ,把我们自定义的 starter 自动配置加入的 spring 工厂在 src/java/resources 目录下新建 META-INF 文件夹,创建 spring.factories 文件,内容如下:把自己的自动配置类路径加入。

# CUSTOM
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lzx.AuthorAutoConfiguration

以上,自定义springboot的功能就已经实现啦。
然后使用idea带的maven install功能将会把生成的jar包lzx-spring-boot-starter-1.0-SNAPSHOT.jar放入maven配置的仓库。

2、使用自定已starter

    创建spring initializr项目,命名为teststarter,Dependencies选中Spring web。创建完成后,在pom.xml中添加lzx-spring-boot-starter-1.0-SNAPSHOT的依赖。

<dependency>
	<groupId>com.lzx</groupId>
	<artifactId>lzx-spring-boot-starter</artifactId>
	<version>1.0-SNAPSHOT</version>
</dependency>

    然后直接在启动类中自动注入AuthorServer类,当收到http的get请求"/author"时,获取配置信息的结果。

@SpringBootApplication
@RestController
public class TeststarterApplication {

	public static void main(String[] args) {
		SpringApplication.run(TeststarterApplication.class, args);
	}

	@Autowired
	private AuthorServer authorServer;

	@RequestMapping("/author")
	String home() {
		return "发布者:"+ authorServer.getAuthor();
	}
}

    配置文件application.properties。

custom.author=hello

    启动项目,http请求结果如下
在这里插入图片描述
    至此,整个过程就完成啦。
    git源码地址:https://github.com/liuzixinlzx/mystarter.git

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值