赞
踩
1. 为什么要用Starter?
所以在2012 年 10 月,一个叫 Mike Youngstrom 的人在 Spring Jira 中创建了一个功能请求,要求在 Spring Framework 中支持无容器 Web 应用程序体系结构,提出了在主容器引导 Spring 容器内配置 Web 容器服务;这件事情对 SpringBoot 的诞生应该说是起到了一定的推动作用。
所以SpringBoot 设计的目标就是简化繁琐配置,快速建立Spring 应用。
在使用spring-boot-starter,会发现,有的项目名称是 XX-spring-boot-starter,有的是spring-boot-starter-XX,这个项目的名称有什么讲究呢?从springboot官方文档摘录:
这段话的大概意思就是,麻烦大家遵守这个命名规范:
Srping官方命名格式为:spring-boot-starter-{name}
非Spring官方建议命名格式:{name}-spring-boot-starter
下面我就以记录日志的一个组件为示例来讲述开发一个starter 的过程。
首先新建一个maven 工程,名称定义为jd-log-spring-boot-starter
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.5.13</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.jd</groupId>
- <artifactId>jd-log-spring-boot-starter</artifactId>
- <version>1.0-SNAPSHOT</version>
- <name>jd-log-spring-boot-starter</name>
- <url>http://www.example.com</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- </properties>
-
-
- <dependencies>
- <!-- 提供了自动装配功能-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-autoconfigure</artifactId>
- </dependency>
- <!-- 在编译时会自动收集配置类的条件,写到一个META-INF/spring-autoconfigure-metadata.json中-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- </dependency>
- <!--记录日志会用到切面,所以需要引入-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-aop</artifactId>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </dependency>
- </dependencies>
- <build>
- <

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。