zoukankan      html  css  js  c++  java
  • Spring Boot 8. 自定义starters

    自定义starters

    starters 原理、自定义 starters
    代码地址
    ssh git@gitee.com:Ding_DangMao/learn-spring-bot.git

    starte:

    1. 这个场景需要使用到的依赖是什么?
    2. 如何编写子自动配置
      @Configuration 指定这个类是一个配置类
      @ConditionalOnXxx 在指定天降成立的情况下自动配置类生效
      @AutoConfigureAfter 指定自动配置的顺序
      @Bean 给容器中添加组件
      
      @ConfigurationPropertie 结合相关XxxProperties生效加入到容器中
      @EnableConfigutrationProperties 让 XxxPropertes生效加入到容器中
      
      自动配置类要加载
      将需要启动就要加载的自动配置类,配置在 META-INF/spring.factories
      org.springframework.context.ApplicationContextInitializer=\
      com.cainiao100.springboot.listener.HelloApplicationContextInitializer
      org.springframework.boot.SpringApplicationRunListener=\
      com.cainiao100.springboot.listener.HelloSpringApplicationRunListener
      
    3. 模式
      启动器只用来做依赖导入
      专门写一个自动配置模块
      启动器依赖自动配置,别人只需要引入启动器(starter)
      mybatis-spring-boot-starter,自定义启动器名,spring-boot-starter

    一、自定义starters

    image

    
        <groupId>com.cainiao</groupId>
        <artifactId>spring-boot-08-autoconfiguer</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
        <parent>
            <artifactId>spring-boot-starter-parent</artifactId>
            <groupId>org.springframework.boot</groupId>
            <version>2.2.1.RELEASE</version>
        </parent>
        <dependencies>
            <!--引入web模块 spring-boot-starter :springboot场景启动器,帮我们导入了web模块正常运行所依赖的 jar包-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    

    自动装配Bean

    • 自动装配使用配置类(@Configuration)结合Spring4 提供的条件判断注解@Conditional及Spring Boot的派生注解如@ConditionOnClass完成;
      配置自动装配Bean;
    • 将标注@Configuration的自动配置类,放在classpath下META-INF/spring.factories文件中,如:
      image

    自动装配顺序

    • 在特定自动装配Class之前
      @AutoConfigureBefore
    • 在特定自动装配Class之后
      @AutoConfigureAfter
    • 指定顺序
      @AutoConfigureOrder
    @ConfigurationProperties(prefix = "sunflower.hello")
    public class HelloProperties {
        private String prefix;
        private String suffix;
    	省略set get
    }
    -------------------------------
    public class HelloService {
    
        private HelloProperties helloProperties;
    
        public String sayHello(String name){
                return helloProperties.getPrefix() + name + helloProperties.getSuffix();
        }
    	省略 set get
    }
    -------------------------------
    @Configuration //当前类是一个组件
    @ConditionalOnWebApplication //web应用才生效
    @EnableConfigurationProperties(HelloProperties.class) //把属性文件生效
    public class HelloServiceAutoConfiguration {
    
        @Autowired
        HelloProperties helloProperties;
    
        @Bean
        public HelloService helloService() {
            HelloService helloService = new HelloService();
            //设置属性文件
            helloService.setHelloProperties(helloProperties);
            return helloService;
        }
    }
    
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.cainiao.starter.HelloServiceAutoConfiguration
    

    二、启动器(starter)

    image

    • 启动器模块是一个空JAR 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库
    • 命名规约:
      • 推荐使用以下命名规约;
      • 官方命名空间
        • 前缀:“spring-boot-starter-”
        • 模式:spring-boot-starter-模块名
        • 举例:spring-boot-starter-web、spring-boot-starter-actuator、spring-boot-starter-jdbc
      • 自定义命名空间
        • 后缀:“-spring-boot-starter”
        • 模式:模块-spring-boot-starter
        • 举例:mybatis-spring-boot-starter

    自定义的starter依赖 autoconfiguer

        <groupId>com.cainiao</groupId>
        <artifactId>spring-boot-08-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <!--启动器-->
        <dependencies>
            <!--引入自动配置模块-->
            <dependency>
                <groupId>com.cainiao</groupId>
                <artifactId>spring-boot-08-autoconfiguer</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    

    测试

    • 重新创建一个 springboot项目
      引入自定义的starter的坐标在项目
        <groupId>com.cainiao</groupId>
        <artifactId>spring-boot-08-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
    
    @RestController
    public class HelloController {
        @Autowired
        HelloService helloService;
    
        @GetMapping("/hello")
        public String hello() {
            return helloService.sayHello("didi");
        }
    }
    
    sunflower.hello.prefix=BB
    sunflower.hello.sufix=EE
    

    image

    spring-boot基础完结!!!

  • 相关阅读:
    获奖2008年金蝶集团优秀员工感言
    对如何建设一个可运维高可靠的SAAS平台,我终于有了想法!!!
    javaeye站点被ARP攻击有感
    回想过去几年软件业的荒唐事
    也说一种普遍错误使用的LOG方式
    看看国外的本科生能做什么?(Robert Love)
    zz乱码、编码问题详
    查找linux内核支持的usb摄像头列表(UVC万能驱动)
    Greg KroahHartman LDD3 作者,LKN作者,linux driver 开发者,新闻两则,因为过时了所以就放我这个垃圾博客里吧
    zz QEMU 与 Bochs
  • 原文地址:https://www.cnblogs.com/zk2020/p/15551842.html
Copyright © 2011-2022 走看看