springboot自定义starter

古城微笑少年丶 2022-05-12 09:36 373阅读 0赞

starter:

1.这个场景需要使用到的依赖是什么?

2.如何编写自动配置

  1. @Configuration //指定这个类是一个配置类
  2. @ConditionalOnxxx //在指定条件成立的情况下自动配置类生效
  3. @AutoConfiguerAfter //指定自动配置类的顺序
  4. @Bean //给容器添加组件
  5. @ConfigurationProperties 结合相关的xxxProperties类来绑定相关配置
  6. @EnableConfigurationProperties//让xxxPropeties生效加入容器

3.模式

启动器只用来做依赖导入;

专门来编写一个自定配置模块;

启动器依赖自动配置,被人只需引入启动器(starter) 命名:自定义启动器名-spring-boot-starter

4.分别新建两个maven项目,启动器为com.hbsi.stater 自动配置为com.hbsi.stater.autoConfigurer

(1)启动器(com.hbsi.stater )模块导入依赖

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.hbsi.springboot</groupId>
  6. <artifactId>hbsi-spring-boot-starter</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <!-- 启动器 -->
  9. <dependencies>
  10. <!--引入自动配置模块 -->
  11. <dependency>
  12. <groupId>com.hbsi.springboot</groupId>
  13. <artifactId>hbsi-spring-boot-starter-autoconfigurer</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. </dependency>
  16. </dependencies>
  17. </project>

(2)自动配置模块(com.hbsi.stater.autoConfigurer)倒入依赖

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.hbsi.springboot</groupId>
  6. <artifactId>hbsi-spring-boot-starter-autoconfigurer</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>2.0.4.RELEASE</version>
  13. <relativePath /> <!-- lookup parent from repository -->
  14. </parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  18. <java.version>1.8</java.version>
  19. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  20. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  21. <java.version>1.8</java.version>
  22. </properties>
  23. <dependencies>
  24. <!-- 引入spring-boot0-starter;所有starter基本的配置 -->
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter</artifactId>
  28. </dependency>
  29. </dependencies>
  30. </project>

(3)新建HelloProperties.class,HelloService.class ,HelloServiceAutoConfiguration

HelloProperties.class

  1. package com.hbsi.starter;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. @ConfigurationProperties(prefix = "com.hbsi")
  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. }

HelloService.class

  1. package com.hbsi.starter;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. public class HelloService {
  4. HelloProperties helloProperties;
  5. public HelloProperties getHelloProperties() {
  6. return helloProperties;
  7. }
  8. public void setHelloProperties(HelloProperties helloProperties) {
  9. this.helloProperties = helloProperties;
  10. }
  11. public String sayHello(String name) {
  12. return helloProperties.getPrefix() + "-" + name +"-" + helloProperties.getSuffix();
  13. }
  14. }

HelloServiceAutoConfiguration

  1. package com.hbsi.starter;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
  5. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. @Configuration
  9. @ConditionalOnWebApplication//web应用才能生效
  10. @EnableConfigurationProperties(HelloProperties.class)//用@EnableConfigurationProperties
  11. //注解使@ConfigurationProperties生效,并从IOC容器中获取bean
  12. public class HelloServiceAutoConfiguration {
  13. @Autowired
  14. HelloProperties helloProperties;
  15. @Bean
  16. public HelloService helloSerice() {
  17. HelloService helloService = new HelloService();
  18. helloService.setHelloProperties(helloProperties);
  19. return helloService;
  20. }
  21. }

(4) 在类路径下新建/META-INF/spring.factories ,让自动配置类生效

  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  2. com.hbsi.starter.HelloServiceAutoConfiguration

(5)将两个maven安装到仓库(install)

(6)在其他maven项目中引入自定义的stater,便可以使用

  1. <!--引入自定义的starter-->
  2. <dependency>
  3. <groupId>com.hbsi.springboot</groupId>
  4. <artifactId>hbsi-spring-boot-starter-autoconfigurer</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. </dependency>

发表评论

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

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

相关阅读

    相关 SpringBoot定义starter

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