cad如何自定义填充案,springboot定义拦截器
1.导读相信很多小伙伴已经对SpringBoot技术很熟悉了。大大简化了Spring应用的开发,大大提高了项目的开发效率,受到开发者和企业的青睐。特别是SpringBoot官方为各种应用场景提供了非常丰富的场景启动器(也叫启动依赖)。开发者只需要在项目的POM文件中导入相应的场景依赖,编写少量的配置,就可以快速实现当前场景的应用开发。真正的实现是开箱即用的。
今天,壹哥将通过这篇文章和一个具体的案例向您介绍如何定制我们自己的SpringBoot场景启动器。毕竟有时候官方首发并不能完全满足我们所有的需求。同时,壹哥也希望通过starter的定制过程,加深对SpringBoot自动配置原理的理解。
二。需求描述我们先来看一段代码。
包com . qf . hello . service;导入com . qf . hello . bean . hello properties;导入org . spring framework . beans . factory . annotation . auto wired;公共类hello service { Autowired hello properties hello properties;公共字符串say hello(String name){ return hello properties . get prefix(): name hello properties . get suffix();}}上面我们定义了一个组件,HelloService,它有一个很简单的功能,就是可以根据调用者传递的名字返回一个问候消息。可以根据配置的前缀和后缀,以指定的格式设置返回消息的内容。现在我们需要把这个功能做成启动器,可以直接导入,以后在其他项目中作为场景启动器使用。
三。设计思路。回顾一下我们之前对已完成的启动器的使用,你会发现无非是以下几个步骤。
在POM文件中导入场景依赖
该场景依赖项包含一个名为xxxAutoConfiguration的自动配置类。
自动配置类根据一定条件自动组装相关组件。
反过来,这些组件被绑定到一个名为xxxProperties属性配置类。
属性类通过指定的前缀从application.yml配置文件中读取属性的配置信息。
最后,在项目中直接使用这些配置的组件。
让我们参考这个步骤来启动自定义启动器操作。
四。实施步骤1。步骤1业务定义创建一个包含两个模块的空项目[customer-starter]。
起动机模块[hello-spring-boot-starter]
自动配置模块[hello-spring-boot-starter-配置]
launcher项目中不需要任何源代码和配置文件,只需要引入自动配置项目的依赖关系。
?xml版编码UTF-8?项目xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4 . 0 . 0 http://maven.apache.org/xsd/maven-4.0.0.xsd模型版本4 . 0 . 0/模型版本groupIdcom.qf/groupId artifact id hello-spring-boot-starter/artifactId打包POM/打包版本1.0-快照/版本依赖项依赖项groupIdcom.qf/groupId artifact id hello-spring-boot-starter-配置/artifact id版本1.0-快照/版本/依赖项/dep
endencies></project>
自动配置项目必须是一个SpringBoot工程同时需要引入spring-boot-starter的依赖。
<?xml version1.0 encodingUTF-8?><project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://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.4.5</version> </parent> <groupId>com.qf</groupId> <artifactId>hello-spring-boot-starter-configuration</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies></project> 2. Step2 自动配置
编写自动配置项目中的内容。
HelloService是整个自定义Starter要装配的核心对象HelloServiceAutoConfiguration是一个配置类HelloProperties是HelloService组件绑定的属性配置类他们的代码分别如下
2.1 HelloService类 //HelloService:该组件不要默认注册到容器中而是通过一个自动配置类按条件进行装配package com.qf.hello.service;import com.qf.hello.bean.HelloProperties;import org.springframework.beans.factory.annotation.Autowired;public class HelloService { Autowired HelloProperties helloProperties; public String sayHello(String name){ return helloProperties.getPrefix() : name >>> helloProperties.getSuffix(); }} 2.2 HelloProperties类 //HelloProperties自配配置属性类package com.qf.hello.bean;import org.springframework.boot.context.properties.ConfigurationProperties;ConfigurationProperties(prefix hello)public class HelloProperties { //sayHello方法使用的前缀信息 private String prefix; //sayHello方法使用的后缀信息 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; }} 2.3 HelloServiceAutoConfiguration类 //自动配置类package com.qf.hello.auto;import com.qf.hello.bean.HelloProperties;import com.qf.hello.service.HelloService;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;ConfigurationEnableConfigurationProperties(HelloProperties.class)public class HelloServiceAutoConfiguration { ConditionalOnMissingBean(HelloService.class) Bean public HelloService helloService(){ System.out.println(使用自定义starter提供的HelloService); return new HelloService(); }} 3. Step3 工厂文件
在自动配置项目中的resources目录中提供一个名称为META-INF的目录并在该目录下提供一个名为spring.factories的文件。
resources/META-INF/spring.factories
spring.factories配置的内容如下
org.springframework.boot.autoconfigure.EnableAutoConfiguration\com.qf.hello.auto.HelloServiceAutoConfiguration
4. Step4 安装
将这两个项目clean并install到本地仓库。
5. Step5 引入使用
创建一个web项目进行自定义starter的使用测试。
5.1 在应用中添加自定义starter依赖坐标 <!-- 1.引入我们自定义的场景启动器 --><dependency> <groupId>com.qf</groupId> <artifactId>hello-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version></dependency> 5.2 编写配置信息 server: port: 8080hello: prefix: 千锋 suffix: 888 5.3 编写测试的Controller
并在该Controller中自动注入自定义场景启动器中的HelloService组件。
package com.qf.boot.controller;import com.qf.hello.service.HelloService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;RestControllerpublic class HelloController { Autowired HelloService helloService; GetMapping(/hello/{name}) public String hello(PathVariable(name) String name){ return helloService.sayHello(name); }} 5.4 打开浏览器输入Controller中定义的访问地址
通过测试发现我们已经可以在其他项目中使用自定义的starter并使用自动配置好的组件功能了现在你知道该怎么自定义starter了吗如果你还有其他问题可以在评论区留言哦。
相关文章
- 如何创建springboot项目[通俗易懂]
- 面试 – 如何编写一个SpringBoot-Starter?
- 面试突击82:SpringBoot 中如何操作事务?
- springboot面试大全
- 手把手教你如何编写springboot中starter
- SpringBoot整合Redis
- SpringBoot(一) - SpringBoot 初识
- SpringBoot中如何加载远程配置
- SpringBoot快速搭建、自动流程进阶、装配机制、功能扩展点详解
- 如何将Thymeleaf技术集成到SpringBoot项目中
- 聊聊springboot项目如何优雅的修改或者填充请求参数
- springboot多环境配置文件_Springboot教程
- springboot jdbc template_Springboot教程
- 注解式elasticsearch+SpringBoot(附分布式配置)
- 基于SSM+SpringBoot+Vue+ElmentUI实现公司案件管理系统
- 秒懂如何使用SpringBoot+Junit4进行单元测试
- SpringBoot:模块探究之spring-boot-cli
- springboot 之集成kafka
- 图解 K8S 中 SpringBoot Pod 如何优雅关闭,减少对客户端影响
- git clone 一个 SpringBoot 项目,如何在本地运行?
- springboot 项目通过过滤器(Filter)实现白名单过滤以及拦截客户端请求服务接口进行统一日志记录的实现方式
- 教你如何处理springboot 没法扫描到repository的解决方法
- SpringBoot(一)走进Springboot的世界详解编程语言