自定义starter实现
- 一、starter概述
-
- 1.1、这个场景需要使用到的依赖是什么?
- 1.2、如何编写自动配置?
- 1.3、模式
- 二、代码编写
-
- 2.1、创建工程目录
-
- 2.1.1、创建启动器模块工程目录
- 2.1.2、创建自动配置模块工程目录
- 2.2、将配置模块添加到启动类中
- 2.3、编写自动配置模块
-
- 2.3.1、删除模块中多余的内容
- 2.3.2、添加HelloService类
- 2.3.3、添加HelloProperties配置类
- 2.3.4、添加HelloServiceAutoConfiguration自动配置类
- 2.3.5、添加spring.factories文件
- 2.4、将启动器和自动配置安装到Maven
- 三、编写测试项目,测试自定义starter
-
- 3.1、新建test工程
- 3.2、将自定义starter引入到test工程中
- 3.3、在application.yml配置文件中添加以下配置
- 3.4、编写测试controller
- 3.5、启动测试
一、starter概述
1.1、这个场景需要使用到的依赖是什么?
1.2、如何编写自动配置?
@Configuration //指定这个类是一个配置类
@ConfigurationOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定配置类的顺序
@Bean //给Spring容器中添加组件
@ConfigurationProperties //结合XXXProperties类来绑定配置
@EnableConfigurationProperties //生效某个配置类,并加入到容器中
自动配置类应该如何加载?
需要将启动就加载的自动配置类,配置在META-INF/spring- factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.moss.starter.config.FtpConfiguration
1.3、模式

启动器只用来做依赖导入;
专门写一个自动配置模块;
启动器依赖自动配置,别人只需要引入启动器(starter)
二、代码编写
2.1、创建工程目录
2.1.1、创建启动器模块工程目录
选择创建一个空工程。编写好自己的名称后,点击finish。我这边的工程目录命名为spring-boot-ftp-starter
按下图箭头指示创建一个maven模块
添加Module相关信息
点击finish完成启动器模块的创建。
2.1.2、创建自动配置模块工程目录
再次点击加号,选择new module。
2.2、将配置模块添加到启动类中
2.3、编写自动配置模块
2.3.1、删除模块中多余的内容
删除以下五部分的内容:
- 1.删除Spring Boot项目的启动类;
- 2.删除/resource目录下的application.properties文件;
- 3.删除/test目录下的测试相关文件;
- 4.删除pom.xml依赖管理中的test依赖;
- 5.删除pom.xml依赖管理中的打包插件;
【SpringBoot】自定义starter实现详解一、starter概述二、代码编写三、编写测试项目,测试自定义starter
2.3.2、添加HelloService类
package com.moss.starter;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
/**
* @author lwj
*/
@Data
public class HelloService {
@Autowired
private HelloProperties helloProperties;
public String sayHello(String name) {
return helloProperties.getPrefix() + "|" + name + "|" + helloProperties.getSuffix();
}
}
2.3.3、添加HelloProperties配置类
在添加@ConfigurationProperties(prefix = “moss-ftp”)注解的时候,会跳出警告提示,点击右侧的提示,会跳转到一个Spring的网页。
这是Idea工具提示我们如果需要添加配置时的提示信息,可以导入下面的这个依赖。
在pom.xml中引入该依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
package com.moss.starter;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "moss-ftp")
public class HelloProperties {
private String prefix;
private String suffix;
}
2.3.4、添加HelloServiceAutoConfiguration自动配置类
package com.moss.starter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
public HelloProperties helloProperties;
@Bean
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
2.3.5、添加spring.factories文件
在这里我们可以参照spring的autoconfigure配置
在本Demo中需要添加的如下:
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.moss.starter.HelloServiceAutoConfiguration
2.4、将启动器和自动配置安装到Maven
点击右侧Maven中两个模块下的install指令,将项目安装到maven工程;
三、编写测试项目,测试自定义starter
3.1、新建test工程
新建Spring Boot项目
由于之前在自定义starter中配置了@ConditionalOnWebApplication,所以在这里需要勾选添加Spring Web相关的依赖。
3.2、将自定义starter引入到test工程中
在test项目的pom.xml依赖配置中添加自定义starter依赖。
<dependency>
<groupId>com.moss.starter</groupId>
<artifactId>moss-ftp-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
3.3、在application.yml配置文件中添加以下配置
moss-ftp:
prefix: China
suffix: Hello
3.4、编写测试controller
新建HelloController类
package com.moss.springboot;
import com.moss.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String hello() {
return helloService.sayHello("李四");
}
}
3.5、启动测试
启动项目
打开浏览器,在浏览器中输入URL:http://localhost:8080/hello
至此自定义starter已经实现了!!!