当前位置:网站首页>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
边栏推荐
- Comparison of advantages and disadvantages between SD NAND and EMMC
- MySQL learning notes -2. how to improve the query performance of SQL statements
- 多商户商城系统功能拆解16讲-平台端会员成长值记录
- JS uses readLine to realize terminal input data
- This section is used to supplement 3
- 立创EDA使用笔记
- Redis学习笔记-2.客户端的使用
- JS question brushing plan - linked list
- Brand new! Uncover the promotion route of Ali P5 Engineer ~p8 architect
- ZbxTable 2.0 重磅发布!6大主要优化功能!
猜你喜欢

MySQL学习笔记-2.如何提高sql语句的查询性能

SSM integration - functional module and interface testing

likeshop外卖点餐系统开源啦100%开源无加密

手写一个Starter

MySQL - 多表查询与案例详解

Redis学习笔记-2.客户端的使用

SSM integration - exception handler and project exception handling scheme

LeetCode简单题之验证回文字符串 Ⅱ

JS uses readLine to realize terminal input data

网络协议:TCP/IP协议
随机推荐
SSM integration configuration
This section is used to supplement 3
flex布局
rancher部署kubernetes集群
2022 Shanghai safety officer C certificate operation certificate examination question bank simulated examination platform operation
[yuntu said] issue 246 digital asset chain - your God of digital asset property protection!
手写一个Starter
Basic module and example pytorch learning
项目中@RequestMapping的作用以及如何使用
2022g1 industrial boiler stoker certificate question bank and simulation examination
MySQL日志介绍
ALV screen input option learning
Use notes of Lichuang EDA
Racher deploys kubernetes cluster
MySQL - 函数及约束命令
2022T电梯修理考试题及在线模拟考试
SSM integration - functional module and interface testing
SMMU carding
基础模块及算例#pytorch学习
MySQL - function and constraint commands