平时 SSM 框架开发中,会遇到很多的配置文件,项目中每次集成一个新的框架,都要新增很多框架配置文件。根据“约定高于配置”原则,官方推出“Springboot”对各种框架进行整合,做为开发项目的“脚手架”。
原理Springboot 对各种框架进行整合,最重要的问题就是装配问题,怎样简单快速的集成一款框架?
集成一款框架最重要的是编写框架的配置文件,Springboot的对配置文件的处理主要有两种方式,分别为自动装配和手动装配;
自动装配 :编写好框架的配置文件之后, 在 resources/meta-INF 目录下编写 spring.factories 文件, 添加属性 org.springframework.boot.autoconfigure.EnableAutoConfiguration=配置文件路径。
eg : springboot-boot-autoconfigure 下的自动装配
引用 springboot-boot-{}-starter 之后可以直接使用的框架,采用的是自动装配的方式,
手动装配 :编写好框架的配置文件之后,需要在启动类上使用注解才能对其加载。这样的方式有很多,向我们经常使用的 @EnableTransaction,@EnableCaching,@EnableAsync,@EnableWebMvc 等等,都是用的手动装配方式加载。
eg: @EnableWebMvc
主要是使用 @import(配置文件.class)
下面就来做一个自制 swagger2 自动装配的 jar 吧
自制 swagger2-boot-starter首先,创建一个 Springboot 项目,并加载 swagger2 jar 包
pom 文件如下(部分):
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.boot
spring-boot-autoconfigure
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
自制 swagger2-boot-starter 最重要的两个步骤
- 读取 application 配置文件中的 swagger 配置
- 自动装配集成 swagger2 的配置文件,并在 spring.factories 中配置
创建 Swagger2AutoConfiguration.java 和 Swagger2Properties.java 分别做这两件事。
读取配置文件Swagger2Properties 代码如下:
@Data
@ConfigurationProperties("swagger2")
public class Swagger2Properties {
private Map groups = new linkedHashMap<>();
@Data
public static class GroupInfo {
private String basePackage;
private String title;
private String description;
}
}
@ConfigurationProperties(“swagger2”): 读取 appliciation 文件中的 swagger2 配置
类的属性为配置的属性
Swagger2AutoConfiguration.java
@Configuration
@EnableSwagger2
public class Swagger2AutoConfiguration implements BeanFactoryAware {
private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
@Bean
public Swagger2Properties properties() {
return new Swagger2Properties();
}
@Bean("createRestOpenApi")
public List createRestOpenApi(Swagger2Properties properties) {
ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) beanFactory;
List docketList = new ArrayList<>();
// swagger 配置
for (String groupName : properties.getGroups().keySet()) {
Swagger2Properties.GroupInfo groupInfo = properties.getGroups().get(groupName);
String basePackage = groupInfo.getbasePackage();
Docket docket = new Docket(documentationType.SWAGGER_2)
.groupName(groupName)
.apiInfo(openApiInfo(groupInfo))
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.any())
.build();
docketList.add(docket);
// docket 加入 IOC 容器
configurableBeanFactory.registerSingleton(groupName, docket);
}
return docketList;
}
private ApiInfo openApiInfo(Swagger2Properties.GroupInfo groupInfo) {
return new ApiInfoBuilder()
.title(groupInfo.getTitle())
.description(groupInfo.getDescription())
.version("1.0")
.build();
}
}
主要将 swagger2 的配置加载到 swagger 的 Docket 中,实现 BeanFactoryAware 接口,创建 Docket 的 Bean。
创建自动装配在 resources/meta-INF 文件下创建 spring.factories 文件,写入
org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.moming.swagger.autocnfigure.Swagger2AutoConfiguration
指定自动加载的配置文件
Ok, 完成了
源码swagger2-boot-starter



