springboot自定义starter源码笔记

桃扇骨 2022-12-21 03:17 153阅读 0赞

starter:
1、这个场景需要使用到的依赖是什么?
2、如何编写自动配置

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

3、模式:
启动器只用来做依赖导入;
专门来写一个自动配置模块;
启动器依赖自动配置;别人只需要引入启动器(starter)
mybatis-spring-boot-starter;自定义启动器名-spring-boot-starter

步骤:
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
  5. http://maven.apache.org/xsd/maven‐4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.atguigu.starter</groupId>
  8. <artifactId>atguigu‐spring‐boot‐starter</artifactId>
  9. <version>1.0‐SNAPSHOT</version>
  10. <!‐‐启动器‐‐>
  11. <dependencies>
  12. <!‐‐引入自动配置模块‐‐>
  13. <dependency>
  14. <groupId>com.atguigu.starter</groupId>
  15. <artifactId>atguigu‐spring‐boot‐starter‐autoconfigurer</artifactId>
  16. <version>0.0.1‐SNAPSHOT</version>
  17. </dependency>
  18. </dependencies>
  19. </project>

2)、自动配置模块

  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‐
  5. 4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.atguigu.starter</groupId>
  8. <artifactId>atguigu‐spring‐boot‐starter‐autoconfigurer</artifactId>
  9. <version>0.0.1‐SNAPSHOT</version>
  10. <packaging>jar</packaging>
  11. <name>atguigu‐spring‐boot‐starter‐autoconfigurer</name>
  12. <description>Demo project for Spring Boot</description>
  13. <parent>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring‐boot‐starter‐parent</artifactId>
  16. <version>1.5.10.RELEASE</version>
  17. <relativePath/> <!‐‐ lookup parent from repository ‐‐>
  18. </parent>
  19. <properties>
  20. <project.build.sourceEncoding>UTF‐8</project.build.sourceEncoding>
  21. <project.reporting.outputEncoding>UTF‐8</project.reporting.outputEncoding>
  22. <java.version>1.8</java.version>
  23. </properties>
  24. <dependencies>
  25. <!‐‐引入spring‐boot‐starter;所有starter的基本配置‐‐>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring‐boot‐starter</artifactId>
  29. </dependency>
  30. </dependencies>
  31. </project>
  32. package com.atguigu.starter;
  33. import org.springframework.boot.context.properties.ConfigurationProperties;
  34. @ConfigurationProperties(prefix = "atguigu.hello")
  35. public class HelloProperties {
  36. private String prefix;
  37. private String suffix;
  38. public String getPrefix() {
  39. return prefix;
  40. }
  41. public void setPrefix(String prefix) {
  42. this.prefix = prefix;
  43. }
  44. public String getSuffix() {
  45. return suffix;
  46. }
  47. public void setSuffix(String suffix) {
  48. this.suffix = suffix;
  49. }
  50. }
  51. package com.atguigu.starter;
  52. public class HelloService {
  53. HelloProperties helloProperties;
  54. public HelloProperties getHelloProperties() {
  55. return helloProperties;
  56. }
  57. public void setHelloProperties(HelloProperties helloProperties) {
  58. this.helloProperties = helloProperties;
  59. }
  60. public String sayHellAtguigu(String name){
  61. return helloProperties.getPrefix()+"‐" +name + helloProperties.getSuffix();
  62. }
  63. }
  64. package com.atguigu.starter;
  65. import org.springframework.beans.factory.annotation.Autowired;
  66. import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
  67. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  68. import org.springframework.context.annotation.Bean;
  69. import org.springframework.context.annotation.Configuration;
  70. @Configuration
  71. @ConditionalOnWebApplication //web应用才生效
  72. @EnableConfigurationProperties(HelloProperties.class)
  73. public class HelloServiceAutoConfiguration {
  74. @Autowired
  75. HelloProperties helloProperties;
  76. @Bean
  77. public HelloService helloService(){
  78. HelloService service = new HelloService();
  79. service.setHelloProperties(helloProperties);
  80. return service;
  81. }
  82. }

更多SpringBoot整合示例
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples

发表评论

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

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

相关阅读

    相关 SpringBoot定义starter

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