springboot自定义启动器

一、springboot启动器主要组成部分

1.启动器模块

用mybatis的启动器举例
工程中只有一个pom.xml文件,用于引入需要依赖的jar包,其中包括mybatis自动配置jar包,mybatis-spring-boot-autoconfigure
在这里插入图片描述

2.自动配置模块

用mybatis的启动器举例
实现自动配置,需要在META-INF文件夹下创建spring.factories文件,指定实现自动配置的类
在这里插入图片描述
点开具体配置类,可以看到mybatis通过@Bean的方式,将bean加入spring容器中
在这里插入图片描述

3. 注解
@Configuration  //指定这个类是一个配置类
@ConditionalOnXXX  //在指定条件成立的情况下自动配置类生效
@Bean  //给容器中添加组件
@ConfigurationProperties //结合相关properties配置文件获取相关配置
@EnableConfigurationProperties //让@ConfigurationProperties生效加入到容器中

二、创建自定义启动器

1.创建springboot工程,负责对公用功能的封装

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.spring.fisher</groupId>
    <artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>custom-spring-boot-starter-autoconfigurer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--启动器必备-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

</project>

2. 创建拦截器CustomHandlerInterceptor
  • 使用@ConfigurationProperties注解,后面配合@EnableConfigurationProperties注解将CustomHandlerInterceptor这个类注入到spring容器中,能够通过@Autowired或@Resource直接使用这个bean

在这里插入图片描述

package com.spring.fisher.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@ConfigurationProperties
public class CustomHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("1 preHandle请求前调用");
        //返回false则请求中断
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("1 postHandle请求后调用");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("1 afterCompletion请求调用完成后回调方法,即在视图渲染完成后回调");
    }
}

3. 添加拦截器MyWebMvcConfigurer

在这里插入图片描述

package com.spring.fisher.config;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class MyWebMvcConfigurer implements WebMvcConfigurer {

    private CustomHandlerInterceptor handlerInterceptor;

    public MyWebMvcConfigurer(CustomHandlerInterceptor handlerInterceptor) {
        this.handlerInterceptor = handlerInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(handlerInterceptor).addPathPatterns("/*");
    }
}

4. 创建配置类ServerConfig
  • @ConfigurationProperties(prefix = “fisher.server”),通过此注解可以获取项目中application.properties中以fisher.server为前缀的属性值

在这里插入图片描述

5. 创建template类

在这里插入图片描述

package com.spring.fisher.start;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FisherTemplate {

    private ServerConfig serverConfig;

    public FisherTemplate(ServerConfig serverConfig) {
        this.serverConfig = serverConfig;
    }

    public String helloWorld(){
        log.info("开启自定义启动器: host={}, port={}",serverConfig.getHost(),serverConfig.getPort());
        return "helloWorld";
    }

}

6. 创建CustomStarterRun类,将template注入到spring容器中

在这里插入图片描述

package com.spring.fisher.start;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@ConditionalOnClass(FisherTemplate.class)
@EnableConfigurationProperties(ServerConfig.class)
public class CustomStarterRun {

    @Resource
    private ServerConfig serverConfig;

    @Bean
    public FisherTemplate fisherTemplate(){
        FisherTemplate fisherTemplate = new FisherTemplate(serverConfig);
        return fisherTemplate;
    }

}

7. 创建WebMvcConfigStarterRun类,将拦截器注入到spring容器中

在这里插入图片描述

package com.spring.fisher.start;

import com.spring.fisher.config.CustomHandlerInterceptor;
import com.spring.fisher.config.MyWebMvcConfigurer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(MyWebMvcConfigurer.class)
@EnableConfigurationProperties(CustomHandlerInterceptor.class)
public class WebMvcConfigStarterRun {

    @Resource
    private CustomHandlerInterceptor customHandlerInterceptor;

    @Bean
    //当配置文件中fisher.webmvcconfigurer.enabled设置为true,此方法才能失效,默认是true
    @ConditionalOnProperty(name = "fisher.webmvcconfigurer.enabled",havingValue = "true")
    public MyWebMvcConfigurer myWebMvcConfigurer(){
        return new MyWebMvcConfigurer(customHandlerInterceptor);
    }

}

8. 创建spring.factories文件
  • 用于指定配置类
    在这里插入图片描述
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.spring.fisher.start.CustomStarterRun,\
com.spring.fisher.start.WebMvcConfigStarterRun
9. 创建spring-configuration-metadata.json
  • 方便在配置文件中提示配置的key

在这里插入图片描述

{
  "properties": [
    {
      "name": "fisher.server.host",
      "type": "java.lang.String",
      "description": "服务器地址",
      "defaultValue": "localhost"
    },{
      "name": "fisher.server.port",
      "type": "java.lang.Integer",
      "description": "服务器的端口",
      "defaultValue": 8080
    },{
      "name": "fisher.webmvcconfigurer.enabled",
      "type": "java.lang.Boolean",
      "description": "是否开启拦截器",
      "defaultValue": true
    }
  ]
}
10. 打jar包

在这里插入图片描述

11. 将jar放入本地仓库中,并将pom.xml另存为一个与jar同名的.pom文件,用于引入依赖包

在这里插入图片描述

12. 创建一个空的maven工程,负责导入公共功能的工程

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

13. 修改pom.xml
  • 只引入custom-spring-boot-starter-autoconfigurer一个jar

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.spring.fisher</groupId>
    <artifactId>fisherTemplate-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>fisherTemplate-spring-boot-starter</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.spring.fisher</groupId>
            <artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

14. 打jar包

在这里插入图片描述

15. 将jar包与.pom文件放入本地仓库

在这里插入图片描述

16. 打开一个springboot项目,引入刚创建的fisherTemplate-spring-boot-starter包
  • 自动添加custom-spring-boot-starter-autoconfigurer包

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.test</groupId>
    <artifactId>springtest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springtest</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.spring.fisher</groupId>
            <artifactId>fisherTemplate-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
        </resources>


    </build>

</project>

17. application.properties添加参数

在这里插入图片描述

fisher.server.host=192.168.0.1
fisher.server.port=80
fisher.webmvcconfigurer.enabled=true
  • 当输入fisher.时会自动提示

在这里插入图片描述

18. controller注入FisherTemplate,调用helloworld方法

在这里插入图片描述

19. 浏览器发起请求,查看打印

在这里插入图片描述

  • host与port只能引用本项目的值,并开启了拦截器功能

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值