SpringBoot自定义starter

野性酷女 2022-05-25 01:28 375阅读 0赞

分析

自定义 starter 分为两个模块:

  • starter 启动器模块,空的 jar 模块,pom 引入自动配置模块。别人引用 starter 时只需引入 starter启动器模块即可
  • 自动配置模块

命名规范:
后缀:-spring-boot-starter
范例:mybatis-spring-boot-starter

自定义 starter 分析(自动配置模块):

  1. 我们的 starter 需要什么依赖
  2. 是否需要属性配置
  3. 配置类中需要提供哪些 Bean 交给 Spring 容器管理
  4. starter 加载是否依赖某些条件
  5. 提供 META-INF/spring.factories 文件供@EnableAutoConfiguration扫描

demo

  1. maven 项目
  2. 创建 mytest-spring-boot-starter 启动器模块,选择 jar
  3. 创建 mytest-spring-boot-starter-autoconfigure 自动配置模块,选择 jar
  4. mytest-spring-boot-starterpom 中引入 mytest-spring-boot-starter-autoconfigure

    1. <dependencies>
    2. <dependency>
    3. <groupId>spring-boot</groupId>
    4. <artifactId>mytest-spring-boot-starter-autoconfigure</artifactId>
    5. <version>0.0.1-SNAPSHOT</version>
    6. </dependency>
    7. </dependencies>
  5. 配置mytest-spring-boot-starter-autoconfigure的pom文件

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter</artifactId>
    5. <version>1.4.7.RELEASE</version>
    6. </dependency>
    7. </dependencies>
  6. mytest-spring-boot-starter-autoconfigure代码

    1. // 属性配置,这个配置在真正的项目中,需要在 application.yml 中配置 p7.hello 的配置项
    2. @ConfigurationProperties(prefix = "p7.hello")
    3. public class HelloProperties {
    4. private String prefix;
    5. private String suffix;
    6. public String getPrefix() {
    7. return prefix;
    8. }
    9. public void setPrefix(String prefix) {
    10. this.prefix = prefix;
    11. }
    12. public String getSuffix() {
    13. return suffix;
    14. }
    15. public void setSuffix(String suffix) {
    16. this.suffix = suffix;
    17. }
    18. }
    19. // 我们要让别人使用这个类
    20. public class HelloService {
    21. private HelloProperties helloProperties;
    22. public String sayHello(String name) {
    23. return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();
    24. }
    25. public HelloProperties getHelloProperties() {
    26. return helloProperties;
    27. }
    28. public void setHelloProperties(HelloProperties helloProperties) {
    29. this.helloProperties = helloProperties;
    30. }
    31. }
    32. // 配置类
    33. @Configuration
    34. // 在 Web 环境下才被加载
    35. @ConditionalOnWebApplication
    36. // 使配置属性立即生效
    37. @EnableConfigurationProperties(value = HelloProperties.class)
    38. public class HelloServiceAutoConfiguration {
    39. @Autowired
    40. private HelloProperties helloProperties;
    41. @Bean
    42. public HelloService helloService() {
    43. HelloService service = new HelloService();
    44. service.setHelloProperties(helloProperties);
    45. return service;
    46. }
    47. }
    48. // 在 src/main/resources 下创建 META-INF 文件夹,再创建 spring.factories 文件,配置我们的配置类
    49. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    50. com.p7.autoconfigure.HelloServiceAutoConfiguration
  7. 创建 Springbootweb 工程,在 pom 中引入 starter 启动器

    1. <dependency>
    2. <groupId>spring-boot</groupId>
    3. <artifactId>mytest-spring-boot-starter</artifactId>
    4. <version>0.0.1-SNAPSHOT</version>
    5. </dependency>
  8. application.yml 中配置 starter 中需要的属性

    1. p7:
    2. hello:
    3. prefix: p7
    4. suffix: hello
  9. 创建测试 Controller

    1. @RestController("/hello")
    2. public class HelloController {
    3. @Autowired
    4. private HelloService helloService;
    5. @GetMapping
    6. public String hello(){
    7. return helloService.sayHello("say");
    8. }
    9. }
  10. 启动 web 项目,访问 localhost:8080/hello

    1. // web 启动类
    2. @SpringBootApplication
    3. public class App {
    4. public static void main(String[] args) {
    5. SpringApplication.run(App.class, args);
    6. }
    7. }

    这里写图片描述

发表评论

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

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

相关阅读

    相关 SpringBoot定义starter

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