自定义Starter
1.Starter介绍
Starter场景启动器是Spring Boot中一种非常重要的机制,它将繁琐的配置统一集成到stater中,我们只需要通过在Maven将starter的依赖导入项目,SpringBoot 就能自动扫描并加载相应的默认配置。starter的出现简化了开发人员的工作量,从繁琐的框架配置中解救出来,让更多的时间专注在业务逻辑的开发,提高了开发效率。
2.命名规范
SpringBoot 提供的 starter 以 spring-boot-starter-xxx 的形式命名。为了与 SpringBoot 生态提供的 starter 进行区分,官方建议第三方开发者或技术(例如 Druid、Mybatis 等等)厂商自定义的 starter 使用 xxx-spring-boot-starter 的形式命名,例如 mybatis-spring-boot-starter、druid-spring-boot-starter 等等。
3.模块规范
Spring Boot 官方建议我们在自定义 starter 时,创建两个 Module :autoConfigure Module(SpringBoot模块) 和 starter Module(Maven 模块),其中 starter Module 依赖于 autoConfigure Module。当然,这只是 Spring Boot 官方的建议,并不是硬性规定,若不需要自动配置代码和依赖项目分离,我们也可以将它们组合到同一个 Module 里。
4.自定义Stater
自定义stater分为以下7步:
1.创建工程
2. 添加POM依赖
3. 定义Properties类
4. 定义Service类
5. 定义配置类
6. 创建spring.factories文件
7. 构建stater
5.创建工程
1.在Intellij IDEA创建一个空项目(Empty Project)
2.添加New Module
在Project Structure 界面,,点击左上角的“+”按钮,选择“New Module”,新建一个名为 liang-hello-spring-boot-starter 的 Maven Module 和一个名为 liang-helllo-spring-boot-starter-autoconfiguration 的 Spring Boot Module两个模块
3.添加POM依赖
在liang-hello-spring-boot-starter 的 pom.xml 中将 liang-helllo-spring-boot-starter-autoconfiguration 作为其依赖项
<dependencies>
<dependency>
<groupId>com.liang</groupId>
<artifactId>liang-spring-boot-autoconfiguration</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
4.定义Properties
在 liang-helllo-spring-boot-starter-autoconfiguration 的 com.liang.properties 包中,创建一个实体类:HelloProperties,通过它来映射配置信息。
package com.liang.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
//让当前类的属性和配置文件中以com.liang.hello开头的配置进行绑定
@ConfigurationProperties("com.liang.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
5.定义Service
在 liang-helllo-spring-boot-starter-autoconfiguration 的 com.liang.service 中,创建一个 Service 类:HelloService,供其他项目使用,
package com.liang.service;
import com.liang.properties.HelloProperties;
import org.springframework.beans.factory.annotation.Autowired;
public class HelloService {
@Autowired
private HelloProperties helloProperties;
public String sayHello(String name)
{
return helloProperties.getPrefix()+name+helloProperties.getSuffix();
}
}
6.定义配置类
在 liang-helllo-spring-boot-starter-autoconfiguration 的 com.liang.auotconfiguration 中,创建一个配置类:HelloAutoConfiguration。
import com.liang.properties.HelloProperties;
import com.liang.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //标识配置类
//将使用了ConfigurationProperties注解的类以组件的形式加入到容器中
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {
@ConditionalOnMissingBean(HelloService.class) //当前容器中没有HelloService这个组件时时生效
@Bean //添加组件,将HelloService以组件的形式添加到容器中
public HelloService helloService()
{
HelloService helloService = new HelloService();
return helloService;
}
}
7.创建spring.factories文件
Spring Factories机制是Spring Boot中一种服务发现机制,这种机制与Java SPI类似。Spring Boot会自动扫描所有Jat包类路径下META-INF/spring.factories文件,并读取其中的内容,进行实例化。这种机制也是Spring Boot Starter的基础。
我们在liangg-helllo-spring-boot-starter-autoconfiguration 的类路径下(resources )中创建一个 META-INF 文件夹,并在 META-INF 文件夹中创建一个 spring.factories 文件,并添加如下配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.liang.autoConfiguration.HelloAutoConfiguration
因为Spring Boot的自动配置时基于Spring Factories机制实现的,所以我们将Spring Boot的EnableAutoConfiguration接口与自定义的的自动配置类 HelloAutoConfiguration组成一组键值对添加到spring.factories文件中,方便 Spring Boot 在启动时,获取到自定义 starter 的自动配置。
8.构建starter
我们需要通过Maven将stater安装到本地仓库,供其他项目使用。
点击install安装到本地仓库,如果没有配置自定义的Maven的话,系统自带的Maven仓库是将我们的jar安装到以下地方。
我们可以在repository找到自定义starterj的Jar包。
9.创建一个Spring Boot项目
创建一个名叫springboot-10-starter的Spring Boot项目,添加自定义的starter依赖
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入自定义的starter-->
<dependency>
<groupId>com.liang</groupId>
<artifactId>liang-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 测试环境-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
10.application.properties
在 Spring Boot 配置文件 application.properties 中,添加以下属性配置。
# starter prefix
com.liang.hello.prefix=你好
# starter suffix
com.liang.hello.suffix=很高兴认识你!
11.定义Controller
在 com.liangcontroller 中创建一个控制器HelloController。
package com.liang.controller;
import com.liang.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("hello")
public String Hello(String name)
{
return helloService.sayHello(name);
}
}
12.启动Spring Boot测试
搞定,自定义的Starter就完成了