当前位置: 首页 > news >正文

枚举的高阶用法之枚举里写方法以及注入spring的bean

1、前言

  一般我们使用枚举都是用来定义一些常量。比如我们需要一个表示订单类(pc订单、手机订单)的常量,那我们就可以使用枚举来实现,如下:

@AllArgsConstructor
public enum OrderTypeEnum{PC("PC", "电脑端"),PHONE("PHONE", "手机端");private String name;private String value;}

  接着再写一下根据name查找对应枚举的方法,枚举就算完成了。但比如我们的业务场景是这样的:我们有一个OrderTypeService接口及其实现类。

public interface OrderTypeService {String processPc(String order);String processPhone(String order);
}
@Service
public class OrderTypeServiceImpl implements OrderTypeService {@Overridepublic String processPc(String order) {//具体的处理pc订单的业务逻辑System.out.println("开始处理pc订单:" + order);return null;}@Overridepublic String processPhone(String order) {//具体的处理phone订单的业务逻辑System.out.println("开始处理phone订单:" + order);return null;}
}

  根据接口的方法名我们一眼就知道,processPc是用来处理PC订单的;processPhone是用来处理PHONE订单的。然后再实际调用这两个方法的地方就需要我们进行if-else的判断。这里的if-else判断的缺点是:代码扩展性很差,如果以后又增加了其它订单类型,我们还需要改这里的if-else逻辑。

        if (OrderTypeEnum.PC.equals(orderTypeEnum)) {orderTypeService.processPc("order");} else if (OrderTypeEnum.PHONE.equals(orderTypeEnum)) {orderTypeService.processPhone("order");}

2、思考

  针对以上if-else判断存在的问题,那我们能不能把调用各自业务逻辑的代码挪到枚举里呢?肯定是可以的,本期我们就来实现一下。

2.1、通过@PostConstruct注解

在枚举内定义BeanInjector,再通过PostConstruct注解的方法给枚举的orderTypeService属性注入bean。

@AllArgsConstructor
public enum OrderTypeEnum {PC("PC", "电脑端") {@Overridepublic void handle(String order) {orderTypeService.processPc(order);}},PHONE("PHONE", "手机端") {@Overridepublic void handle(String order) {orderTypeService.processPhone(order);}};private String name;private String value;protected static OrderTypeService orderTypeService;public abstract void handle(String order);@Componentpublic static class BeanInjector {@Autowiredprivate OrderTypeService orderTypeService;@PostConstructpublic void postConstruct() {OrderTypeEnum.orderTypeService = this.orderTypeService;}}
}

调用代码:

        OrderTypeEnum pcOrderType = OrderTypeEnum.PC;pcOrderType.handle("1");

输出结果:

开始处理pc订单:1

2.2、在枚举内实现ApplicationContextAware回调接口,获取到ApplicationContext,再从context里获取我们需要的bean

@AllArgsConstructor
public enum OrderTypeEnum {PC("PC", "电脑端") {@Overridepublic void handle(String order) {OrderTypeService orderTypeService = OrderTypeEnum.ApplicationContextProvider.getApplicationContext().getBean(OrderTypeService.class);orderTypeService.processPc(order);}},PHONE("PHONE", "手机端") {@Overridepublic void handle(String order) {OrderTypeService orderTypeService = OrderTypeEnum.ApplicationContextProvider.getApplicationContext().getBean(OrderTypeService.class);orderTypeService.processPhone(order);}};private String name;private String value;public abstract void handle(String order);@Componentpublic static class ApplicationContextProvider implements ApplicationContextAware {private static ApplicationContext context;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) {context = applicationContext;}public static ApplicationContext getApplicationContext() {return context;}}
}

调用代码同2.1
输出结果同2.1

2.3、还是需要实现ApplicationContextAware接口,但这次不是直接使用ApplicationContext,而是把ApplicationContext里的bean值直接注入到我们的枚举字段中。

public enum OrderTypeEnum {PC("PC", "电脑端") {@Overridepublic void handle(String order) {orderTypeService.processPc(order);}},PHONE("PHONE", "手机端") {@Overridepublic void handle(String order) {orderTypeService.processPhone(order);}};private String name;private String value;OrderTypeEnum(String name, String value) {this.name = name;this.value = value;}public abstract void handle(String order);@AutowiredOrderTypeService orderTypeService;
}
@Component
public class OrderTypeEnumInjector implements ApplicationContextAware {@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {for (OrderTypeEnum orderTypeEnum : OrderTypeEnum.values()) {applicationContext.getAutowireCapableBeanFactory().autowireBean(orderTypeEnum);}}
}

调用代码同2.1
输出结果同2.1
  这里重点是使用applicationContext.getAutowireCapableBeanFactory().autowireBean。它可以把枚举中使用了@Autowired注解的字段和applicationContext中的bean根据类型和名称做匹配,匹配到之后就会把bean注入到该字段中。从而枚举中的orderTypeService就有值了。

3、延申

  applicationContext.getAutowireCapableBeanFactory().autowireBean能否给普通的类(没有被spring管理)的字段注入bean呢?答案也是可以的,这里最好把这个普通对象定义成单例的,这样我们给字段注入一次bean就可以一直使用了,否则每次new 的时候还需要重新注入bean。

public class OrderTypeManager {private OrderTypeManager(){}private static OrderTypeManager orderTypeManager = null;public synchronized static OrderTypeManager getInstance(){if(orderTypeManager == null){orderTypeManager = new OrderTypeManager();}return orderTypeManager;}@Autowiredprivate OrderTypeService orderTypeService;public  void test(){orderTypeService.processPhone("2");}
}

  如上定义了一个单例的OrderTypeManager,但是OrderTypeManager没有被spring所管理,那我们怎么把OrderTypeService这个bean注入到orderTypeService字段里呢?

@Component
public class OrderTypeEnumInjector implements ApplicationContextAware {@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {for (OrderTypeEnum orderTypeEnum : OrderTypeEnum.values()) {applicationContext.getAutowireCapableBeanFactory().autowireBean(orderTypeEnum);}OrderTypeManager orderTypeManager = OrderTypeManager.getInstance();applicationContext.getAutowireCapableBeanFactory().autowireBean(orderTypeManager);}
}

调用代码:

        OrderTypeManager orderTypeManager = OrderTypeManager.getInstance();orderTypeManager.test();

输出结果:

开始处理phone订单:2

相关文章:

枚举的高阶用法之枚举里写方法以及注入spring的bean

1、前言 一般我们使用枚举都是用来定义一些常量。比如我们需要一个表示订单类(pc订单、手机订单)的常量,那我们就可以使用枚举来实现,如下: AllArgsConstructor public enum OrderTypeEnum{PC("PC", "电脑端"),PHONE("PHONE", "手机端&quo…...

游戏开发面试题2

网络游戏分为客户端和服务端,你能说说客户端和服务端都干了一些什么工作吗? 客户端(Client) 客户端是玩家直接交互的部分,主要负责用户界面、输入处理、渲染和部分逻辑处理。具体工作包括: 用户界面&…...

华为机试题-单车道汽车通行时间-Java

代码在最后面 1 题目描述 M(1 ≤ M ≤ 20)辆车需要在一条不能超车的单行道到达终点,起点到终点的距离为 N(1 ≤ N ≤ 400)。 速度快的车追上前车后,只能以前车的速度继续行驶,求最后一辆车到达…...

6-5,web3浏览器链接区块链(react+区块链实战)

6-5,web3浏览器链接区块链(react区块链实战) 6-5 web3浏览器链接区块链(调用读写合约与metamask联动) 6-5 web3浏览器链接区块链(调用读写合约与metamask联动) 这里就是浏览器端和智能合约的交…...

C# 多态性

C# 多态性 介绍 多态性是面向对象编程(OOP)的一个核心概念,它允许不同类的对象对同一消息做出响应,并产生不同的结果。在C#中,多态性主要通过继承、接口和虚方法来实现。本文将深入探讨C#中的多态性,包括其原理、实现方式以及在实际编程中的应用。 原理 多态性允许将…...

Visual Studio 安装程序无法执行修复或更新

一.问题场景 出现问题的场景:当你的VS已经安装但是无法在工具中下载新组件或者卸载了当时一直无法安装。 二.问题原因 如果计算机上的 Visual Studio 实例已损坏,则可能会出现此问题。 三.解决方法 如果之前尝试修复或更新 Visual Studio 失败&…...

C#与PLC通信——如何设置电脑IP地址

前言: 我们与PLC通过以太网通信时,首先要做的就是先设置好电脑的IP,这样才能实现上位机电脑与PLC之间的通信,并且电脑的ip地址和PLC的Ip地址要同处于一个网段,比如电脑的Ip地址为192.168.1.1,那么PLC的Ip地…...

Milvus 核心设计(1) ---- 数据一致性的等级及使用场景

目录 背景 Milvus的数据一致性 设置数据一致性等级 等级类型 PACELC定理 level 详细解释 Strong Bounded staleness Session Eventually 总结 背景 分布式上的可扩展性是个比较重要的concept。Chroma 核心之前写过了,他的最大优势在于轻量级且好用。Milvus相对Ch…...

EasyCVR视频技术:城市电力抢险的“千里眼”,助力抢险可视化

随着城市化进程的加速和电力需求的不断增长,电力系统的稳定运行对于城市的正常运转至关重要。然而,自然灾害、设备故障等因素常常导致电力中断,给城市居民的生活和企业的生产带来严重影响。在这种情况下,快速、高效的电力抢险工作…...

【Wamp】局域网设备访问WampServer | 使用域名访问Wamp | Wamp配置HTTPS

局域网设备访问WampServer 参考&#xff1a;https://www.jianshu.com/p/d431a845e5cb 修改Apache的httpd.conf文件 D:\Academic\Wamp\program\bin\apache\apache2.4.54.2\conf\httpd.conf 搜索 Require local 和Require all denied&#xff0c;改为Require all granted <…...

采用自动微分进行模型的训练

自动微分训练模型 简单代码实现&#xff1a; import torch import torch.nn as nn import torch.optim as optim# 定义一个简单的线性回归模型 class LinearRegression(nn.Module):def __init__(self):super(LinearRegression, self).__init__()self.linear nn.Linear(1, 1) …...

k8s怎么配置secret呢?

在Kubernetes中&#xff0c;配置Secret主要涉及到创建、查看和使用Secret的过程。以下是配置Secret的详细步骤和相关信息&#xff1a; ### 1. Secret的概念 * Secret是Kubernetes用来保存密码、token、密钥等敏感数据的资源对象。 * 这些敏感数据可以存放在Pod或镜像中&#x…...

算法篇 滑动窗口 leetcode 长度最小的子数组

长度最小的子数组 1. 题目描述2. 算法图分析2.1 暴力图解2.2 滑动窗口图解 3. 代码演示 1. 题目描述 2. 算法图分析 2.1 暴力图解 2.2 滑动窗口图解 3. 代码演示...

数据库作业d8

要求&#xff1a; 一备份 1 mysqldump -u root -p booksDB > booksDB_all_tables.sql 2 mysqldump -u root -p booksDB books > booksDB_books_table.sql 3 mysqldump -u root -p --databases booksDB test > booksDB_and_test_databases.sql 4 mysql -u roo…...

前后端数据交互设计到的跨域问题

前后端分离项目的跨域问题及解决办法 一、跨域简述 1、问题描述 这里前端vue项目的端口号为9000&#xff0c;后端springboot项目的端口号为8080 2、什么是跨域 当一个请求url的协议、域名、端口三者之间任意一个与当前页面url不同即为跨域 当前页面url被请求页面url是否…...

非洲猪瘟监测设备的作用是什么?

TH-H160非洲猪瘟监测设备的主要作用是迅速、准确地检测出非洲猪瘟病毒&#xff0c;从而帮助控制和预防疫情的扩散。这些设备利用先进的生物传感技术和PCR分子生物学方法&#xff0c;能够在极短的时间内提供精确的检测结果<sup>1</sup><sup>2</sup><…...

移动硬盘损坏无法读取?专业恢复策略全解析

在数字化信息爆炸的今天&#xff0c;移动硬盘作为我们存储和传输大量数据的重要工具&#xff0c;其安全性和稳定性直接关系到个人与企业的数据安全。然而&#xff0c;当移动硬盘突然遭遇损坏&#xff0c;无法正常读取时&#xff0c;我们该如何应对&#xff1f;本文将深入探讨移…...

神经网络以及简单的神经网络模型实现

神经网络基本概念&#xff1a; 神经元&#xff08;Neuron&#xff09;&#xff1a; 神经网络的基本单元&#xff0c;接收输入&#xff0c;应用权重并通过激活函数生成输出。 层&#xff08;Layer&#xff09;&#xff1a; 神经网络由多层神经元组成。常见的层包括输入层、隐藏层…...

java中压缩文件的解析方式(解析文件)

背景了解&#xff1a;java中存在IO流的方式&#xff0c;支持我们对文件进行读取&#xff08;Input&#xff0c;从磁盘到内存&#xff09;或写入&#xff08;output&#xff0c;从内存到磁盘&#xff09;&#xff0c;那么我们在面对 “zip”格式或者 “rar” 格式的压缩文件&…...

巧用 VScode 网页版 IDE 搭建个人笔记知识库!

[ 知识是人生的灯塔&#xff0c;只有不断学习&#xff0c;才能照亮前行的道路 ] 巧用 VScode 网页版 IDE 搭建个人笔记知识库! 描述&#xff1a;最近自己在腾讯云轻量云服务器中部署了一个使用在线 VScode 搭建部署的个人Markdown在线笔记&#xff0c;考虑到在线 VScode 支持终…...

使用docker在3台服务器上搭建基于redis 6.x的一主两从三台均是哨兵模式

一、环境及版本说明 如果服务器已经安装了docker,则忽略此步骤,如果没有安装,则可以按照一下方式安装: 1. 在线安装(有互联网环境): 请看我这篇文章 传送阵>> 点我查看 2. 离线安装(内网环境):请看我这篇文章 传送阵>> 点我查看 说明&#xff1a;假设每台服务器已…...

相机Camera日志实例分析之二:相机Camx【专业模式开启直方图拍照】单帧流程日志详解

【关注我&#xff0c;后续持续新增专题博文&#xff0c;谢谢&#xff01;&#xff01;&#xff01;】 上一篇我们讲了&#xff1a; 这一篇我们开始讲&#xff1a; 目录 一、场景操作步骤 二、日志基础关键字分级如下 三、场景日志如下&#xff1a; 一、场景操作步骤 操作步…...

QMC5883L的驱动

简介 本篇文章的代码已经上传到了github上面&#xff0c;开源代码 作为一个电子罗盘模块&#xff0c;我们可以通过I2C从中获取偏航角yaw&#xff0c;相对于六轴陀螺仪的yaw&#xff0c;qmc5883l几乎不会零飘并且成本较低。 参考资料 QMC5883L磁场传感器驱动 QMC5883L磁力计…...

uni-app学习笔记二十二---使用vite.config.js全局导入常用依赖

在前面的练习中&#xff0c;每个页面需要使用ref&#xff0c;onShow等生命周期钩子函数时都需要像下面这样导入 import {onMounted, ref} from "vue" 如果不想每个页面都导入&#xff0c;需要使用node.js命令npm安装unplugin-auto-import npm install unplugin-au…...

2025盘古石杯决赛【手机取证】

前言 第三届盘古石杯国际电子数据取证大赛决赛 最后一题没有解出来&#xff0c;实在找不到&#xff0c;希望有大佬教一下我。 还有就会议时间&#xff0c;我感觉不是图片时间&#xff0c;因为在电脑看到是其他时间用老会议系统开的会。 手机取证 1、分析鸿蒙手机检材&#x…...

网络编程(UDP编程)

思维导图 UDP基础编程&#xff08;单播&#xff09; 1.流程图 服务器&#xff1a;短信的接收方 创建套接字 (socket)-----------------------------------------》有手机指定网络信息-----------------------------------------------》有号码绑定套接字 (bind)--------------…...

安宝特方案丨船舶智造的“AR+AI+作业标准化管理解决方案”(装配)

船舶制造装配管理现状&#xff1a;装配工作依赖人工经验&#xff0c;装配工人凭借长期实践积累的操作技巧完成零部件组装。企业通常制定了装配作业指导书&#xff0c;但在实际执行中&#xff0c;工人对指导书的理解和遵循程度参差不齐。 船舶装配过程中的挑战与需求 挑战 (1…...

Oracle11g安装包

Oracle 11g安装包 适用于windows系统&#xff0c;64位 下载路径 oracle 11g 安装包...

Spring Security 认证流程——补充

一、认证流程概述 Spring Security 的认证流程基于 过滤器链&#xff08;Filter Chain&#xff09;&#xff0c;核心组件包括 UsernamePasswordAuthenticationFilter、AuthenticationManager、UserDetailsService 等。整个流程可分为以下步骤&#xff1a; 用户提交登录请求拦…...

SQL Server 触发器调用存储过程实现发送 HTTP 请求

文章目录 需求分析解决第 1 步:前置条件,启用 OLE 自动化方式 1:使用 SQL 实现启用 OLE 自动化方式 2:Sql Server 2005启动OLE自动化方式 3:Sql Server 2008启动OLE自动化第 2 步:创建存储过程第 3 步:创建触发器扩展 - 如何调试?第 1 步:登录 SQL Server 2008第 2 步…...