1. starter简介
:::tips
SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为 我提供的这些场景启动器,我们再进行少量的配置就能使用相应的功能。即使是这样,springboot也不能囊括我们所有 的使用场景,往往我们需要自定义starter,来简化我们对springboot的使用。
:::
2. 如何自定义
2.1 Spring-boot自动装配规范
:::tips
springboot的出现就是为了简化spring开发,提供开箱即用的手脚架功能.基于springboot自动装配原理手撸一个starterstarter
命名规范:
- 官方命名格式:spring-boot-starter-模块名
自定义命名规范:模块名-spring-boot-starter :::
步骤
创建项目:mck-spring-boot-starter
- 引入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
引入打包工具
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</build>
编写MckProperties
package com.itmck.mckspringbootstarter.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 太阳当空照,花儿对我笑
* <p>
* Create by M ChangKe 2021/8/7 21:51
**/
@Data
@ConfigurationProperties(prefix = "mck")
public class MckProperties {
private String nm;
}
- DesAopConfig
package com.itmck.mckspringbootstarter.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(MckProperties.class)
@ConditionalOnProperty(
prefix = "mck",
name = "nm",
havingValue = "true"
)
public class DesAopConfig {
@Autowired
MckProperties mckProperties;
public String handler() {
return mckProperties.getNm();
}
}
- 创建resources\META-INF\spring.factories
:::info org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.itmck.mckspringbootstarter.config.DesAopConfig :::
- 打包
:::info mvn clean install :::
使用starter步骤:
引入依赖
<dependency>
<groupId>com.itmck</groupId>
<artifactId>mck-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
application.yml
mck:
nm: true
java中引入如下:
@Slf4j
@Component
public class Publisher {
@Resource
DesAopConfig desAopConfig;
public void send() {
log.info("desAopConfig nm:{}",desAopConfig.handler());
}
}
运行结果如下
3.源码简述
:::tips 通过上面springboot自动装配的源码发现
- AutoConfigurationImportSelector implements DeferredImportSelector
- DeferredImportSelector extends ImportSelector :::
因为springboot通过一系列操作最终还是找到factories进行读取配置.但是从上面源码我们还可以直接通过使用 @Import 设置对应的类也是一样的所以还可以直接:
@Import({DesAopConfig.class}) 或者@Import({MyImportSelector.class})
注意,通过自定义类实现ImportSelector 重写 selectImports 方法可以注入多个
- MyImportSelector ```java package com.itmck.mckspringbootstarter.init;
import com.itmck.mckspringbootstarter.config.DesAopConfig; import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata;
/**
- 太阳当空照,花儿对我笑
Create by M ChangKe 2021/8/7 19:26 **/ public class MyImportSelector implements ImportSelector {
@Override public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[]{DesAopConfig.class.getName()};
} } ```
- 自定义注解
package com.itmck.mckspringbootstarter.config;
import com.itmck.mckspringbootstarter.init.MyImportSelector;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* 太阳当空照,花儿对我笑
* <p>
* Create by M ChangKe 2021/8/7 22:01
**/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({MyImportSelector.class})
public @interface Mck {
}
启动类上开启注解 @Mck ```java @Mck @SpringBootApplication public class SpringbootMqApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMqApplication.class, args);
}
}
```