项目结构

  1. hello-spring-boot-starter
  2. ├── highness-hello-spring-boot-starter 主要模块
  3. ├── highness-hello-spring-boot-starter-autoconfigure 自动配置模块
  4. └── highness-hello-spring-boot-starter-demo 测试模块

项目依赖

  1. <!-- hello-spring-boot-starter -->
  2. <groupId>top.parak</groupId>
  3. <artifactId>hello-spring-boot-starter</artifactId>
  4. <packaging>pom</packaging>
  5. <version>1.0-SNAPSHOT</version>
  6. <modules>
  7. <module>highness-hello-spring-boot-starter</module>
  8. <module>highness-hello-spring-boot-starter-autoconfigure</module>
  9. <module>highness-hello-spring-boot-starter-demo</module>
  10. </modules>
  11. <properties>
  12. <maven.compiler.source>1.8</maven.compiler.source>
  13. <maven.compiler.target>1.8</maven.compiler.target>
  14. <spring.boot.version>2.2.2.RELEASE</spring.boot.version>
  15. </properties>
  16. <!-- highness-hello-spring-boot-starter -->
  17. <parent>
  18. <artifactId>hello-spring-boot-starter</artifactId>
  19. <groupId>top.parak</groupId>
  20. <version>1.0-SNAPSHOT</version>
  21. </parent>
  22. <modelVersion>4.0.0</modelVersion>
  23. <artifactId>highness-hello-spring-boot-starter</artifactId>
  24. <dependencies>
  25. <dependency>
  26. <artifactId>highness-hello-spring-boot-starter-autoconfigure</artifactId>
  27. <groupId>top.parak</groupId>
  28. <version>${project.version}</version>
  29. </dependency>
  30. </dependencies>
  31. <!-- highness-hello-spring-boot-autoconfigure -->
  32. <parent>
  33. <artifactId>hello-spring-boot-starter</artifactId>
  34. <groupId>top.parak</groupId>
  35. <version>1.0-SNAPSHOT</version>
  36. </parent>
  37. <modelVersion>4.0.0</modelVersion>
  38. <artifactId>highness-hello-spring-boot-starter-autoconfigure</artifactId>
  39. <dependencies>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter</artifactId>
  43. <version>${spring.boot.version}</version>
  44. </dependency>
  45. </dependencies>
  46. <!-- highness-hello-spring-boot-demo -->
  47. <parent>
  48. <artifactId>hello-spring-boot-starter</artifactId>
  49. <groupId>top.parak</groupId>
  50. <version>1.0-SNAPSHOT</version>
  51. </parent>
  52. <modelVersion>4.0.0</modelVersion>
  53. <artifactId>highness-hello-spring-boot-starter-demo</artifactId>
  54. <dependencies>
  55. <dependency>
  56. <groupId>top.parak</groupId>
  57. <artifactId>highness-hello-spring-boot-starter-autoconfigure</artifactId>
  58. <version>${project.version}</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>org.springframework.boot</groupId>
  62. <artifactId>spring-boot-starter-web</artifactId>
  63. <version>${spring.boot.version}</version>
  64. </dependency>
  65. </dependencies>

核心编码

highness-hello-spring-boot-starter为空模块,highness-hello-spring-boot-starter-autoconfigure模块如下:

  1. top
  2. └─parak
  3. └─hello
  4. ├─auto──HelloServiceConfigure
  5. ├─bean──HelloProperties
  6. └─service──HelloService

HelloService

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import top.parak.hello.bean.HelloProperties;
  3. /**
  4. * @author KHighness
  5. * @since 2021-09-01
  6. */
  7. public class HelloService {
  8. @Autowired
  9. private HelloProperties helloProperties;
  10. public String sayHello(String username) {
  11. return helloProperties.getPrefix() + ' ' +
  12. username + helloProperties.getSuffix();
  13. }
  14. }

HelloProperties

  1. import org.springframework.boot.context.properties.ConfigurationProperties;
  2. /**
  3. * @author KHighness
  4. * @since 2021-09-01
  5. */
  6. @ConfigurationProperties("parak.hello")
  7. public class HelloProperties {
  8. private String prefix;
  9. private String suffix;
  10. public String getPrefix() {
  11. return prefix;
  12. }
  13. public void setPrefix(String prefix) {
  14. this.prefix = prefix;
  15. }
  16. public String getSuffix() {
  17. return suffix;
  18. }
  19. public void setSuffix(String suffix) {
  20. this.suffix = suffix;
  21. }
  22. }

HelloServiceConfigure

  1. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  2. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import top.parak.hello.bean.HelloProperties;
  6. import top.parak.hello.service.HelloService;
  7. /**
  8. * @author KHighness
  9. * @since 2021-09-01
  10. */
  11. @Configuration
  12. @ConditionalOnMissingBean(HelloService.class)
  13. @EnableConfigurationProperties(HelloProperties.class)
  14. public class HelloServiceConfigure {
  15. @Bean
  16. public HelloService helloService() {
  17. return new HelloService();
  18. }
  19. }

另外,需要在resources目录下新建META-INF文件夹,新建spring.factories文件:

  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  2. top.parak.hello.auto.HelloServiceConfigure

测试代码

highness-spring-boot-starter-demo模块如下:

  1. top
  2. └─parak
  3. └─hello
  4. ├─controller──HelloController
  5. └─KhighnessApplication

在resources下面新建application.properties文件:

  1. server.port=3333
  2. parak.hello.prefix=Hello
  3. parak.hello.suffix=!

HelloController

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RequestParam;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import top.parak.hello.service.HelloService;
  6. /**
  7. * @author KHighness
  8. * @since 2021-09-01
  9. */
  10. @RestController
  11. public class HelloController {
  12. @Autowired
  13. private HelloService helloService;
  14. @GetMapping("/hello")
  15. public String sayHello(@RequestParam("name") String name) {
  16. return helloService.sayHello(name);
  17. }
  18. }

KhighnessApplication

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. /**
  4. * @author KHighness
  5. * @since 2021-09-01
  6. */
  7. @SpringBootApplication
  8. public class KHighnessApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(KHighnessApplication.class, args);
  11. }
  12. }

测试结果

image.png