游戏平台网站建设,木马文创的工业设计,成都建设银行保安招聘网站,关于企业的网站目录 引言1. Spring Boot Starter机制1.1 什么是Spring Boot Starter1.2 为什么要使用Spring Boot Starter1.3.应用场景1.4.自动加载核心注解说明 2. 综合案例配置类制作控制功能实现 总结 引言
在当今互联网时代#xff0c;构建高性能、可维护的应用已成为开发者的首要任务。… 目录 引言1. Spring Boot Starter机制1.1 什么是Spring Boot Starter1.2 为什么要使用Spring Boot Starter1.3.应用场景1.4.自动加载核心注解说明 2. 综合案例配置类制作控制功能实现 总结 引言
在当今互联网时代构建高性能、可维护的应用已成为开发者的首要任务。Spring Boot Starter机制作为Spring Boot框架的一项强大功能为开发者提供了简化配置、快速搭建项目的便捷工具。本文将深度解析Spring Boot Starter机制揭示其背后的原理同时通过综合案例展示其实际应用。
1. Spring Boot Starter机制
1.1 什么是Spring Boot Starter
SpringBoot中的starter是一种非常重要的机制(自动化配置)能够抛弃以前繁杂的配置将其统一集成进starter应用者只需要在maven中引入starter依赖SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。starter让我们摆脱了各种依赖库的处理需要配置各种信息的困扰。
SpringBoot会自动通过classpath路径下的类发现需要的Bean并注册进IOC容器。SpringBoot提供了针对日常企业应用研发各种场景的spring-boot-starter依赖模块。
所有这些依赖模块都遵循着约定成俗的默认配置并允许我们调整这些配置即遵循“约定大于配置”的理念。
1.2 为什么要使用Spring Boot Starter
在我们的日常开发工作中经常会有一些独立于业务之外的配置模块我们经常将其放到一个特定的包下然后如果另一个工程需要复用这块功能的时候需要将代码硬拷贝到另一个工程重新集成一遍麻烦至极。如果我们将这些可独立于业务代码之外的功能配置模块封装成一个个starter复用的时候只需要将其在pom中引用依赖即可 SpringBoot为我们完成自动装配简直不要太爽。
对比分析Starter与传统配置的优势 通过实际案例对比分析使用Spring Boot Starter相对于传统配置方式的便捷性、可维护性等方面的优势。帮助开发者理解在何种场景下选择使用Spring Boot Starter更为合适。
1.3.应用场景
在我们的日常开发工作中可能会需要开发一个通用模块以供其它工程复用。SpringBoot就为我们提供这样的功能机制我们可以把我们的通用模块封装成一个个starter这样其它工程复用的时候只需要在pom中引用依赖即可由SpringBoot为我们完成自动装配。
常见应用场景
1通用模块-短信发送模块
2基于AOP技术实现日志切面
3分布式雪花IDLong转String解决精度问题
4微服务项目的数据库连接池配置
5微服务项目的每个模块都要访问redis数据库每个模块都要配置redisTemplate
1.4.自动加载核心注解说明 2. 综合案例
配置类制作
yml
sms:key: 1001secret: 1002pom.xml
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdoptionaltrue/optional
/dependencyspring-boot-configuration-processor 是一个注解处理器用于处理 Spring Boot 配置类的注解并生成配置属性的元数据。它在开发过程中起到以下几个重要的作用
生成配置属性的元数据: 当你使用 ConfigurationProperties 注解来声明配置类时spring-boot-configuration-processor 会解析该注解并生成与配置属性相关的元数据。这些元数据包括属性的名称、类型、描述、默认值等信息。这些信息可以帮助 IDE 在开发过程中提供代码提示、自动补全和验证功能。提供配置属性的编译时验证: 使用 ConfigurationProperties 注解时你可以使用其他注解如 Value、Valid 等来描述配置属性的约束条件。spring-boot-configuration-processor 可以处理这些注解并在编译时进行验证。这样你可以在开发阶段及早发现配置属性的错误或不一致而不是在运行时才遇到问题。简化配置类的编写: 通过使用 spring-boot-configuration-processor你可以更轻松地编写配置类。它会自动处理 ConfigurationProperties 注解及其相关注解生成属性的 getter、setter 方法并提供默认的配置文件映射规则。这样你可以专注于定义配置属性的结构和业务逻辑而不需要手动编写重复的代码。提升开发体验: spring-boot-configuration-processor 增强了开发工具的功能例如在 IDE 中提供配置属性的智能提示、文档、类型检查等功能。这可以减少开发过程中的错误并提高代码的可读性和可维护性。
package com.yuan.smsspringbootstart;import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** author 叶秋* site* company 卓京公司* create 2023-12-15 14:03*/
DataConfigurationProperties(prefix sms)
public class SmsProperties {//应用标识private String key;//应用密钥private String secret;
}
package com.yuan.smsspringbootstart;import com.yuan.smsspringbootstart.service.ISmsService;
import com.yuan.smsspringbootstart.service.SmsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;/*** author 叶秋* site* company 卓京公司* create 2023-12-15 14:28*/
Configuration
EnableConfigurationProperties({SmsProperties.class})
public class SmsConfig {Autowiredprivate SmsProperties smsProperties;Beanpublic ISmsService smsService(){return new SmsServiceImpl(smsProperties);}}
package com.yuan.smsspringbootstart.service;public interface ISmsService {/*** 发送短信** param phone 要发送的手机号* param data 要发送的内容*/void send(String phone, String data);}
package com.yuan.smsspringbootstart.service;import com.yuan.smsspringbootstart.SmsProperties;
import org.springframework.stereotype.Service;public class SmsServiceImpl implements ISmsService {private SmsProperties smsProperties; //nullpublic SmsServiceImpl(SmsProperties smsProperties) {this.smsPropertiessmsProperties;}Overridepublic void send(String phone, String data) {String key smsProperties.getKey();String secret smsProperties.getSecret();System.out.println(接入短信系统Key key ,Secret secret);System.out.println(短信发送phone phone data data);}}
控制功能实现
在新的项目中加入配置 pom.xml
dependencygroupIdcom.yuan/groupIdartifactIdsms-spring-boot-start/artifactIdversion0.0.1-SNAPSHOT/version/dependencyyml
sms:key: 1001secret: 1002enable: truepackage com.yuan.smsspringbootstart;import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** author 叶秋* site* company 卓京公司* create 2023-12-15 14:03*/
Data
ConfigurationProperties(prefix sms)
public class SmsProperties {//应用标识private String key;//应用密钥private String secret;private String enable;
}
package com.yuan.smsspringbootstart;import com.yuan.smsspringbootstart.service.ISmsService;
import com.yuan.smsspringbootstart.service.SmsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;/*** author 叶秋* site* company 卓京公司* create 2023-12-15 14:28*/
Configuration
EnableConfigurationProperties({SmsProperties.class})
ConditionalOnProperty(prefix sms,name enable,havingValue true)
public class SmsConfig {Autowiredprivate SmsProperties smsProperties;Beanpublic ISmsService smsService(){return new SmsServiceImpl(smsProperties);}}
org.springframework.boot.autoconfigure.EnableAutoConfigurationcom.yuan.smsspringbootstart.SmsConfig测试效果 总结
Spring Boot Starter机制作为Spring Boot框架的一项核心功能为开发者提供了更便捷、高效的开发体验。通过深入解析Starter的结构、优势以及自动加载机制本文旨在帮助开发者更好地理解和应用Spring Boot Starter从而构建出更为强大、可维护的应用。希望读者在阅读完本文后能够对Spring Boot Starter有更深入的认识并在实际项目中充分发挥其优势。