springboot的Starter?
starter是啥?
starter有啥用?
怎么自定义starter?
针对于这些,我也不知道!哈哈哈!但是经过一番折腾后,有个大概了解,有不对之处,多多指教!
starter是啥?
其实啥也不是,就是一堆jar包的集合,你引用我,我引用你,然后就有一大堆jar出现了,也就是个maven工程,之后还是要放到公司私服给其他同事使用(我的想法是写starter给其他公司用,哈哈哈),大概也就是一个pom的maven工程,然后可以让别人引入!!
starter有啥用?
这玩意还是有点用的:
- starter可以将配置文件相关的东西全部弄好,在引入到springboot的时候通过spi机制,就可以扫描到能正常使用的bean了。所以说,Spring Boot 是简化配置。
- starter可以配置好与spring整合相关的配置和相关依赖(jar和jar版本),不用去考虑版本的问题,使用者引入就可以使用。
怎么自定义starter?
写代码才好玩,才有意思,开始进入主题!!
首先需要创建两个工程:
- 一个是starter上传
- 一个是starter的使用
自定义starter工程
no bb!看图:
- 一个是starter
- 一个是autoconfigure
为啥需要这样创建呢,我也不知道,啊哈哈,学人家springboot官方的,其实也能理解,starter就是个空的项目,只需要引入autoconfigure即可,这样类似于多态的形式,易于扩展
==》看看starter里面有啥:
创建项目过程就不演示了,首先是创建一个空的工程,然后分别创建两个模块,这里空工程最好不要创建成maven工程,不需要统一版本管理;
上图那样就可以创建出空工程了!!
继续回到starter模块,其实就一个地方有改动,pom文件:
<?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.simple</groupId>
<artifactId>simple-spring-boot-starter</artifactId>
<version>1.0.0.RELEASE</version>
<dependencies>
<dependency>
<groupId>com.simple</groupId>
<artifactId>simple-spring-boot-starter-autoconfigure</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
</dependencies>
</project>
我们其实可以看到,就是引入了autoconfigure模块,自己本身没有什么逻辑,所以没了。简单不,那就看看下一个吧!!
自定义autoconfigure工程
看上图,其实也很简单,就是对外提供一个HelloService服务,我把三个文件全部粘贴出来(这些项目在我gitee上面全部都有,还有很多其他的项目)
- 首先看看配置类:HelloProperties.java
package com.simple.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
* 项目: starter-test
* <p>
* 功能描述:
*
* @author: WuChengXing
* @create: 2021-05-16 21:56
**/
@ConfigurationProperties(prefix = "simple.hello")
@Component
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;
}
}
文件很简单,就是有个前缀和后缀,这个参数可以在yaml文件或者properties文件中进行配置,之后使用这个starter的时候会说
- 再看看HelloService.java
package com.simple.starter;
/**
* 项目: starter-test
* <p>
* 功能描述:
*
* @author: WuChengXing
* @create: 2021-05-16 21:56
**/
public class HelloService {
HelloProperties helloProperties;
public HelloService(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(String name) {
return helloProperties.getPrefix() + ": " + name + ", " + helloProperties.getSuffix();
}
}
这里就是一个很简单的逻辑,往传进来的字符串前后拼接前面说的前后缀;
- 再看看比较核心的:HelloServiceAutoConfiguration.java
package com.simple.starter;
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;
/**
* 项目: starter-test
* <p>
* 功能描述:
*
* @author: WuChengXing
* @create: 2021-05-16 21:57
**/
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService() {
return new HelloService(helloProperties);
}
}
注意: 这里有个注解 @ConditionalOnWebApplication,这个含义就是需要在web环境下才会生效!!!
这个文件就是能给其他工程提供服务的关键之一,这里就相当于把HelloService装配好,然后供使用者使用,但是有个问题,那这个配置文件怎么起作用??这个配置文件Springboot怎么知道他在哪??要知道他在哪才知道有HelloService??
所以还有个关键文件:spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.simple.starter.HelloServiceAutoConfiguration
这个文件需要放在resources -> META-INF目录下才能生效!!
为什么放在这里才能生效了??我偷偷看了人家官方写的,都是放在那的,哈哈哈!!
其实不难,这个跟springboot自动加载的过程有关,也是springboot的启动过程有关,这里就不详细展开讲了,可以在网上找一篇博文看看(等我有时间,也来奉献一篇)
然后在看看pom文件:
<?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.simple</groupId>
<artifactId>simple-spring-boot-starter-autoconfigure</artifactId>
<version>1.0.0.RELEASE</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
</dependencies>
</project>
一个引入boot相关的,另外一个是结局HelloProperties文件报红的依赖,可以去掉试试!
到此两个工程就算是做完了,也就是我们自己的starter算是写完了;然后将两个项目进行安装,到自己本地仓库(也可以推送到自己的私服,或者maven的共有仓库):
这里注意,每次install时候,最好先安装被依赖的那个工程先,即autoxxx;
至此我们的starter就算安装完了!!!GO!!开始测试使用!
怎么使用自定义的starter?
首先创建一个工程,看看其pom文件:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.simple.starter</groupId>
<artifactId>starter-test</artifactId>
<version>1.0.0.RELEASE</version>
<name>starter-test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 引入自定义的starter -->
<dependency>
<groupId>com.simple</groupId>
<artifactId>simple-spring-boot-starter</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
引入我们自定义的starter,如果没有提示的话,就重新安装一下starter项目,或者直接从starter项目那里直接粘贴过来:
然后在来写个代码测试一下:
package com.simple.starter.controller;
import com.simple.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 项目: starter-use-test
* <p>
* 功能描述:
*
* @author: WuChengXing
* @create: 2021-05-16 22:50
**/
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello() {
return helloService.sayHello("wcx");
}
}
然后我在 application.properties文件中也进行了设置:
simple.hello.prefix=Prefix-Hello
simple.hello.suffix=Suffix-World
万事具备,只差测试,哈哈哈;启动项目!!
然后在浏览器中输入:http://127.0.0.1:8080/hello
看看效果:
哈哈哈,可以看到我们项目已经引入了,可以正常使用了!!
总结
这只是定义了一个很简单的starter,实际开发中要结合业务需求之类的再去自定义自己的starter,常见于 redis的starter 各种支付、登录的sdk等!!分享就到这里了!
感谢大家阅读、互相学习;
有问题评论或者发邮箱;
gitee:很多代码仓库;
1449697757@qq.com