SpringBoot04-自定义starter

自定义SpringBoot-starter

套路(模板来源mybatis-spring-boot-starter)

​ 1、创建一个xxx-spring-boot-autoconfigure项目:声明配置类xxxAutoConfiguration并且加入到Springboot的EnableAutoConfiguration的扫描范围中

​ 2、创建一个xxx-spring-boot-starter项目:啥事也不干,只在pom中引入xxx-spring-boot-autoconfigure的坐标

​ 3、在第三方项目中,直接引入starter,即可自动配置,即xxxAutoConfiguration起作用

自定义redis-spring-boot-starter

redis-spring-boot-autoconfigure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.10.2</version>
<!-- 依赖不想上传递 可以利用此配置 来根据引入的组件选择性注入(如springboot内置tomcat) -->
<optional>true</optional>
<!-- 只在编译阶段起作用 -->
<scope>compile</scope>
</dependency>
<!-- 是自定义配置在 application.properties 编辑时有提示 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

</dependencies>

定义自动配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@ConfigurationProperties(prefix = "redis")
public class RedisProperties {

private String localHost = "localhost";

private Integer port = 6379;

public String getLocalHost() {
return localHost;
}

public Integer getPort() {
return port;
}

public void setLocalHost(String localHost) {
this.localHost = localHost;
}

public void setPort(Integer port) {
this.port = port;
}
}

@Configuration
@ConditionalOnClass(Jedis.class)
@EnableConfigurationProperties(value = RedisProperties.class)
public class RedisAutoConfiguration {

@Bean
@ConditionalOnMissingBean(Jedis.class)
public Jedis jedis(RedisProperties redisProperties){
return new Jedis(redisProperties.getLocalHost(),redisProperties.getPort());
}
}

redis-spring-boot-starter

只做坐标和依赖引入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.study</groupId>
<artifactId>redis-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.10.2</version>
</dependency>
</dependencies>

springboot-03-use-my-starter

引入redis-spring-boot-starter 即自动配置注入了jedis组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.study</groupId>
<artifactId>redis-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
public class Springboot03UseMyStarterApplication {

public static void main(String[] args) {
ConfigurableApplicationContext ioc = SpringApplication.run(Springboot03UseMyStarterApplication.class, args);
Jedis redis = ioc.getBean(Jedis.class);
redis.set("abc","123");
System.out.println(redis.get("abc"));

}
}