当前位置:网站首页>Write a starter
Write a starter
2022-07-26 19:07:00 【Yang Fan's blog】
Preface
To make up for it java Defects of the upward class loading mechanism : The upper layer provides interfaces , The specific implementation is provided by subclasses ; In order to load this method into java In the virtual machine ,java Provides a SPI Mechanism (service provider interface), It can be loaded into implementation classes that cannot be loaded by the class loading mechanism , There are many schemes for class loading using this mechanism , Such as jdbc load Driver,Dubbo Of SPI Mechanism ,Spring Automatic assembly, etc ; Next, we will package a Starter, Then it is referenced by another service , When the service starts, it will pass SpringBoot Of SpringBootApplication Medium EnableAutoConfiguration Under the @Import(AutoConfigurationImportSelector.class) Annotations automatically assemble a Starter.
Writing a Starter
First, we need to build a multi moudle engineering , Under this project, two module, One redission-spring-boot-starter The other is the quotation change starter Of spring-boot-demo, The specific project structure is shown in the figure below , The following describes how to establish these two projects ;

redission-spring-boot-starter
1、pom file
pom In file redisson rely on , The details are as follows
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.6.RELEASE</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.13.2</version>
</dependency>2、 stay resource Create one in the directory META-INF The catalog of , And create a spring-factories The file of , The content of the document is composed of two parts , It's actually equivalent to a key structure ,
key yes :org.springframework.boot.autoconfigure.EnableAutoConfiguration
value It is a specific class that is automatically configured :com.yangfan.redissionspringbootstarter.RedissonAutoConfiguration
key and value In between =\ Segmentation , The details are as follows
org.springframework.boot.autoconfigure.EnableAutoConfiguration =\ com.yangfan.redissionspringbootstarter.RedissonAutoConfiguration
3、RedissonProperties
RedissonProperties It's a read yml perhaps apollo Inside the configuration information class , The details are as follows :
@ConfigurationProperties(prefix = "yang.redisson")
public class RedissonProperties {
private String host = "localhost";
private int port = 6379;
private int timeout;// Timeout time
private boolean ssl;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public boolean isSsl() {
return ssl;
}
public void setSsl(boolean ssl) {
this.ssl = ssl;
}
}4、RedissonAutoConfiguration Is an automatic assembly redission Configuration class , The details are as follows
@Configuration
// Conditions for assembly , Used to control the bean Is it loaded
@ConditionalOnClass(Redisson.class)
//RedissonProperties Injection into Spring ioc In the container
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfiguration {
@Bean
public RedissonClient redissonClient(RedissonProperties redissonProperties){
Config config = new Config();
String prefix = "redis://";
if (redissonProperties.isSsl()){
prefix = "rediss://";
}
config.useSingleServer().setAddress(prefix+redissonProperties.getHost()+":"+redissonProperties.getPort()).setConnectTimeout(redissonProperties.getTimeout());
return Redisson.create(config);
}
}spring-boot-demo
1、 In the first pom Introduce in the file redission-spring-boot-starter,pom The specific contents of the document are as follows
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>redission-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>2、 stay application.properties Configuration in file redis Required domain name and port
yang.redisson.host=127.0.0.1 yang.redisson.port=6379
3、 Write a controller Verify whether the automatic assembly is successful
@RestController
@RequestMapping
public class RedisController {
@Autowired
private RedissonClient redissonClient;
@GetMapping("/test")
public String test(){
RBucket bucket = redissonClient.getBucket("name");
if(bucket.get() == null){
bucket.set("com.yang.redisson");
}else {
bucket.expire(1, TimeUnit.SECONDS);
}
return bucket.get()+"";
}
}Before enabling the service, you need to check redission-spring-boot-starter Conduct install once , In this way, it can be referenced by the second service , If install times Unable to find main class, So you need to redission-spring-boot-starter Of build Get rid of ;

adopt http Request to see the following information to prove that the automatic assembly is successful

SPI Principle analysis
First, let's look at this method :getAutoConfigurationEntry The way is to get META-INF/spring.factories Load the contents of java In the virtual machine ;
org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getAutoConfigurationEntry
Let's take a look getCandidateConfigurations This method , The core is this loading method :loadSpringFactories
边栏推荐
- The diagram of user login verification process is well written!
- 模板进阶(跑路人笔记)
- Use notes of Lichuang EDA
- SD NAND与eMMC优劣势对比
- NFT digital collection system development: fellow uncle first promoted the blessing series digital collection, which will be sold out immediately
- OpenSSF 基金会总经理 Brian Behlendorf :预计 2026 年将有 4.2 亿个开源
- LeetCode简单题之第一个出现两次的字母
- Leetcode simple question: the minimum total time required to fill a cup
- The first ABAP ALV reporter construction process
- NFT数字藏品系统开发:上线即售罄,网民“秒杀”数字藏品
猜你喜欢

工赋开发者社区 | 定了!就在7月30日!

【考研词汇训练营】Day 14 —— panini,predict,access,apologize,sense,transport,aggregation

VTK (the Visualization Toolkit) loads STL models

Brand new! Uncover the promotion route of Ali P5 Engineer ~p8 architect

LeetCode简单题之装满杯子需要的最短总时长

FTP协议

SSM整合-功能模块和接口测试

基础模块及算例#pytorch学习

NFT digital collection system development: sold out when online, and netizens "spike" Digital Collections

Likeshop takeout order system is open source, 100% open source, no encryption
随机推荐
Meta Cambria handle exposure, active tracking + multi tactile feedback scheme
MySQL日志介绍
Complete MySQL database commands
js map使用
Redis learning notes-2. Use of the client
Write a thesis and read this one
模型定义#pytorch学习
2022年流动式起重机司机考试试题模拟考试平台操作
2022g1 industrial boiler stoker certificate question bank and simulation examination
场景之分页查询设计
ZbxTable 2.0 重磅发布!6大主要优化功能!
Accused of excessive patent licensing fees! The U.S. Court ruled that Qualcomm violated the antitrust law: Qualcomm's share price fell 10.86%!
Learn UML system modeling from me
Redis学习笔记-2.客户端的使用
销量下滑,品牌边缘化,失去“安全牌”的沃尔沃,还能走多远?
Operations research 69 | explanation of classic examples of dynamic planning
Seata 入门简介
分布式事务-seata
2022年云商店联合营销市场发展基金(MDF)介绍
Verification palindrome string II of leetcode simple question