0
点赞
收藏
分享

微信扫一扫

Spring Boot Starter工程如何快速定位自动化配置类


前言


现在Spring Boot越来越流行,而各种Spring Boot Starter工程由于其提供的自动化配置的特性,极大的提高了我们的开发速度。但同时也给定位问题,了解实现原理带来了一定的困难。 而要了解Spring Boot Starter工程的底层原理,最核心的就是找到工程中的自动化配置类。 本文根据自己的开发经验,总结以下快速定位自动化配置类的方法。

由于最近在进行gateway的改造,就以 spring-cloud-starter-gateway来说明.

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

一、根据自动化配置的类名前缀搜索

一般自动化配置类会根据Maven依赖中的artifactId里面的名称来命令。

比如:spring-cloud-starter-gateway,表示这个依赖项目是gateway网关依赖包,一般对应的自动化配置类会以Gateway开头。

在idea中,直接双击Shift搜索Gateway开头的类就能很轻易的发现GatewayAutoConfiguration类。

Spring Boot Starter工程如何快速定位自动化配置类_starter

二、根据启用注解查找

自动化配置实现原理里面提到过,一般有2种实现方式。
第一种是在META-INF目录下的spring.factories配置org.springframework.boot.autoconfigure.EnableAutoConfiguration属性指定自动化配置文件。
第二种是通过@Enable***启动注解来注入自动化配置文件。
比如@EnableDiscoveryClient,@EnableOpenApi,@EnableSwagger2

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

要启动springfox-boot-starter的自动化配置特性,需要在启动类上添加@EnableOpenApi注解。

@SpringBootApplication(exclude = {
GatewayDiscoveryClientAutoConfiguration.class
})
@EnableDiscoveryClient
@Slf4j
@EnableOpenApi
public class WxswjGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(WxswjGatewayApplication.class, args);
log.info("网关服务启动成功!");
}
}

在idea中,点击@EnableOpenApi注解,观察@Import注解导入的自动化配置类OpenApiDocumentationConfiguration.class

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({OpenApiDocumentationConfiguration.class})
public @interface EnableOpenApi {
}

三、根据对应的配置属性定位

一般导入Spring Boot Starter工程的依赖后,都会调整一些对应的配置属性。我们可以根据配置属性来定位相关的自动化配置类。

下面是gateway网关集成注册中心的相关配置

#配置网关的默认路由
spring.cloud.gateway.enabled=true
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

点击属性spring.cloud.gateway.discovery.locator.enabled,会跳转到DiscoveryLocatorProperties 属性配置文件中。

@ConfigurationProperties("spring.cloud.gateway.discovery.locator")
public class DiscoveryLocatorProperties {
private boolean enabled = false;
private String routeIdPrefix;
private String includeExpression = "true";
private String urlExpression = "'lb://'+serviceId";
private boolean lowerCaseServiceId = false;
private List<PredicateDefinition> predicates = new ArrayList();
private List<FilterDefinition> filters = new ArrayList();

public DiscoveryLocatorProperties() {
}

public boolean isEnabled() {
return this.enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
……

然后点击IDEA中的文件定位按钮,快速定位到相关配置属性类和自动化配置文件GatewayDiscoveryClientAutoConfiguration

Spring Boot Starter工程如何快速定位自动化配置类_starter_02


Spring Boot Starter工程如何快速定位自动化配置类_spring boot_03

四、根据依赖包查找

根据依赖包的名称spring-cloud-starter-gateway,在工程的maven依赖下,找到对应的工程,展开查看。

可以通过在META-INF目录下的spring.factories配置org.springframework.boot.autoconfigure.EnableAutoConfiguration属性查看相关的自动化配置文件。

也可以直接查看config目录下的自动化配置文件。

Spring Boot Starter工程如何快速定位自动化配置类_自动化配置_04

五、四种方法对比

第一种方法直接通过自动化配置名称搜索是最方便的方式,当然前提是引入的Spring Boot Starter工程的自动化配置类的命名符合规范。

第二种方式通过类似@EnableSwagger2的启动注解来定位自动化配置类,只适用采用@EnableXXX注入自动化配置类实现自动化配置的项目。

第三种方式利用相关的配置属性来定位实现自动化配置类,这种方式最精准,能根据相关功能的配置直接定位到对应功能的自动化配置类。
比如在研究gateway网关怎么和注册中心整合的时候,如果通过上面的2种方式,很难定位到对应功能的自动化配置类GatewayDiscoveryClientAutoConfiguration。而根据配置属性spring.cloud.gateway.discovery.locator.enabled,可以很轻松的定位到自动化配置类。
所以相对来说,前2种方式更适合定位总的自动化配置入口,方式三适合精准定位。

第四种方式是最原始的一种傻瓜式查找操作,如果项目的maven依赖非常多,根据依赖包的名称找对应的依赖工程就容易眼花缭乱,不推荐。

总结

本文主要是总结了4种快速定位Spring Boot Starter工程的自动化配置类的方法。
在学习spring boot的过程中,大家首先要熟悉Spring Boot的自动化配置原理,这样就很容易理解Spring Boot Starter工程的实现原理。
然后在实际开发中,通过快速定位Spring Boot Starter工程对应的自动化配置类XXXAutoConfiguration查看相关源码,可以帮助我们理解其内部的实现原理,方便更深入的定制开发和问题定位。

更多精彩,关注我吧。

Spring Boot Starter工程如何快速定位自动化配置类_自动化配置_05


举报

相关推荐

0 条评论