zoukankan      html  css  js  c++  java
  • SpringBoot学习笔记(四)

    本文主要介绍:SpringBoot开发中如何自定义starter

    1.什么是starter

    Starter可以理解为一个可拔插式的插件,提供一系列便利的依赖描述符,您可以获得所需的所有Spring和相关技术的一站式服务。应用程序只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。用一句话描述,就是springboot的场景启动器。

    下面是Spring官方提供的部分starter,全部的请参考官网:starter文档

    image

    开始自定义starter前看看Springboot的AutoConfiguration机制。

    2.Spring boot的AutoConfiguration机制

    标记一个应用为Springboot应用,需要一个SpringBootApplication注解,下面是一个标准的spring boot启动程序。

    image

    点进去看源码发现,SpringBootApplication注解又被一个EnableAutoConfiguration注解。EnableAutoConfiguration注解就是自动加载配置的关键。

    image

    EnableAutoConfiguration是一个组合注解,用Import把AutoConfigurationImportSelector导入容器中,springboot启动的时候会加载所有的selector并执行selectImports方法,这个方法会加载META-INF/spring.factories中配置的EnableAutoConfiguration,从而加载自动配置。

    image

    3.自定义starter

    首先:编写启动器

    1.在IDEA中新建一个空项目:mystarter

    image

    2、新建一个Springboot模块:zhou-spring-boot-starter-autoconfigure

    image

    3.新建一个Springboot模块:my-starter-test

    image

    4.基本结构如下:

    image

    第二步:编写模块:zhou-spring-boot-starter-autoconfigure

    1.导入pom依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>2.4.3</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.5.0</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-test</artifactId>
                <version>2.4.3</version>
            </dependency>
        </dependencies>
    

    2.定义配置类:HelloProperties类,添加注解@ConfigurationProperties(prefix = "zhou.hello")

    image

    package com.zhou;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    //编写HelloProperties 配置类
    //前缀 zhou.hello
    @ConfigurationProperties(prefix = "zhou.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;
        }
    }
    

    3.创建一个自己的服务类:HelloService类

    image

    package com.zhou;
    //编写一个自己的服务
    public class HelloService {
        private HelloProperties helloProperties;
    
    
        public HelloService(HelloProperties helloProperties) {
            this.helloProperties = helloProperties;
        }
    
        public String sayHello(String name){
            return helloProperties.getPrefix()+name+helloProperties.getSuffix();
        }
    }
    

    4.创建Configuration类:HelloServiceAutoConfiguration类,添加注解@Configuration@EnableConfigurationProperties,把当前类设置成配置类,并且注入HelloProperties。

    image

    package com.zhou;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    //编写我们的自动配置类并注入bean
    @Configuration
    @EnableConfigurationProperties(HelloProperties.class)
    public class HelloServiceAutoConfiguration {
        @Autowired
        HelloProperties helloProperties;
        @Bean
        @ConditionalOnMissingBean(HelloService.class)
        public HelloService helloService(){
    
            return new HelloService(helloProperties);
        }
    }
    

    5.重要的一步,在resources目录下创建META-INF目录,并添加文件spring.factories。在这个文件中配置EnableAutoConfiguration,具体如下:

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=
    com.zhou.HelloServiceAutoConfiguration
    

    6.避免端口冲突(不想干掉Tomcat端口号)

    image

    7.编写完成后,可以安装到maven仓库中!

    image

    模块:zhou-spring-boot-starter-autoconfigu的整体结构如下:

    image

    第三步:模块:my-starter-test(测试自定义的Starter)

    1.pom文件添加刚刚创建好的依赖:

    <dependency>
    	<groupId>com.zhou</groupId>
    		<artifactId>zhou-spring-boot-starter-autoconfigure</artifactId>
    		<version>0.0.1-SNAPSHOT</version>
    </dependency>
    

    2.在application.properties文件中添加自定义前缀/后缀:

    zhou.hello.prefix="你好呀,"
    zhou.hello.suffix="成了~"
    

    【注意】.properties文件会出现中文会乱码问题。

    第一种解决办法:

    image

    第二种解决办法:将application.properties文件替换成application.yaml

    zhou.hello.prefix: 你好呀,
    zhou.hello.suffix: 成了~
    

    image

    3.创建一个Controller,把HelloService注入进来

    image

    package com.zhou.controller;
    
    import com.zhou.HelloService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/say")
    public class MyController {
        @Autowired
        private HelloService helloService;
        @GetMapping("/hello")
        public String get(){
            return helloService.sayHello("这是我自定义的starter,嘻嘻");
        }
    }
    

    4.启动,测试

    Run:

    image

    访问:http://localhost:8081/say/hello

    image

    个人学习笔记,针对本人在自学中遇到的问题。

  • 相关阅读:
    C++ 11 :override 关键字的使用
    vsphere部署说明
    Windows 下部署Subversion
    Centos 7 最小化部署zabbix
    在vmware中安装mac os
    如何将OS dmg文件制作成U盘启动
    HDU1518(dfs)java/ c++
    HDU1010(bfs)
    HDU1495(bfs)
    HD1285(拓扑排序)
  • 原文地址:https://www.cnblogs.com/1693977889zz/p/14561083.html
Copyright © 2011-2022 走看看