官方Starter依赖梳理

以官方的<artifactId>spring-boot-starter-test</artifactId>场景启动器为例,我们分析一下start场景启动器的pom依赖关系

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-test</artifactId>
  4. <scope>test</scope>
  5. </dependency>

点进<artifactId>spring-boot-starter-test</artifactId>

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4. <!-- This module was also published with a richer model, Gradle metadata, -->
  5. <!-- which should be used instead. Do not delete the following line which -->
  6. <!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
  7. <!-- that they should prefer consuming it instead. -->
  8. <!-- do_not_remove: published-with-gradle-metadata -->
  9. <modelVersion>4.0.0</modelVersion>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-test</artifactId>
  12. <version>2.4.0</version>
  13. <name>spring-boot-starter-test</name>
  14. <description>Starter for testing Spring Boot applications with libraries including JUnit Jupiter, Hamcrest and Mockito</description>
  15. <url>https://spring.io/projects/spring-boot</url>
  16. <organization>
  17. <name>Pivotal Software, Inc.</name>
  18. <url>https://spring.io</url>
  19. </organization>
  20. <licenses>
  21. <license>
  22. <name>Apache License, Version 2.0</name>
  23. <url>https://www.apache.org/licenses/LICENSE-2.0</url>
  24. </license>
  25. </licenses>
  26. <developers>
  27. <developer>
  28. <name>Pivotal</name>
  29. <email>info@pivotal.io</email>
  30. <organization>Pivotal Software, Inc.</organization>
  31. <organizationUrl>https://www.spring.io</organizationUrl>
  32. </developer>
  33. </developers>
  34. <scm>
  35. <connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
  36. <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
  37. <url>https://github.com/spring-projects/spring-boot</url>
  38. </scm>
  39. <issueManagement>
  40. <system>GitHub</system>
  41. <url>https://github.com/spring-projects/spring-boot/issues</url>
  42. </issueManagement>
  43. <dependencies>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter</artifactId>
  47. <version>2.4.0</version>
  48. <scope>compile</scope>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-test</artifactId>
  53. <version>2.4.0</version>
  54. <scope>compile</scope>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-test-autoconfigure</artifactId>
  59. <version>2.4.0</version>
  60. <scope>compile</scope>
  61. </dependency>
  62. <dependency>
  63. <groupId>com.jayway.jsonpath</groupId>
  64. <artifactId>json-path</artifactId>
  65. <version>2.4.0</version>
  66. <scope>compile</scope>
  67. </dependency>
  68. <dependency>
  69. <groupId>jakarta.xml.bind</groupId>
  70. <artifactId>jakarta.xml.bind-api</artifactId>
  71. <version>2.3.3</version>
  72. <scope>compile</scope>
  73. </dependency>
  74. <dependency>
  75. <groupId>org.assertj</groupId>
  76. <artifactId>assertj-core</artifactId>
  77. <version>3.18.1</version>
  78. <scope>compile</scope>
  79. </dependency>
  80. <dependency>
  81. <groupId>org.hamcrest</groupId>
  82. <artifactId>hamcrest</artifactId>
  83. <version>2.2</version>
  84. <scope>compile</scope>
  85. </dependency>
  86. <dependency>
  87. <groupId>org.junit.jupiter</groupId>
  88. <artifactId>junit-jupiter</artifactId>
  89. <version>5.7.0</version>
  90. <scope>compile</scope>
  91. </dependency>
  92. <dependency>
  93. <groupId>org.mockito</groupId>
  94. <artifactId>mockito-core</artifactId>
  95. <version>3.6.0</version>
  96. <scope>compile</scope>
  97. </dependency>
  98. <dependency>
  99. <groupId>org.mockito</groupId>
  100. <artifactId>mockito-junit-jupiter</artifactId>
  101. <version>3.6.0</version>
  102. <scope>compile</scope>
  103. </dependency>
  104. <dependency>
  105. <groupId>org.skyscreamer</groupId>
  106. <artifactId>jsonassert</artifactId>
  107. <version>1.5.0</version>
  108. <scope>compile</scope>
  109. </dependency>
  110. <dependency>
  111. <groupId>org.springframework</groupId>
  112. <artifactId>spring-core</artifactId>
  113. <version>5.3.1</version>
  114. <scope>compile</scope>
  115. </dependency>
  116. <dependency>
  117. <groupId>org.springframework</groupId>
  118. <artifactId>spring-test</artifactId>
  119. <version>5.3.1</version>
  120. <scope>compile</scope>
  121. </dependency>
  122. <dependency>
  123. <groupId>org.xmlunit</groupId>
  124. <artifactId>xmlunit-core</artifactId>
  125. <version>2.7.0</version>
  126. <scope>compile</scope>
  127. <exclusions>
  128. <exclusion>
  129. <artifactId>jaxb-api</artifactId>
  130. <groupId>javax.xml.bind</groupId>
  131. </exclusion>
  132. </exclusions>
  133. </dependency>
  134. </dependencies>
  135. </project>
  136. ---
  137. 最重要的是它,spring-boot-starter
  138. <dependency>
  139. <groupId>org.springframework.boot</groupId>
  140. <artifactId>spring-boot-starter</artifactId>
  141. <version>2.4.0</version>
  142. <scope>compile</scope>
  143. </dependency>
  144. 最重要的是它,spring-boot-test-autoconfigure
  145. <dependency>
  146. <groupId>org.springframework.boot</groupId>
  147. <artifactId>spring-boot-test-autoconfigure</artifactId>
  148. <version>2.4.0</version>
  149. <scope>compile</scope>
  150. </dependency>

路线1:接着点进<artifactId>spring-boot-starter</artifactId>

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4. <!-- This module was also published with a richer model, Gradle metadata, -->
  5. <!-- which should be used instead. Do not delete the following line which -->
  6. <!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
  7. <!-- that they should prefer consuming it instead. -->
  8. <!-- do_not_remove: published-with-gradle-metadata -->
  9. <modelVersion>4.0.0</modelVersion>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter</artifactId>
  12. <version>2.4.0</version>
  13. <name>spring-boot-starter</name>
  14. <description>Core starter, including auto-configuration support, logging and YAML</description>
  15. <url>https://spring.io/projects/spring-boot</url>
  16. <organization>
  17. <name>Pivotal Software, Inc.</name>
  18. <url>https://spring.io</url>
  19. </organization>
  20. <licenses>
  21. <license>
  22. <name>Apache License, Version 2.0</name>
  23. <url>https://www.apache.org/licenses/LICENSE-2.0</url>
  24. </license>
  25. </licenses>
  26. <developers>
  27. <developer>
  28. <name>Pivotal</name>
  29. <email>info@pivotal.io</email>
  30. <organization>Pivotal Software, Inc.</organization>
  31. <organizationUrl>https://www.spring.io</organizationUrl>
  32. </developer>
  33. </developers>
  34. <scm>
  35. <connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
  36. <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
  37. <url>https://github.com/spring-projects/spring-boot</url>
  38. </scm>
  39. <issueManagement>
  40. <system>GitHub</system>
  41. <url>https://github.com/spring-projects/spring-boot/issues</url>
  42. </issueManagement>
  43. <dependencies>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot</artifactId>
  47. <version>2.4.0</version>
  48. <scope>compile</scope>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-autoconfigure</artifactId>
  53. <version>2.4.0</version>
  54. <scope>compile</scope>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-starter-logging</artifactId>
  59. <version>2.4.0</version>
  60. <scope>compile</scope>
  61. </dependency>
  62. <dependency>
  63. <groupId>jakarta.annotation</groupId>
  64. <artifactId>jakarta.annotation-api</artifactId>
  65. <version>1.3.5</version>
  66. <scope>compile</scope>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.springframework</groupId>
  70. <artifactId>spring-core</artifactId>
  71. <version>5.3.1</version>
  72. <scope>compile</scope>
  73. </dependency>
  74. <dependency>
  75. <groupId>org.yaml</groupId>
  76. <artifactId>snakeyaml</artifactId>
  77. <version>1.27</version>
  78. <scope>compile</scope>
  79. </dependency>
  80. </dependencies>
  81. </project>
  82. ---
  83. 最终找到了我们熟悉的<artifactId>spring-boot-autoconfigure</artifactId>
  84. <dependency>
  85. <groupId>org.springframework.boot</groupId>
  86. <artifactId>spring-boot-autoconfigure</artifactId>
  87. <version>2.4.0</version>
  88. <scope>compile</scope>
  89. </dependency>

路线2:或者我们点进<artifactId>spring-boot-test-autoconfigure</artifactId>
发现<artifactId>spring-boot-test-autoconfigure</artifactId>当中也有同样的<artifactId>spring-boot-autoconfigure</artifactId>

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4. <!-- This module was also published with a richer model, Gradle metadata, -->
  5. <!-- which should be used instead. Do not delete the following line which -->
  6. <!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
  7. <!-- that they should prefer consuming it instead. -->
  8. <!-- do_not_remove: published-with-gradle-metadata -->
  9. <modelVersion>4.0.0</modelVersion>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-test-autoconfigure</artifactId>
  12. <version>2.4.0</version>
  13. <name>spring-boot-test-autoconfigure</name>
  14. <description>Spring Boot Test AutoConfigure</description>
  15. <url>https://spring.io/projects/spring-boot</url>
  16. <organization>
  17. <name>Pivotal Software, Inc.</name>
  18. <url>https://spring.io</url>
  19. </organization>
  20. <licenses>
  21. <license>
  22. <name>Apache License, Version 2.0</name>
  23. <url>https://www.apache.org/licenses/LICENSE-2.0</url>
  24. </license>
  25. </licenses>
  26. <developers>
  27. <developer>
  28. <name>Pivotal</name>
  29. <email>info@pivotal.io</email>
  30. <organization>Pivotal Software, Inc.</organization>
  31. <organizationUrl>https://www.spring.io</organizationUrl>
  32. </developer>
  33. </developers>
  34. <scm>
  35. <connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
  36. <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
  37. <url>https://github.com/spring-projects/spring-boot</url>
  38. </scm>
  39. <issueManagement>
  40. <system>GitHub</system>
  41. <url>https://github.com/spring-projects/spring-boot/issues</url>
  42. </issueManagement>
  43. <dependencies>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot</artifactId>
  47. <version>2.4.0</version>
  48. <scope>compile</scope>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-test</artifactId>
  53. <version>2.4.0</version>
  54. <scope>compile</scope>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-autoconfigure</artifactId>
  59. <version>2.4.0</version>
  60. <scope>compile</scope>
  61. </dependency>
  62. </dependencies>
  63. </project>
  64. ---
  65. 还是我们最终熟悉的<artifactId>spring-boot-autoconfigure</artifactId>
  66. <dependency>
  67. <groupId>org.springframework.boot</groupId>
  68. <artifactId>spring-boot-autoconfigure</artifactId>
  69. <version>2.4.0</version>
  70. <scope>compile</scope>
  71. </dependency>

image.png
结论:官方自定义的starter-test,引入了如下两个关键依赖

  • spring-boot-starter
  • spring-boot-test-autoconfigure

这两个依赖殊途同归,最后生效的或者最终都依赖的是这个<artifactId>spring-boot-autoconfigure</artifactId>

场景启动器jar包分析【空jar包】

image.png
image.png
可以发现官方的任何一个场景启动器其实是一个空的内容,场景启动器的功能只是声明了有哪些依赖,把依赖给引进去
还是以<artifactId>spring-boot-starter-test</artifactId>为例
点进去:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4. <!-- This module was also published with a richer model, Gradle metadata, -->
  5. <!-- which should be used instead. Do not delete the following line which -->
  6. <!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
  7. <!-- that they should prefer consuming it instead. -->
  8. <!-- do_not_remove: published-with-gradle-metadata -->
  9. <modelVersion>4.0.0</modelVersion>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-test</artifactId>
  12. <version>2.4.0</version>
  13. <name>spring-boot-starter-test</name>
  14. <description>Starter for testing Spring Boot applications with libraries including JUnit Jupiter, Hamcrest and Mockito</description>
  15. <url>https://spring.io/projects/spring-boot</url>
  16. <organization>
  17. <name>Pivotal Software, Inc.</name>
  18. <url>https://spring.io</url>
  19. </organization>
  20. <licenses>
  21. <license>
  22. <name>Apache License, Version 2.0</name>
  23. <url>https://www.apache.org/licenses/LICENSE-2.0</url>
  24. </license>
  25. </licenses>
  26. <developers>
  27. <developer>
  28. <name>Pivotal</name>
  29. <email>info@pivotal.io</email>
  30. <organization>Pivotal Software, Inc.</organization>
  31. <organizationUrl>https://www.spring.io</organizationUrl>
  32. </developer>
  33. </developers>
  34. <scm>
  35. <connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
  36. <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
  37. <url>https://github.com/spring-projects/spring-boot</url>
  38. </scm>
  39. <issueManagement>
  40. <system>GitHub</system>
  41. <url>https://github.com/spring-projects/spring-boot/issues</url>
  42. </issueManagement>
  43. <dependencies>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter</artifactId>
  47. <version>2.4.0</version>
  48. <scope>compile</scope>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-test</artifactId>
  53. <version>2.4.0</version>
  54. <scope>compile</scope>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-test-autoconfigure</artifactId>
  59. <version>2.4.0</version>
  60. <scope>compile</scope>
  61. </dependency>
  62. <dependency>
  63. <groupId>com.jayway.jsonpath</groupId>
  64. <artifactId>json-path</artifactId>
  65. <version>2.4.0</version>
  66. <scope>compile</scope>
  67. </dependency>
  68. <dependency>
  69. <groupId>jakarta.xml.bind</groupId>
  70. <artifactId>jakarta.xml.bind-api</artifactId>
  71. <version>2.3.3</version>
  72. <scope>compile</scope>
  73. </dependency>
  74. <dependency>
  75. <groupId>org.assertj</groupId>
  76. <artifactId>assertj-core</artifactId>
  77. <version>3.18.1</version>
  78. <scope>compile</scope>
  79. </dependency>
  80. <dependency>
  81. <groupId>org.hamcrest</groupId>
  82. <artifactId>hamcrest</artifactId>
  83. <version>2.2</version>
  84. <scope>compile</scope>
  85. </dependency>
  86. <dependency>
  87. <groupId>org.junit.jupiter</groupId>
  88. <artifactId>junit-jupiter</artifactId>
  89. <version>5.7.0</version>
  90. <scope>compile</scope>
  91. </dependency>
  92. <dependency>
  93. <groupId>org.mockito</groupId>
  94. <artifactId>mockito-core</artifactId>
  95. <version>3.6.0</version>
  96. <scope>compile</scope>
  97. </dependency>
  98. <dependency>
  99. <groupId>org.mockito</groupId>
  100. <artifactId>mockito-junit-jupiter</artifactId>
  101. <version>3.6.0</version>
  102. <scope>compile</scope>
  103. </dependency>
  104. <dependency>
  105. <groupId>org.skyscreamer</groupId>
  106. <artifactId>jsonassert</artifactId>
  107. <version>1.5.0</version>
  108. <scope>compile</scope>
  109. </dependency>
  110. <dependency>
  111. <groupId>org.springframework</groupId>
  112. <artifactId>spring-core</artifactId>
  113. <version>5.3.1</version>
  114. <scope>compile</scope>
  115. </dependency>
  116. <dependency>
  117. <groupId>org.springframework</groupId>
  118. <artifactId>spring-test</artifactId>
  119. <version>5.3.1</version>
  120. <scope>compile</scope>
  121. </dependency>
  122. <dependency>
  123. <groupId>org.xmlunit</groupId>
  124. <artifactId>xmlunit-core</artifactId>
  125. <version>2.7.0</version>
  126. <scope>compile</scope>
  127. <exclusions>
  128. <exclusion>
  129. <artifactId>jaxb-api</artifactId>
  130. <groupId>javax.xml.bind</groupId>
  131. </exclusion>
  132. </exclusions>
  133. </dependency>
  134. </dependencies>
  135. </project>

我们就以路线1为例<artifactId>spring-boot-starter</artifactId>

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter</artifactId>
  4. <version>2.4.0</version>
  5. <scope>compile</scope>
  6. </dependency>

真正起作用是他,是spring-boot-starter-2.4.0.pom里的最终<artifactId>spring-boot-autoconfigure</artifactId>

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-autoconfigure</artifactId>
  4. <version>2.4.0</version>
  5. <scope>compile</scope>
  6. </dependency>

autoconfigure.jar包分析

最终<artifactId>spring-boot-autoconfigure</artifactId>jar包长这样
image.png

自定义starter

上面理梳理了starter的依赖,真正起作用是他,是spring-boot-starter-2.4.0.pom里的最终<artifactId>spring-boot-autoconfigure</artifactId>

另外还要注意命名规范: starter命名规则:官方名称:spring-boot-starter-xxx 第三方命名:xxx-spring-boot-starter

我们创建一个空工程:
添加模块1:atguigu-hello-springboot-starter和模块2:atguigu-hello-springboot-starter-autoconfigurer
模块1依赖模块2,模块1是个空模块,只声明依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.atguigu</groupId>
  7. <artifactId>atguigu-hello-spring-boot-starter</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <dependency>
  11. <groupId>com.atguigu</groupId>
  12. <artifactId>atguigu-hello-spring-boot-starter-autoconfigure</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. </dependency>
  15. </dependencies>
  16. </project>

模块2依赖官方<artifactId>spring-boot-starter</artifactId>

  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"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.4.0</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.atguigu</groupId>
  12. <artifactId>atguigu-hello-spring-boot-starter-autoconfigure</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>atguigu-hello-spring-boot-starter-autoconfigure</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter</artifactId>
  23. </dependency>
  24. </dependencies>
  25. </project>

SpringBoot自定义Starter - 图5

新建空工程

image.png

模块1:atguigu-hello-springboot-starter

模块1只写pom别的都不写,全部删除

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.atguigu</groupId>
  7. <artifactId>atguigu-hello-spring-boot-starter</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <dependency>
  11. <groupId>com.atguigu</groupId>
  12. <artifactId>atguigu-hello-spring-boot-starter-autoconfigure</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. </dependency>
  15. </dependencies>
  16. </project>

模块2:atguigu-hello-springboot-starter-autoconfigure

业务类HelloService

这是我们抽取的抽象业务类:HelloService

  1. package com.atguigu.hello.service;
  2. import com.atguigu.hello.bean.HelloProperties;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. /**
  5. * 默认不要放在容器中
  6. */
  7. public class HelloService {
  8. @Autowired
  9. HelloProperties helloProperties;
  10. public String sayHello(String userName){
  11. return helloProperties.getPrefix() + ":"+userName+"》"+helloProperties.getSuffix();
  12. }
  13. }

属性配置类HelloProperties

上面需要我们注入属性getPrefix,getSuffix,怎么办呢?模仿Spring的自动配置源码,封装属性配置类HelloProperties
并打上注解@ConfigurationProperties("atguigu.hello"),表明谁要用该场景启动器,谁就要在自己的yaml/properties配置指定前缀配置方可生效

  1. package com.atguigu.hello.bean;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. @ConfigurationProperties("atguigu.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. }

开启自动配置HelloServiceAutoConfiguration

开启@EnableConfigurationProperties(HelloProperties.class)注解,使属性配置类HelloProperties.class生效
同时使用@Configuration搭配@Bean以及@ConditionalOnMissingBean(HelloService.class)将我们的目标业务类HelloService有条件的注入到容器,假如引入该场景启动器的**用户自己new了HelloService且自己打了@Bean注解**则使用用户自己new的

  1. package com.atguigu.hello.auto;
  2. import com.atguigu.hello.bean.HelloProperties;
  3. import com.atguigu.hello.service.HelloService;
  4. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  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. @EnableConfigurationProperties(HelloProperties.class) //默认HelloProperties放在容器中
  10. public class HelloServiceAutoConfiguration{
  11. @ConditionalOnMissingBean(HelloService.class)
  12. @Bean
  13. public HelloService helloService(){
  14. HelloService helloService = new HelloService();
  15. return helloService;
  16. }
  17. }

META-INF/spring.factories

同时不要忘记最后一步,在模块2新建META-INF/spring.factories文件,将自动配置类全类名com.atguigu.hello.auto.HelloServiceAutoConfiguration写进META-INF/spring.factories,这样我们的场景启动器才会使我们所有的场景配置生效:
META-INF/spring.factories

  1. # Auto Configure
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  3. com.atguigu.hello.auto.HelloServiceAutoConfiguration

项目结构如下所示:
image.png

其实模块2可以不写的,只有1个moudel也可以实现 如果是1个模块,那么模块1也可以建spring.factories,将模块2的逻辑封装写到模块1,HelloServiceAutoConfiguration写到模块1

打包模块2

打包并打包到本地maven仓库,clean &&install
因为我们模块1依赖模块2,所以先打包模块2,再打包模块1
image.png
这样 包就放到了我们的本地maven仓库E:\dev_dir_temp\apache-maven-3.3.9\respository

  1. [INFO] --- maven-install-plugin:2.5.2:install (default-install) @ atguigu-hello-spring-boot-starter-autoconfigure ---
  2. [INFO] Installing E:\开发教程\springboot2\boot-09-customer-starter\atguigu-hello-spring-boot-starter-autoconfigure\target\atguigu-hello-spring-boot-starter-autoconfigure-0.0.1-SNAPSHOT.jar to E:\dev_dir_temp\apache-maven-3.3.9\respository\com\atguigu\atguigu-hello-spring-boot-starter-autoconfigure\0.0.1-SNAPSHOT\atguigu-hello-spring-boot-starter-autoconfigure-0.0.1-SNAPSHOT.jar
  3. [INFO] Installing E:\开发教程\springboot2\boot-09-customer-starter\atguigu-hello-spring-boot-starter-autoconfigure\pom.xml to E:\dev_dir_temp\apache-maven-3.3.9\respository\com\atguigu\atguigu-hello-spring-boot-starter-autoconfigure\0.0.1-SNAPSHOT\atguigu-hello-spring-boot-starter-autoconfigure-0.0.1-SNAPSHOT.pom
  4. [INFO] ------------------------------------------------------------------------
  5. [INFO] BUILD SUCCESS
  6. [INFO] ------------------------------------------------------------------------
  7. [INFO] Total time: 1.826 s
  8. [INFO] Finished at: 2022-05-08T16:13:55+08:00
  9. [INFO] Final Memory: 28M/263M
  10. [INFO] ------------------------------------------------------------------------
  11. Process finished with exit code 0

tips:如果使用mvn clean package,那么包只会放到我们的项目target目录而已

打包模块1

image.png

  1. [INFO] --- maven-install-plugin:2.4:install (default-install) @ atguigu-hello-spring-boot-starter ---
  2. [INFO] Installing E:\开发教程\springboot2\boot-09-customer-starter\atguigu-hello-spring-boot-starter\target\atguigu-hello-spring-boot-starter-1.0-SNAPSHOT.jar to E:\dev_dir_temp\apache-maven-3.3.9\respository\com\atguigu\atguigu-hello-spring-boot-starter\1.0-SNAPSHOT\atguigu-hello-spring-boot-starter-1.0-SNAPSHOT.jar
  3. [INFO] Installing E:\开发教程\springboot2\boot-09-customer-starter\atguigu-hello-spring-boot-starter\pom.xml to E:\dev_dir_temp\apache-maven-3.3.9\respository\com\atguigu\atguigu-hello-spring-boot-starter\1.0-SNAPSHOT\atguigu-hello-spring-boot-starter-1.0-SNAPSHOT.pom
  4. [INFO] ------------------------------------------------------------------------
  5. [INFO] BUILD SUCCESS
  6. [INFO] ------------------------------------------------------------------------
  7. [INFO] Total time: 0.984 s
  8. [INFO] Finished at: 2022-05-08T16:13:18+08:00
  9. [INFO] Final Memory: 12M/243M
  10. [INFO] ------------------------------------------------------------------------
  11. Process finished with exit code 0

用户项目导入自定义starter使用

用户项目结构如下:
image.png
引入自定义starter,引入的是模块1

  1. <dependency>
  2. <groupId>com.atguigu</groupId>
  3. <artifactId>atguigu-hello-spring-boot-starter</artifactId>
  4. <version>1.0-SNAPSHOT</version>
  5. </dependency>

image.png
编写application.properties

  1. atguigu.hello.prefix=ATGUIGU
  2. atguigu.hello.suffix=88888

注入场景启动器目标业务类Bean

  1. package com.atguigu.boot.controller;
  2. import com.atguigu.hello.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.RestController;
  6. @RestController
  7. public class HelloController {
  8. @Autowired
  9. HelloService helloService;
  10. @GetMapping("/hello")
  11. public String sayHello(){
  12. String s = helloService.sayHello("张三");
  13. return s;
  14. }
  15. }

测试http://localhost:8080/hello
image.png

覆盖注入场景启动器目标业务类Bean

image.png
假如我们在新工程里面自己手动用new的方式创建了HelloService这个Bean,由于我们源码里面写了

  1. @ConditionalOnMissingBean(HelloService.class)
  2. @Bean
  3. public HelloService helloService(){
  4. HelloService helloService = new HelloService();
  5. return helloService;
  6. }

所以,用户创建的HelloServicebean会覆盖源码的HelloServicebean


可以在用户自己注入bean的地方打断点,重新运行新项目发现会执行用户的bean创建
image.png