基于SpringBoot的策略模式多实现类注入(Map注入)

Map+函数式接口代替策略模式 最近写了一个服务:根据优惠券的类型resourceType和编码resourceId来 查询 发放方式。 实现方式: 根据优惠券类型resourceType -> 确定查询哪个数据表 根据编码resourceId -> 查询派发方式grantType 优惠券有多种类型,分别对应了不同的数据库表: 美食满减红包,简称红包 商品购物券,简称购物券 QQ会员 外卖会员等 实际情况远不止这些,这个需求是要我们写一个业务分派的逻辑 第一个能想到的思路就是if-else或者switch case: 阅读详情
AI摘要

本文介绍了如何利用策略模式解决图形Shape的正删改查业务逻辑差异问题,通过接口与实现类分离、枚举类型定义和工厂模式,避免代码冗余并提升维护性。

1前言

在业务处理的过程中遇见一个场景,图形Shape分A,B,C,他们都是对应的实现都是正删改查,但是不同的图形Shape对应的增删改查业务逻辑有存在差异,这个时候如果不想办法设计代码就会出现A,B,C三个图形Shape都要写一个对应接口(XXXserver),然后分别再去实现他们(XXXserverImpl),如果后来再增加一台图形Shape D,我们又要去重复相关接口和实现,不仅显得代码冗余,更是不好维护和管理,所以利用设计模式---策略模式就解决了这个问题。

2基础知识

2.1对@Autowired注解的理解

在平常使用中我们对@Autowired都是这样用

@Autowired
private XXXService service;

解释说明:@Autowired是按照byType注入的(即按照bean的类型),当注入接口的时候我们都知道,他是注入的是这个接口的实现类,只不过引用是接口而已,当你调用其中的方法的时候,实质是调用接口实现的方法(多态原理),所以XXXService有多个实现类时,用上面代码块的方式进行注入,会报错。

3策略模式的引入

3.1代码展示:

3.1.1一个接口有多个实现

//接口
public interface Shape {
   void draw();
}
//实现1
@Service
public class Rectangle implements Shape {
   @Override
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}

        
SpringBoot策略模式实战:利用Map注入优雅管理多实现类 本文详细介绍了如何在SpringBoot项目中利用策略模式和Map注入优雅管理多实现类。通过实际案例展示如何定义策略接口、实现具体策略类,并使用Spring的自动装配机制将实现类注入到Map中,从而提升代码的可维护性和扩展性。文章还分享了高级应用技巧和常见问题解决方案,帮助开发者高效应对复杂业务场景。 阅读详情

相关推荐

策略模式在SpringBoot中的高级应用:告别if-else,用Map注入实现动态调用

本文深入探讨了策略模式在SpringBoot中的高级应用,通过Map注入实现动态调用,有效替代传统的if-else逻辑。文章详细介绍了如何利用SpringBoot的依赖注入特性,将策略模式与Map注入结合,提升代码的可维护性和扩展性,适用于订单处理、图形绘制等多种业务场景。

weixin_29232507的博客 308

SpringBoot中如何优雅地管理多个策略实现类?Map注入实战指南

本文详细介绍了在SpringBoot中如何利用Map注入优雅管理多个策略实现类,结合策略模式实现灵活的业务逻辑扩展。通过实战示例展示基础实现和高级技巧,包括自定义Map key、条件化策略注册等,帮助开发者构建可维护的代码架构。

weixin_33724046的博客 207

springboot 根据用户ID切换动态数据源代码实现

MyBatis是一个Java持久层框架,可以与各种关系型数据库进行交互。在MyBatis中实现动态数据源意味着根据运行时的需求,动态地切换和使用不同的数据源。

洛阳泰山的博客 3151

在SpringBoot中怎么注入各种类型(String,List,Map,Object)配置?

在SpringBoot中怎么注入各种类型(String,List,Map,Object)配置?背景String类型配置注入List类型配置注入Map类型配置注入Object类型配置注入结语 背景 在开发过程中,绝大多数场景都是要使用一些配置项的。至于配置项如何注入到应用程序中,一直没有过多的关注过。今天抽了点时间实际试了下,这里做个简单的记录,便于需要的朋友了解下。 在下面的讲述中,我们针对下面的一组配置来进行说明,分别演示读取字符串String、列表List、哈希表Map和对象Object进行说明。开发框架为Springboot。 config: str: 123456 list:

时序定江山,功耗写风骨——解读时序库lib文件的奥义【芯片设计 lib文件】

lib库,全称liberty library format file:它是Liberty时序文件的简称,使用 Liberty 语法来编写。它可以通过library compiler/dc转换成.db文件(database),供synopsis家的DC和PT使用,也可以通过spyglass的library compiler来生成sglib文件,供spyglass使用。他们是等价的,但db文件是二进制格式,人类不可读。

LogicYarn的博客 2601

STM32F103跑LVGL,用DMA加速刷屏,800*480大屏也能丝滑

本文详细介绍了如何在STM32F103芯片上通过DMA加速和内存优化,实现800*480大屏的LVGL流畅运行。通过FSMC+DMA硬件架构优化、双缓冲机制和LVGL参数调优,显著提升了界面刷新率和响应速度,适用于嵌入式GUI开发中的高性能需求场景。

weixin_30357231的博客 376

vcs中systemverilog和c/c++联合仿真

前言 systemverilog中,可以通过DPI、PLI来与外部其它语言的程序进行交互。PLI又分为三类,TF、ACC和VPI,可以认为是PLI技术的三代演进: 其中VPI(PLI3.0)是TF/ACC的一个超集,目前使用的PLI基本上都是VPI。 与PLI技术相独立的另一个技术就是DPI,DPI技术简化了systemverilog与foreign language的交互步骤,对于开发者相对友好,所以使用相对广泛一些。 本文不对DPI中程序调用/数据交互展开讨论(这部分可以参考绿皮书..

kevindas的博客 1万+

GB 44497-2024《智能网联汽车 自动驾驶数据记录系统》标准解读

GB 44497-2024《智能网联汽车 自动驾驶数据记录系统》针对智能网联汽车的自动驾驶数据记录系统进行了全面规范。标准要求我们记录车辆在自动驾驶模式下的所有关键操作数据,如:1. 车辆状态数据,如速度、加速度、转向角度、制动状态等;2. 环境感知数据,如周围物体的位置、速度、类型等;3. 驾驶决策数据,如路径规划、避障决策、紧急制动指令等;4. 系统状态数据,如软件版本、硬件状态、系统警告信息等。在数据存储方面,标准强调了数据的安全性和可靠性。

daopuyun的博客 2668

YOLOv8中Bottleneck模块详解

Bottleneck 模块在YOLOv8中的作用是进行特征提取和增强,是网络中的核心构建模块之一。它的主要功能是通过卷积操作来处理输入特征图,并在适当情况下应用残差连接,使得信息能够有效地通过网络层进行传播

zzzyyy8的博客 8060

本地玩转AI还得靠这个,SillyTavern(酒馆)到底有多强?

SillyTavern是一款本地运行的LLM交互前端,支持文本生成、图像生成、语音合成等功能。它最初是TavernAI的分支,现已独立开发,社区活跃,功能更新频繁。SillyTavern通过前端UI和插件系统连接各种大模型后端,如OpenAI、KoboldAI等,用户可以在本地创建“虚拟酒馆”与各种角色对话。其功能亮点包括角色卡、角色记忆、世界设定、多人聊天室、自定义UI和行为脚本、插件扩展支持等。SillyTavern适合AI创作用户、AI工程师、LLM爱好者和AIGC创作者。它有两个维护分支:relea

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 8033

“探索thumbdata4文件的奥秘:编程中的神秘存在“

综上所述,thumbdata4文件在编程中是一个常见的存在,与图库应用程序和图片浏览器密切相关。通过使用适当的库和工具,我们可以读取和处理thumbdata4文件中的缩略图数据,以提供更好的用户体验。我们首先读取8字节的块头,其中包含了下一个缩略图块的大小。你可以根据自己的需求来定义对块数据的处理方式,例如将其保存为图像文件或进行其他的分析操作。thumbdata4文件通常以二进制格式存储,其中包含着设备上浏览过的图片的缩略图。因此,你可能需要根据具体的thumbdata4文件结构进行适当的解析和处理。

JieLun_C的博客 792

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

上一篇: IDEA中查看方法的调用链
下一篇: SpringBoot单元测试时实现回滚事务
jothan_lee
博客等级 码龄9年 30粉丝 58原创
评论 10
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值