SpringBoot 自定义 starter

素颜马尾好姑娘i 2022-01-20 11:49 444阅读 0赞

一、简介

SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入SpringBoot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应的功能。即使是这样,SpringBoot也不能囊括我们所有的使用场景,往往我们需要自定义starter,来简化我们对SpringBoot的使用。

二、如何自定义starter

1.实例

如何编写自动配置 ?

我们参照@WebMvcAutoConfiguration为例,我们看看需要准备哪些东西,下面是WebMvcAutoConfiguration的部分代码:

  1. @Configuration
  2. @ConditionalOnWebApplication
  3. @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class})
  4. @ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
  5. @AutoConfigureOrder(-2147483638)
  6. @AutoConfigureAfter({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class})
  7. public class WebMvcAutoConfiguration {
  8. @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
  9. @EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})
  10. public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {
  11. @Bean
  12. @ConditionalOnBean({View.class})
  13. @ConditionalOnMissingBean
  14. public BeanNameViewResolver beanNameViewResolver() {
  15. BeanNameViewResolver resolver = new BeanNameViewResolver();
  16. resolver.setOrder(2147483637);
  17. return resolver;
  18. }
  19. }
  20. }
  21. 复制代码

我们可以抽取到我们自定义starter时,同样需要的一些配置。

  1. @Configuration //指定这个类是一个配置类
  2. @ConditionalOnXXX //指定条件成立的情况下自动配置类生效
  3. @AutoConfigureOrder //指定自动配置类的顺序
  4. @Bean //向容器中添加组件
  5. @ConfigurationProperties //结合相关xxxProperties来绑定相关的配置
  6. @EnableConfigurationProperties //让xxxProperties生效加入到容器中
  7. 自动配置类要能加载需要将自动配置类,配置在META-INF/spring.factories
  8. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  9. org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
  10. org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
  11. 复制代码

模式

我们参照 spring-boot-starter 我们发现其中没有代码:

我们在看它的pom中的依赖中有个 springboot-starter

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter</artifactId>
  4. </dependency>
  5. 复制代码

我们再看看 spring-boot-starter 有个 spring-boot-autoconfigure

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-autoconfigure</artifactId>
  4. </dependency>
  5. 复制代码

关于web的一些自动配置都写在了这里 ,所以我们有以下总结:

  1. 启动器starter只是用来做依赖管理
  2. 需要专门写一个类似spring-boot-autoconfigure的配置模块
  3. 用的时候只需要引入启动器starter,就可以使用自动配置了
  4. 复制代码

命名规范

官方命名空间

  • 前缀:spring-boot-starter-
  • 模式:spring-boot-starter-模块名
  • 举例:spring-boot-starter-web、spring-boot-starter-jdbc

自定义命名空间

  • 后缀:-spring-boot-starter
  • 模式:模块-spring-boot-starter
  • 举例:mybatis-spring-boot-starter

三、自定义starter实例

我们需要先创建两个工程 hello-spring-boot-starterhello-spring-boot-starter-autoconfigurer

1. hello-spring-boot-starter

1.pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.gf</groupId>
  5. <artifactId>hello-spring-boot-starter</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>hello-spring-boot-starter</name>
  9. <!-- 启动器 -->
  10. <dependencies>
  11. <!-- 引入自动配置模块 -->
  12. <dependency>
  13. <groupId>com.gf</groupId>
  14. <artifactId>hello-spring-boot-starter-autoconfigurer</artifactId>
  15. <version>0.0.1-SNAPSHOT</version>
  16. </dependency>
  17. </dependencies>
  18. </project>
  19. 复制代码

同时删除 启动类、resources下的文件,test文件。

2. hello-spring-boot-starter-autoconfigurer

1. pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.gf</groupId>
  5. <artifactId>hello-spring-boot-starter-autoconfigurer</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>hello-spring-boot-starter-autoconfigurer</name>
  9. <description>Demo project for Spring Boot</description>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>1.5.9.RELEASE</version>
  14. <relativePath/> <!-- lookup parent from repository -->
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. <dependencies>
  22. <!-- 引入spring-boot-starter,所有starter的基本配合 -->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter</artifactId>
  26. </dependency>
  27. </dependencies>
  28. </project>
  29. 复制代码

2. HelloProperties

  1. package com.gf.service;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. @ConfigurationProperties(prefix = "gf.hello")
  4. public class HelloProperties {
  5. private String prefix;
  6. private String suffix;
  7. public String getPrefix() {
  8. return prefix;
  9. }
  10. public void setPrefix(String prefix) {
  11. this.prefix = prefix;
  12. }
  13. public String getSuffix() {
  14. return suffix;
  15. }
  16. public void setSuffix(String suffix) {
  17. this.suffix = suffix;
  18. }
  19. }
  20. 复制代码

3. HelloService

  1. package com.gf.service;
  2. public class HelloService {
  3. HelloProperties helloProperties;
  4. public HelloProperties getHelloProperties() {
  5. return helloProperties;
  6. }
  7. public void setHelloProperties(HelloProperties helloProperties) {
  8. this.helloProperties = helloProperties;
  9. }
  10. public String sayHello(String name ) {
  11. return helloProperties.getPrefix()+ "-" + name + helloProperties.getSuffix();
  12. }
  13. }
  14. 复制代码

4. HelloServiceAutoConfiguration

  1. package com.gf.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
  4. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. @Configuration
  8. @ConditionalOnWebApplication //web应该生效
  9. @EnableConfigurationProperties(HelloProperties.class)
  10. public class HelloServiceAutoConfiguration {
  11. @Autowired
  12. HelloProperties helloProperties;
  13. @Bean
  14. public HelloService helloService() {
  15. HelloService service = new HelloService();
  16. service.setHelloProperties( helloProperties );
  17. return service;
  18. }
  19. }
  20. 复制代码

5. spring.factories

resources 下创建文件夹 META-INF 并在 META-INF 下创建文件 spring.factories ,内容如下:

  1. # Auto Configure
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  3. com.gf.service.HelloServiceAutoConfiguration
  4. 复制代码

到这儿,我们的配置自定义的starter就写完了 ,我们把 hello-spring-boot-starter-autoconfigurer、hello-spring-boot-starter 安装成本地jar包。

三、测试自定义starter

我们创建个项目 hello-spring-boot-starter-test,来测试系我们写的stater。

1. pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.gf</groupId>
  5. <artifactId>hello-spring-boot-starter-test</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>hello-spring-boot-starter-test</name>
  9. <description>Demo project for Spring Boot</description>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>1.5.9.RELEASE</version>
  14. <relativePath/> <!-- lookup parent from repository -->
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <!-- 引入自定义starter -->
  27. <dependency>
  28. <groupId>com.gf</groupId>
  29. <artifactId>hello-spring-boot-starter</artifactId>
  30. <version>0.0.1-SNAPSHOT</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-test</artifactId>
  35. <scope>test</scope>
  36. </dependency>
  37. </dependencies>
  38. <build>
  39. <plugins>
  40. <plugin>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-maven-plugin</artifactId>
  43. </plugin>
  44. </plugins>
  45. </build>
  46. </project>
  47. 复制代码

2. HelloController

  1. package com.gf.controller;
  2. import com.gf.service.HelloService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class HelloController {
  9. @Autowired
  10. HelloService helloService;
  11. @GetMapping("/hello/{name}")
  12. public String hello(@PathVariable(value = "name") String name) {
  13. return helloService.sayHello( name + " , " );
  14. }
  15. }
  16. 复制代码

3. application.properties

  1. gf.hello.prefix = hi
  2. gf.hello.suffix = what's up man ? 复制代码

我运行项目访问 http://127.0.0.1:8080/hello/zhangsan,结果如下:

  1. hi-zhangsan , what's up man ? 复制代码

源码下载: github.com/gf-huanchup…

发表评论

表情:
评论列表 (有 0 条评论,444人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringBoot定义starter

    SpringBoot自定义starter 按照一般的模式, 我们创建一个启动器, 但是该启动器只用来做依赖导入 然后创建另外一个自动配置模块, 用来定义自动配置 启动