【定制化体验:使用Spring Boot自动配置,打造个性化Starter】

ops/2024/10/24 1:37:06/

项目结构

在这里插入图片描述

Pom

<?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.custom</groupId><artifactId>spring-boot-starter-custom</artifactId><version>0.1-SNAPSHOT</version><packaging>jar</packaging><description>封装一个简单User组件</description><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>2.5.8</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>2.5.8</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><version>2.5.8</version></dependency></dependencies>
</project>

spring.factories 注意书写格式

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.custom.config.UserAutoConfiguration

UserService (最后注入成bean)

java">public class UserService {private String name;private String age;private Long id;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public void pint() {System.out.println(String.format("获取的数据用户名:%s----密码:%s----id:%s", name, age, id));}
}

UserProperties (用户配置)

java">@ConfigurationProperties(prefix = "song.user")
public class UserProperties {private String start;private String name;private String age;private Long id;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getStart() {return start;}public void setStart(String start) {this.start = start;}
}

最关键的配置 自动配置类 一定要看注释


java">/*** 功能描述: 自动配置类** @author Songxianyang* @date 2024-04-26 13:40*/
@Configuration
// yaml档中song.user.start 有没有  如果没有则无法自动配置改类
@ConditionalOnProperty(prefix = "song.user", name = {"start"})
// 把yml配置的属性注入进来 然后使用它。启动自动配置属性
@EnableConfigurationProperties(UserProperties.class)
public class UserAutoConfiguration {@Beanpublic UserService userService(UserProperties userProperties) {UserService userService = new UserService();userService.setId(userProperties.getId());userService.setName(userProperties.getName());userService.setAge(userProperties.getAge());return userService;}
}

如何使用自己封装的starter

随便找一个spring boot 项目 然后再pom中引入自己的starter

这里以我自己kkxx-poi 项目为例子
在这里插入图片描述

再写一个web控制层调用一下

在这里插入图片描述


关键配置yml (application-local.yml)

在这里插入图片描述

测试 读取到具体的配置拉

在这里插入图片描述

知识点汇总

  1. 自动配置原理
  2. Spring Boot环境选择
  3. yml配置的类
  4. 条件注解
  5. 开启关闭组件逻辑

代码分享

starter组件

调用组件的类


http://www.ppmy.cn/ops/24884.html

相关文章

Dokcer容器分布式搭建LNMP+wordpress论坛

目录 引言 一、架构环境 二、搭建容器 &#xff08;一&#xff09;自定义网络 &#xff08;二&#xff09;搭建nginx容器 1.文件准备 2.查看与编辑文件 3.生成镜像 4.创建容器 &#xff08;三&#xff09;搭建MySQL容器 1.文件准备 2.查看与编辑文件 3.生成镜像 …

域名系统与域名解析服务器DNS

IPv4根域名服务器 IPv4根服务器全球只有13台&#xff08;这13台IPv4根域名服务器名字分别为“A”至“M”&#xff09;&#xff0c;1个为主根服务器在美国&#xff0c;由美国互联网机构Network Solutions运作。其余12个均为辅根服务器&#xff0c;其中9个在美国&#xff0c;2个…

docker菜鸟教程

Docker是一个开源的应用容器引擎&#xff0c;它允许开发者将应用及其依赖打包到一个可移植的容器中&#xff0c;然后发布到任何Linux机器上。以下是Docker的一些基本概念和操作指南&#xff1a; 镜像(Image)&#xff1a;Docker镜像是一个文件系统&#xff0c;它包含了应用程序及…

leetCode61. 旋转链表

leetCode61. 旋转链表 题目思路&#xff1a;见如图所示 代码展示 /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* Li…

行列式求解

行列式 给出一个矩阵求 行列式。 输入&#xff1a; 1 3 1 -2 -1 0 3 2 3 1 -1 思路&#xff1a; 不能直接乘上上面行的倍数来消除本行对应元素。试试辗转相减法把。 (1,3)减去2倍(0,1)->(1,0) (5,3)减去0倍(3,5)减去1倍(2,3)减去1倍(1,2)减去2倍(0,1)->(1,0) 然…

Python3.11修改并运行oneforall

遇到的问题 使用python3.11默认无法运行oneforall脚本&#xff0c;出现如下报错 # 解决方案 修改 /usr/local/lib/python3.11/dist-packages/exrex.py exrex.py具体文件路径报错中会显示 vim /usr/local/lib/python3.11/dist-packages/exrex.py# 修改前 from re import sre…

前端工程化Vue使用Node.js永久设置国内高速npm镜像源

前端工程化Vue使用Node.js永久设置国内高速npm镜像源 接续上篇错误收录&#xff0c;此篇通过简单配置永久设置国内高速npm镜像源方法 1.更换新版镜像 清空npm缓存 npm cache clean --force修改回原版镜像源或直接删除配置过的镜像源 npm config set registry https://registr…

游戏视频录制软件有哪些?这3款推荐给你

在数字化时代的今天&#xff0c;游戏视频录制已经成为广大游戏爱好者和职业玩家的必需品&#xff0c;那么游戏视频录制软件有哪些&#xff1f;哪个更适合您呢&#xff1f; 本文将为您推荐3款游戏视频录制软件&#xff0c;帮助您记录下游戏中的精彩瞬间&#xff0c;也可以通过录…