SpringBoot Starter世上最全详解
下文可以毫不夸张的说--是互联网世界中最全的一篇针对SpringBootStarter详解的文章
此篇文章将从
SpringBoot Starter的简介
SpringBoot Starter的使用场景
SpringBoot Starter的开发示例
一一分析,如下所示
SpringBoot Starter简介
我们都知道SpringBoot程序的出现,使java web开发变的异常简单, 如:我们无需关心版本问题,无需关注配置问题
因为SpringBoot中有一个重要的开发插件starter
当我们在maven项目中引入相应的starter时,则会将一些配置bean放入到Spring IOC容器中进行管理
starter就是帮助springboot加载一些配置及将一些bean放入到SpringIOC容器中
Starter的使用场景
我们在开发中进行看见*** web-starter mybatis***starter等一些starter 他们的功能就是将一些bean注册到SpringIOC容器中,那么Starter常见的使用场景有哪些呢?
starter常见场景如下所示:
构建通用模块:如:短信、邮件发送模块
基于AOP技术实现日志切面
创建自定义SpringBoot Starter
Starter命令规范
每个Starter都会遵循标准的命名规范
其中分为官方 Starter 的命名方法和自定义 Starter 的命名方式:
SpringBoot 官方命名方式:
格式:spring-boot-starter-{模块名}
例:spring-boot-starter-web
自定义命名方式:
格式:{模块名}-spring-boot-starter
例:mystarter-spring-boot-starter
Starter创建步骤
SpringBoot自动装配原理:
1.classpath 中中寻找 resources/META-INF/spring.factories 文件
2.spring.factories 配置加载 AutoConfigure 类
根据@Conditional注解条件
进行自动配置并将Bean注入Spring IOC容器中
注意事项:
starter创建,不需要启动类,即通常创建一个空maven项目
1.创建 Maven 项目
创建空Maven项目2.添加依赖
<?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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wty</groupId>
<artifactId>email-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--包含自动配置的代码-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!--非必须:编写配置文件时会有提示-->
<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>
</dependencies>
</project>
编写相关属性配置类
@Data
@ConfigurationProperties(prefix = "email.config")
public class EmailProperties {
private String username = "";
private String password = "";
private String host = "smtp.163.com";
private Integer port = 88;
}
编写Starter功能类
@Data
public class EmailService {
private EmailProperties emailProperties;
/**
* 模拟发送邮件
*/
public void sendEmail() {
System.out.println("Sending email... \n" +
"username: " + emailProperties.getUsername() + "\n" +
"password: " + emailProperties.getPassword() + "\n" +
"host: " + emailProperties.getHost() + "\n" +
"port: " + emailProperties.getPort());
}
}
编写自动配置类----核心类
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({EmailService.class})
@EnableConfigurationProperties({EmailProperties.class})
public class EmailAutoConfiguration {
@Autowired
private EmailProperties emailProperties;
@Bean
@ConditionalOnMissingBean
public EmailService emailService() {
EmailService emailService = new EmailService();
emailService.setEmailProperties(emailProperties);
return emailService;
}
}
注解功能说明: @Configuration: 注解修饰表示该类为配置类,会注入到容器中,proxyBeanMethods = false 表示使用 Lite 轻量级模式; @ConditionalOnClass: 注解表示只有存在 EmailService.class 类时才生效自动配置类; @EnableConfigurationProperties: 注解使我们定义的 EmailProperties.class 属性配置类生效。
编写spring.factories文件
在 src/main/resources 下添加 META-INF/spring.factories 文件 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.java265.starter.config.EmailAutoConfiguration
打包 mvn package
测试
另起项目引入
自定义starter依赖
<dependency>
<groupId>com.java265</groupId>
<artifactId>email-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
在yml配置相应属性(idea中会有相应的提示信息)
email:
config:
username: xxx@java265.com
password: 88888
host: smtp.java265.com
port: 25
编写测试代码
@RestController
@RequestMapping("/starter")
public class StarterController {
@Autowired
private EmailService emailService;
@GetMapping("/email")
public void testEmail() {
emailService.sendEmail();
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


