How to write a springboot starter by hand?

This article mainly shares how to write a spring starter by hand and open source your code as a jar package.
Naming rules (do not use spring-boot at the beginning to avoid renaming spring-boot officially using your starter in the future)
The official naming format is: spring-boot-starter-xxx
The unofficial naming format is: xxx-spring-boot-starter

1. The original project that needs to be open source (file-storage-core)

For example, you write a framework that encapsulates file storage, supports local disks, Alibaba Cloud OSS, MinIO, Amazon AWS, Baidu BOS, Huawei Cloud OBS, JD Cloud OOS, Tencent Cloud COS, NetEase NOS, etc. The project does not depend on springboot .
You want to open source this framework so that colleagues in need can quickly use it in the springboot project. The best way is to provide a springboot starter project.

2. Add the springboot starter project (file-storage-spring-boot-starter)

1. Pom.xml adds automatic configuration dependencies, scope needs to be set to provided, because the version is not necessarily consistent with project X (a project that depends on the starter), so scope=provided is to let project X decide to automatically configure the dependent version
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>2.0.5.RELEASE</version>
    <scope>provided</scope>
</dependency>
2. pom.xml adds file-storage-core dependencies, there is no need to set scope here, so that after project X relies on file-storage-spring-boot-starter, it will automatically add file-storage-core to dependencies
<dependency>
    <groupId>com.jqdi</groupId>
    <artifactId>file-storage-core</artifactId>
    <version>1.0.0</version>
</dependency>
3. Add FileStorageAutoConfiguration to create spring beans
4. Add /META-INF/spring.factories under src/main/resources to complete the automatic configuration (here is the spi mechanism using springboot, you can learn more about the automatic configuration of springboot)
Confused?
Question 1: Is it okay to not add @Configuration to FileStorageAutoConfiguration?
Answer: OK
The bean of FileStorageAutoConfiguration is done by EnableAutoConfiguration @Import, not by bean scanning. But what is strange is that the open source jars provided by the industry, such as RedisAutoConfiguration, RabbitAutoConfiguration, and MybatisAutoConfiguration all retain this @Configuration annotation.
Question 2: Is it okay not to use the spring.factories configuration file?
Answer: Yes, but generally not recommended
1) The @SpringBootApplication project will scan the root directory and subdirectories. Generally, the package of project X is different from the package of the open source jar package. If they are the same, you can directly scan to FileStorageAutoConfiguration according to @Configuration in 1.
2) Or add @Import(FileStorageAutoConfiguration.class) to the startup class. The principle is the same as that of spring.factories.

3. Add the demo of project X (file-storage-springboot-demo)

1.pom.xml adds file-storage-spring-boot-starter dependencies
<dependency>
    <groupId>com.jqdi</groupId>
    <artifactId>file-storage-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>
2. Use the function of file storage in the way of @Autowired
How about it? If you find it useful, don't hesitate to collect it! ! !
Attachment: the code directory involved

Guess you like

Origin blog.csdn.net/w13528476101/article/details/128997024