开发者

SpringBoot定制化Starter实现方法

开发者 https://www.devze.com 2023-01-13 10:39 出处:网络 作者: 百思不得小赵
目录什么是Spring Boot Starterandroid实现步骤启动器自动配置包总结自定义Starter的实现逻辑Spring Boot Starter官网描述:Spring Boot Starter官方介绍
目录
  • 什么是Spring Boot Starterandroid
  • 实现步骤
    • 启动器
    • 自动配置包
  • 总结自定义Starter的实现逻辑

    Spring Boot Starter官网描述:Spring Boot Starter官方介绍

    什么是Spring Boot Starter

    Starters可以理解为启动器,它包含了一系列可以集成到应用里面的依赖包,可以一站式集成 Spring和其他技术,而不需要到处找示例代码和依赖包。Spring Boot Starter的工作原理是:Spring BFkAZdXpFOnoot在启动时扫描项目所依赖的JAR包,寻找包含spring.factories文件的JAR包,根据spring.factories配置加载AutoConfigure类,根据@Conditional注解的条件,进行自动配置并将Bean注入Spring Context

    为什么要自定义Spring Boot Starter?

    在Spring Boot官网为了简化我们的开发,已经提供了非常多场景的Starter来为我们使用,即便如此,也无法全面的满足我们实际工作中的开发场景,这时我们就需要自定义实现定制化的Starter。

    SpringBoot定制化Starter实现方法

    实现步骤

    1.首javascript先,创建一个Maven空工程,添加两个模块

    启动器

    启动器中没有任何的源代码,只是告诉我们当前场景需要引入哪些依赖即可!

    创建启动器模块为maven工程,命名为

    xiaozhao-hello-spring-boot-starter,对应的依赖文件

    <groupId>com.zhao</groupId>
    <artifactId>xiaozhao-hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    

    自动配置包

    自动配置包中实现了所有的自动配置功能!

    创建自动配置包模块为SpringBoot初始化工程,命名为xiaozhao-hello-spring-boot-starter-autoconfigure

    SpringBoot定制化Starter实现方法

    最终的项目模块如下:

    SpringBoot定制化Starter实现方法

    2.模块创建完成后,需要在启动器中引入自动配置模块(别人引入场景启动器,自动配置包就会自动引入)

    <dependencies>
        <dependency>
            <groupId>com.zhao</groupId>
            <artifactId>xiaozhao-hello-spring-boot-starter-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    

    3.编写自动配置模块。

    创建自定义的Properties文件

    @ConfigurationProperties("xiaozhao.hello")
    public class HelloProperties {
        private String prefix;
        private String suffix;
        public String getPrefix() {
            return prefix;
        }
        public void setPrefix(String prefix) {
            this.prefix = prefix;
        }
        public String getSuffix() {
            return suffix;
        }
        public void setSuffix(String suffix) {
            this.suffix = suffix;
        }
    }
    

    创建业务类读取Properties文件中的值

    public class HelloService {
        @Auto开发者_JAVA学习wired
        HelloProperties helloProperties;
         public String sayHello(String userName){
            return helloProperties.getPrefix() + ":" + userName + ">" + helloProperties.getSuffix();
         }
    }
    

    再个自动配置类,自动进行类加载

    @Configuration
    @ConditionalOnMissingBean(HelloService.class)
    @EnableConfigurationProperties(HelloProperties.class)
    public class HelloServiceAutoConfiguration {
        @Bean
        public HelloService helloService(){
            HelloService helloService = new HelloService();
            return helloService;
        }
    }
    

    最终的效果如下:

    SpringBoot定制化Starter实现方法

    4.在resources目录下创建META-INF/spring.factories添加如下配置信息

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

    com.zhao.hello.auto.HelloServiceAutoConfiguration

    SpringBoot定制化Starter实现方法

    5.装hello-spring-boot-starter-autoconfigure模块和xiaozhao-hello-spring-boot-starter

    SpringBoot定制化Starter实现方法

    SpringBoot定制化Starter实现方法

    6.装完成后,创建新的项目来引入创建好的starter.新的的项目名为hello-test的Spring Boot初始化项目。

    SpringBoot定制化Starter实现方法

    引入我们之前定义的Starter启动器和Spring Boot自带的Web启动器

    <dependencies>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
    javascript      <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
       </dependency>
       <dependency>
          <groupId>com.zhao</groupId>
          <artifactId>xiaozhao-hello-spring-boot-starter</artifactId>
          <version>1.0-SNAPSHOT</version>
       </dependency>
    </dependencies>
    

    SpringBoot定制化Starter实现方法

    7.在hellopython-test项目中创建测试Controller

    @RestController
    public class HelloController {
        @Autowired
        HelloService helloService;
        @GetMapping("/hello")
        public String sayHello() {
            String str = helloService.sayHello("李四");
            return str;
        }
    }
    

    编写配置文件

    xiaozhao.hello.prefix=hello

    xiaozhao.hello.suffix=666

    SpringBoot定制化Starter实现方法

    8.启动项目,测试一下

    SpringBoot定制化Starter实现方法

    总结自定义Starter的实现逻辑

    • 首先引入自定义的Starter:xiaozhao-hello-spring-boot-starter,在这个Starter中引入自定义的自动配置场景
    • 当自动配置场景启动时,会去寻找spring.factories文件,去自动加载HelloServiceAutoConfiguration类文件
    • 加载完自动配置类后,@ConditionalOnMissingBean(HelloService.class)通过这个注解,当容器中没有HelloService时,去自动添加一个HelloService组件。
    • HelloService组件的所有属性,通过HelloProperties配置文件进行绑定的,@ConfigurationProperties("xiaozhao.hello"),通过xiaozhao.hello。xxx进行绑定。
    • 如果自己在容器中注入一个HelloService组件,使用的就不是自动配置的,而是重新注入的。

    到此这篇关于SpringBoot定制化Starter实现方法的文章就介绍到这了,更多相关SpringBoot Starter内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    关注公众号