Spring-based state conversion method and device for multiple service fields
By using a global state machine design, reflection mechanism, and proxy technology, the problems of high coupling and poor scalability of traditional state patterns in complex business systems are solved. This enables flexible management of multiple state machines and efficient state switching, making it suitable for complex business scenarios such as alarm management and order processing systems.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- 广州海量数据库技术有限公司
- Filing Date
- 2026-01-14
- Publication Date
- 2026-04-24
AI Technical Summary
Traditional state patterns suffer from problems in complex business systems, such as high complexity of state machine composition, high coupling, difficulty in supporting hot updates of business rules and delayed binding of methods, repetitive development and weak reliability in multi-state machine collaboration scenarios.
By employing a global state machine design, reflection mechanism, and proxy technology, and defining abstract state classes, state implementation classes, business state machines, and state machine containers, we can achieve unified management of multiple state machines and dynamic method invocation, and support state transitions across state machines.
It achieves decoupling and collaborative management of multiple state machines, improves the system's flexibility and maintainability, supports dynamic expansion of business rules and efficient state switching, and is suitable for complex business scenarios such as alarm management and order processing systems.
Smart Images

Figure CN121921829A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer operation technology, and in particular to a method, apparatus, computer-readable storage medium, and electronic device for state transition of multiple business fields based on Spring. Background Technology
[0002] The State pattern, a classic design pattern in software engineering, encapsulates the behavior of an object within a state class, achieving a dynamic binding mechanism where behavior changes with state. However, in the engineering practice of complex business systems, the traditional State pattern solution has revealed several structural flaws:
[0003] First, when a system needs to maintain multiple dimensions of states simultaneously (such as alarm lifecycle states, record reading states, and other orthogonal business attributes), the combinatorial complexity between state machines increases exponentially, leading to highly coupled state transition logic and a significant decrease in maintainability and scalability. The exponential increase in state machines can easily cause redundant definitions and consistency issues in state transition paths.
[0004] Secondly, traditional implementations rely on static method dispatching mechanisms of specific state subclasses, lacking runtime dynamic behavior orchestration capabilities, making it difficult to support hot updates of business rules and delayed binding of methods, and failing to meet the requirements of business flexibility under the current microservice architecture.
[0005] Third, in large-scale distributed systems such as alarm management and work order flow, there may be combinations and switching logic of multiple states, such as alarm states (triggered, recovered, closed, etc.) and alarm record reading states (not read, read, etc.). In this case, multi-state machine collaboration scenarios require state transitions to have transactional guarantees and idempotency control. Existing technical solutions have failed to provide standardized cross-state machine collaboration primitives, and there are common problems such as repeated development and weak reliability in engineering implementation.
[0006] The aforementioned technical deficiencies restrict the effectiveness of state patterns in engineering applications in complex enterprise-level business scenarios, and there is an urgent need to develop a state management framework that can support collaborative conversion of multiple business fields and has dynamic scalability. Summary of the Invention
[0007] To overcome the aforementioned deficiencies in existing technologies, this application proposes a novel state transition method based on Spring for multiple business fields. By introducing a global state machine design and a dynamic method invocation mechanism, this invention constructs a state pattern transition framework that combines state patterns, reflection mechanisms, and proxy technology. This solution can simultaneously manage multiple state machines and their respective states, providing a flexible, easily extensible, and highly maintainable solution for state machine management and state pattern transitions.
[0008] Specifically, this application provides the following technical solutions:
[0009] The first aspect of this application provides a state transition method for multiple business fields based on Spring, the method comprising:
[0010] S1. Define an abstract state class for each business field, and declare a state transition method in the abstract state class. The state transition method is used to transition between different states.
[0011] S2. Define multiple state implementation classes. Each state implementation class inherits from the abstract state class and implements the state transition method. In the state transition method, business logic is executed and the current state is switched to the target state.
[0012] S3. Define a business state machine. The business state machine inherits from the abstract state class and holds instances of all state implementation classes. The business state machine maintains the current memory state instance and routes the state transition method to the current memory state instance for execution based on external calls.
[0013] S4. Define a state machine container, which aggregates multiple business state machines from different business dimensions to achieve unified registration and management of multiple state machines;
[0014] S5. Use reflection mechanism to obtain the target transition method and its parameter type of the target state machine, and dynamically call the target transition method through proxy technology to complete the state transition across state machines;
[0015] S6. Provide a unified state transition interface. The business layer passes in the state switching instruction through the state transition interface to trigger the proxy technology to complete the state transition.
[0016] Furthermore, in the method of this application, the state implementation class is configured as a component bean of the Spring framework, and the business state machine instance is obtained through the Spring dependency injection mechanism to perform state switching; and the state update interface of the business state machine is called in the state transition method to realize the synchronous update of business data and business state machine.
[0017] Furthermore, in the method of this application, before executing the state transition method, the business state machine calls the initialization method to load the corresponding state implementation class instance from the Spring container according to the business identifier, and sets the instance as the current memory state instance.
[0018] Furthermore, the method of this application also includes: dynamically obtaining Bean instances from the Spring container through a Spring context holder, wherein the Spring context holder implements the ApplicationContextAware interface to hold the application context.
[0019] Furthermore, in the method of this application, the abstract state class includes abstract methods for upward state transition and downward state transition, and the state machine type is constrained by generics.
[0020] Furthermore, in the method of this application, before routing the state transition method, the business state machine executes pre-verification logic to determine whether the current memory state instance meets the transition conditions; if not, the transition is terminated.
[0021] Furthermore, in the method of this application, the method for obtaining the target state machine and its parameter types using a reflection mechanism includes:
[0022] Obtain all public methods of the target state machine object;
[0023] Iterate through the public methods and match the target method name;
[0024] Extract the parameter type list of the matching method, and use the parameter type list to construct the reflection call.
[0025] Furthermore, in the method of this application, the state transition interface encapsulates multi-layer proxy calls, and the business layer only interacts with the outermost utility class. The utility class sequentially calls the state proxy and the state proxy executor to complete the final migration.
[0026] A second aspect of this application provides a Spring-based multi-business-field state transition apparatus, wherein the apparatus, when running, implements the aforementioned Spring-based multi-business-field state transition method steps, and the apparatus includes:
[0027] The abstract state class definition module is used to define the abstract state class for each business field, and declare state transition methods in the abstract state class. The state transition methods are used to switch between different states.
[0028] The state implementation class definition module is used to define multiple state implementation classes. The state implementation classes inherit from the abstract state class and implement the state transition method. The state transition method executes business logic and switches the current state to the target state.
[0029] The business state machine definition module is used to define a business state machine. The business state machine inherits from the abstract state class and holds instances of all state implementation classes. The business state machine maintains the current memory state instance and routes the state transition method to the current memory state instance for execution based on external calls.
[0030] The state machine container definition module is used to define state machine containers, which aggregate multiple business state machines from different business dimensions to achieve unified registration and management of multiple state machines;
[0031] The state proxy execution module is used to obtain the target transition method and its parameter type of the target state machine using the reflection mechanism, and dynamically call the target transition method through proxy technology to complete the state transition across state machines;
[0032] The state transition interface module provides a unified state transition interface. The business layer passes in state switching instructions through the state transition interface, which triggers the proxy technology to complete the state transition.
[0033] A third aspect of this application provides an electronic device, including: a memory and a processor;
[0034] Memory: Used to store computer programs;
[0035] Processor: Used to execute the computer program to implement the aforementioned steps of the Spring-based multi-business field state transition method.
[0036] A fourth aspect of this application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the aforementioned steps of the Spring-based multi-business field state transition method.
[0037] In summary, compared with the prior art, the present invention has the following advantages:
[0038] (1) Decoupling and collaborative management of multi-state machines
[0039] A global state machine is used to uniformly orchestrate multi-dimensional business state machines (such as alarm lifecycle state machines, alarm record reading state machines, etc.). The state registry is used to achieve decoupled registration and independent evolution of state machines, which significantly reduces the coupling between orthogonal state attributes, facilitates the expansion of new state machines and state logic, and supports standardized collaboration and runtime dynamic mounting of heterogeneous state machines.
[0040] (2) Dynamic method invocation mechanism based on reflection
[0041] By leveraging reflection and the proxy pattern, dynamic invocation of state machine methods is achieved, transforming traditional static method calls into runtime dispatch. Developers no longer need to explicitly specify state methods; instead, the proxy dynamically determines the state machine and method to be invoked. This dynamic adaptation mechanism greatly improves the framework's flexibility, making it particularly suitable for multi-state transition scenarios in complex business environments.
[0042] (3) Simplified code structure and improved maintainability
[0043] By eliminating explicit if-else or switch-case state branch logic through the proxy pattern, direct dependence on specific implementations is reduced, improving code simplicity and maintainability.
[0044] (4) Horizontal scaling architecture that conforms to the open / closed principle
[0045] The state machine logic supports non-intrusive extension by adding new state classes or behavior methods without modifying the existing state transition chain. It meets the design principle of being closed to modification and open to extension, and is suitable for complex business scenarios with frequent requirement iterations.
[0046] (5) Efficient state switching and logic execution
[0047] The global state machine design makes state transitions more efficient. By leveraging the proxy pattern, new behavioral logic can be dynamically injected during state transitions without modifying the original state classes. This avoids frequent refactoring of existing state classes, reduces redundant definitions of state transition paths, and lowers maintenance costs and the risk of introducing defects during system evolution while ensuring efficient state transitions.
[0048] (6) Wide range of application scenarios
[0049] This invention is particularly suitable for complex business scenarios that require the management of multiple states and state switching logic, such as alarm management systems, order processing systems, and workflow engines.
[0050] Other features and advantages of this application will be set forth in detail in the following description, or will become apparent through the implementation of the relevant technical solutions of this application. The objectives and other advantages of this application can be achieved through the technical features and means explicitly pointed out in the description, claims, and drawings, and will be obtained through the implementation of these technical contents. Attached Figure Description
[0051] To more clearly illustrate the technical solution of this application, the accompanying drawings involved in the description of this invention will be briefly introduced below. It should be noted that the drawings only show some embodiments of the invention. For those skilled in the art, other related drawings can be derived from these drawings without creative effort.
[0052] Figure 1 This is a flowchart illustrating the overall implementation process of the state transition method for multiple business fields based on Spring in this application.
[0053] Figure 2 This is a complete implementation process diagram of the method in the embodiments of this application.
[0054] Figure 3 This is a structural diagram of the state transition device for multiple business fields based on Spring in this application.
[0055] Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0056] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. It should be noted that the described embodiments are only some embodiments of this application, and not all embodiments. All other embodiments obtained by those skilled in the art based on the embodiments of this application without creative effort are within the protection scope of this application.
[0057] In this document, the term "comprising" and any variations thereof (such as "including," "including," etc.) are open-ended expressions and should be understood as "including but not limited to," meaning that the listed content is not exhaustive and may include other content not explicitly mentioned. The term "based on" should be understood as "at least partially based on," meaning that the basis or condition referred to may not be the only factor and may involve other relevant factors. The term "one embodiment" should be understood as "at least one embodiment," meaning that the described embodiment is not the only possible implementation, and other similar embodiments may exist.
[0058] In this application, the terms "a" and "a plurality of" are used to modify related elements or features, and their expression is illustrative rather than restrictive. Unless otherwise expressly stated in the context, "a" should be understood as "at least one," and "a plurality of" should be understood as "at least two." Those skilled in the art should reasonably interpret these terms based on the semantic and logical relationships of the context to ensure that they cover the possibility of "one or more."
[0059] Example: A Spring-based method for state transition of multiple business fields
[0060] Figure 1 The diagram shows the overall implementation process of the method described in this application, including the following steps:
[0061] S1. Define an abstract state class for each business field, and declare a state transition method in the abstract state class. The state transition method is used to transition between different states.
[0062] S2. Define multiple state implementation classes. Each state implementation class inherits from the abstract state class and implements the state transition method. In the state transition method, business logic is executed and the current state is switched to the target state.
[0063] S3. Define a business state machine. The business state machine inherits from the abstract state class and holds instances of all state implementation classes. The business state machine maintains the current memory state instance and routes the state transition method to the current memory state instance for execution based on external calls.
[0064] S4. Define a state machine container, which aggregates multiple business state machines from different business dimensions to achieve unified registration and management of multiple state machines;
[0065] S5. Use reflection mechanism to obtain the target transition method and its parameter type of the target state machine, and dynamically call the target transition method through proxy technology to complete the state transition across state machines;
[0066] S6. Provide a unified state transition interface. The business layer passes in the state switching instruction through the state transition interface to trigger the proxy technology to complete the state transition.
[0067] Figure 2 The following is a complete implementation process of this embodiment, including the following steps:
[0068] Step 1: Define the status interface class and business methods for each business field.
[0069] / **
[0070] * Alarm Status Interface
[0071] /
[0072] public abstract class AbstractAlertState {
[0073] / **
[0074] * Turn up
[0075] * @param id
[0076] /
[0077] public void up(Integer id) {}
[0078] / **
[0079] * Turn down
[0080] * @param id
[0081] /
[0082] public void down(Integer id) {}
[0083] }
[0084] The code above defines an abstract class, some business methods, and the behavior of the state machine in a specific state.
[0085] Step 2: Define specific state classes and implement business methods.
[0086] @Component
[0087] public class RecoverAlertState extends AbstractAlertState {
[0088] @Autowired
[0089] private AlertStateMachine alertStateMachine;
[0090] @Override
[0091] public void up(Integer id) {
[0092] System.out.println("
Restore
[0093] / / Update business data status
[0094] / / After the update, change the state of the state machine in memory.
[0095] alertStateMachine.setMemoryAlertState(alertStateMachine.getCloseAlertState());
[0096] }
[0097] @Override
[0098] public void down(Integer id) {
[0099] System.out.println("
Restore
[0100] / / Update business data status
[0101] / / After the update, change the state of the state machine in memory.
[0102] alertStateMachine.setMemoryAlertState(alertStateMachine.getFiringAlertState());
[0103] }
[0104] }
[0105] The code above defines specific behaviors under a specific state, which can realize specific business functions and modify the business state to the target state.
[0106] Step 3: Define the state machine and implement the business methods.
[0107] / **
[0108] * Alarm State Machine
[0109] /
[0110] @Component
[0111] @Data
[0112] public class AlertStateMachine extends AbstractAlertState implementsSerializable {
[0113] / **
[0114] State machine in memory
[0115] /
[0116] private AbstractAlertState memoryAlertState;
[0117] @Autowired
[0118] private FiringAlertState firingAlertState;
[0119] @Autowired
[0120] private RecoverAlertState recoverAlertState;
[0121] @Autowired
[0122] private CloseAlertState closeAlertState;
[0123] / **
[0124] * Initialize the state machine's state
[0125] *
[0126] * @param id
[0127] /
[0128] public void initMemoryFileState(Integer id) {
[0129] if (id == null) {
[0130] System.out.println("Business ID not found");
[0131] }
[0132] memoryAlertState = SpringContextHolder.getBean(AlertStateEnum.getStateMachine(id));
[0133] }
[0134] @Override
[0135] public void up(Integer id) {
[0136] initMemoryFileState(id);
[0137] / / This is the logic check. If the state is not as expected, it can throw an error or return.
[0138] if (memoryAlertState == closeAlertState) {
[0139] System.out.println("It's already in the closed state, and its state cannot be changed further");
[0140] }
[0141] memoryAlertState.up(id);
[0142] }
[0143] @Override
[0144] public void down(Integer id) {
[0145] initMemoryFileState(id);
[0146] / / This is the logic check. If the state is not as expected, it can throw an error or return.
[0147] if (memoryAlertState == firingAlertState) {
[0148] System.out.println("It's already in a triggered state, so the state cannot be changed further");
[0149] }
[0150] memoryAlertState.down(id);
[0151] }
[0152] }
[0153] The code above is a concrete implementation of a state machine, which includes a method for initializing the state machine and the implementation of business logic methods. Before executing the specific state method, some logical judgments or common code logic can be added. Only when the expected state is reached can the business logic continue to be executed.
[0154] Step 4: Define the global state machine
[0155] / **
[0156] Global State Machine
[0157] /
[0158] @Data
[0159] @Component
[0160] public class GlobalStateMachine {
[0161] / **
[0162] * Alarm State Machine
[0163] /
[0164] private AlertStateMachine alertStateMachine;
[0165] / **
[0166] * Record reading of state machine
[0167] /
[0168] private RecordStateMachine recordStateMachine;
[0169] }
[0170] The code above defines a global state machine, which references multiple state machines, providing unified management of state machines for multiple business states.
[0171] Step 5: Define the agent execution state machine
[0172] / **
[0173] * Proxy execution state machine
[0174] /
[0175] public class StateMachineProxyUtil {
[0176] / **
[0177] * State transitions of the agent execution state machine
[0178] *
[0179] * @param stateChange State transition information
[0180] * @param objectService Service class
[0181] /
[0182] public static void proxyExecutionSingleState(StateChange stateChange,Object objectService) {
[0183] / / Get the specified state machine from the global state machine
[0184] Field field = ReflectionUtils.findField(objectService.getClass(),stateChange.getStateMachineType());
[0185] if (field == null) {
[0186] System.out.println("The specified state machine was not found in the global state machine");
[0187] }
[0188] / / Get the state machine object to be executed
[0189] Object stateMachineObject = SpringContextHolder.getBean(field.getName());
[0190] try {
[0191] / / Get the parameter types of the target execution method
[0192] List <Class <t>> paramTypesList = getMethodParamTypes(stateMachineObject, stateChange.getEventMethodName());
[0193] / / Construct the target execution method
[0194] Method method = ReflectionUtils.findMethod(stateMachineObject.getClass(), stateChange.getEventMethodName(),paramTypesList.toArray(new Class[paramTypesList.size()]));
[0195] if (stateChange.getObjectParam().length != paramTypesList.size()) {
[0196] System.out.println("param count is invalid");
[0197] } else {
[0198] ReflectionUtils.invokeMethod(method, stateMachineObject,stateChange.getObjectParam());
[0199] }
[0200] } catch (ClassNotFoundException e) {
[0201] System.out.println("The specified class was not found");
[0202] }
[0203] }
[0204] / **
[0205] * Retrieve the types of all parameters based on the method name
[0206] *
[0207] * @param classInstance Class instance
[0208] * @param methodName methodName
[0209] * @return List <Class <t>> Object Collection
[0210] * @throws ClassNotFoundException
[0211] /
[0212] public static List <Class <t>> getMethodParamTypes(ObjectclassInstance, String methodName) throws ClassNotFoundException {
[0213] List<Class <t>> paramTypes = new ArrayList<>();
[0214] Method[] methods = classInstance.getClass().getMethods();
[0215] for (Method method : methods) {
[0216] if (method.getName().equals(methodName)) {
[0217] Class<?>[] params = method.getParameterTypes();
[0218] for (Class<?> classParamType : params) {
[0219] paramTypes.addAll(Collections.singleton((Class <t>) Class.forName(classParamType.getName())));
[0220] }
[0221] break
[0222] }
[0223] }
[0224] return paramTypes;
[0225] }
[0226] }
[0227] In the code above, the specified state machine in the global state machine is first obtained, then the parameter type of the target method is obtained, and finally the target method to be executed is constructed.
[0228] Step 6: Define a Spring container context utility class
[0229] / **
[0230] * @description Context utility class
[0231] /
[0232] @Slf4j
[0233] @Component
[0234] public class SpringContextHolder implements ApplicationContextAware,DisposableBean {
[0235] private static ApplicationContext applicationContext = null;
[0236] / **
[0237] * Retrieve the Bean from the static variable applicationContext, and automatically cast it to the type of the assigned object.
[0238] /
[0239] @SuppressWarnings("unchecked")
[0240] public static <t>T getBean(String name) {
[0241] assertContextInjected();
[0242] return (T) applicationContext.getBean(name);
[0243] }
[0244] / **
[0245] * Retrieve the Bean from the static variable applicationContext, and automatically cast it to the type of the assigned object.
[0246] /
[0247] public static <t>T getBean(Class <t>requiredType) {
[0248] assertContextInjected();
[0249] return applicationContext.getBean(requiredType);
[0250] }
[0251] / **
[0252] * Check that the ApplicationContext is not empty.
[0253] /
[0254] private static void assertContextInjected() {
[0255] if (applicationContext == null) {
[0256] throw new IllegalStateException("applicationContext property not injected, please inject in applicationContext" +
[0257] Define SpringContextHolder in the .xml file or register SpringContextHolder in the Spring Boot startup class.
[0258] }
[0259] }
[0260] / **
[0261] * Clear the ApplicationContext in SpringContextHolder to null.
[0262] /
[0263] private static void clearHolder() {
[0264] log.debug("Clear the ApplicationContext in SpringContextHolder:")
[0265] + applicationContext);
[0266] applicationContext = null;
[0267] }
[0268] @Override
[0269] public void destroy() {
[0270] SpringContextHolder.clearHolder();
[0271] }
[0272] @Override
[0273] public void setApplicationContext(ApplicationContextapplicationContext) throws BeansException {
[0274] if (SpringContextHolder.applicationContext != null) {
[0275] log.warn("The ApplicationContext in SpringContextHolder has been overwritten; the original ApplicationContext was: " + SpringContextHolder.applicationContext);
[0276] }
[0277] SpringContextHolder.applicationContext = applicationContext;
[0278] }
[0279] }
[0280] The code above is used to retrieve a specified object from the Spring container, allowing the system to dynamically retrieve objects from the Spring container at runtime.
[0281] Step 7: Define the proxy class for the state machine.
[0282] / **
[0283] * Execute state machine agent
[0284] /
[0285] public class StateMachineProxy {
[0286] / **
[0287] * Get the global state machine bean
[0288] *
[0289] @return Object
[0290] /
[0291] public static Object getGlobalStateMachine() {
[0292] return SpringContextHolder.getBean(GlobalStateMachine.class);
[0293] }
[0294] / **
[0295] * State transitions of the agent execution state machine
[0296] *
[0297] * @param stateChange State transition information
[0298] /
[0299] public static void proxyExecutionSingleState(StateChange stateChange){
[0300] StateMachineProxyUtil.proxyExecutionSingleState(stateChange,getGlobalStateMachine());
[0301] }
[0302] }
[0303] The code above defines a method for obtaining the global state machine and a method for state transitions via a proxy execution machine.
[0304] Step 8: Define the state machine utility class
[0305] / **
[0306] * State machine utility class
[0307] /
[0308] public class StateMachineUtil {
[0309] / **
[0310] * Execute state machine state transition
[0311] *
[0312] * @param stateChange State transition information
[0313] /
[0314] public static void stateChange(StateChange stateChange) {
[0315] StateMachineProxy.proxyExecutionSingleState(stateChange);
[0316] }
[0317] }
[0318] The above methods serve as the entry point for this state machine pattern, allowing business code to call them.
[0319] Figure 3 The image shows a state transition device for multiple business fields based on Spring, as proposed in this application. The device includes:
[0320] The abstract state class definition module is used to define the abstract state class for each business field, and declare state transition methods in the abstract state class. The state transition methods are used to switch between different states.
[0321] The state implementation class definition module is used to define multiple state implementation classes. The state implementation classes inherit from the abstract state class and implement the state transition method. The state transition method executes business logic and switches the current state to the target state.
[0322] The business state machine definition module is used to define a business state machine. The business state machine inherits from the abstract state class and holds instances of all state implementation classes. The business state machine maintains the current memory state instance and routes the state transition method to the current memory state instance for execution based on external calls.
[0323] The state machine container definition module is used to define state machine containers, which aggregate multiple business state machines from different business dimensions to achieve unified registration and management of multiple state machines;
[0324] The state proxy execution module is used to obtain the target transition method and its parameter type of the target state machine using the reflection mechanism, and dynamically call the target transition method through proxy technology to complete the state transition across state machines;
[0325] The state transition interface module provides a unified state transition interface. The business layer passes in state switching instructions through the state transition interface, which triggers the proxy technology to complete the state transition.
[0326] The above-mentioned device implements the steps of the Spring-based multi-business field state transition method disclosed in this application when it is running.
[0327] The flowcharts and block diagrams in the accompanying drawings illustrate possible implementations of apparatus, methods, and computer program products according to various embodiments of this application, including architecture, functionality, and operation. In these figures, each block may represent a module, program segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should be noted that each block in the block diagrams and / or flowcharts, and combinations thereof, can be implemented using either a dedicated hardware-based system or a combination of dedicated hardware and computer instructions to achieve the specified function or operation.
[0328] like Figure 4 As shown, embodiments of this application also disclose an electronic device, including: a processor 310, a communication interface 320, a memory 330 for storing a processor-executable computer program, and a communication bus 340. The processor 310, communication interface 320, and memory 330 communicate with each other via the communication bus 340. The processor 310 executes the executable computer program to implement the steps of the Spring-based multi-service field state transition method described above.
[0329] It is understood that, in addition to memory and a processor, this electronic device may also include input devices (such as a keyboard), output devices (such as a display), and other communication modules. These input devices, output devices, and other communication modules all communicate with the processor through I / O interfaces (i.e., input / output interfaces).
[0330] The operations described in this application can be implemented by writing computer program code using one or more programming languages or a combination thereof. The programming languages include, but are not limited to, the following types:
[0331] Object-oriented programming languages, such as Java, Smalltalk, C++, etc.
[0332] Conventional procedural programming languages, such as "C" or similar programming languages.
[0333] The execution methods of program code include, but are not limited to:
[0334] It runs entirely on the user's computer;
[0335] Part of it executes on the user's computer, and part of it executes on a remote computer;
[0336] Execute as a standalone software package;
[0337] It is executed entirely on a remote computer or server.
[0338] In scenarios involving remote computers, the remote computer can connect to the user's computer via any type of network, including but not limited to local area networks (LANs) or wide area networks (WANs). Furthermore, the remote computer can also connect to external computers via an internet service provider, for example, by utilizing the internet.
[0339] Furthermore, this application also discloses a computer-readable storage medium that, when the instructions in the computer-readable storage medium are executed by a processor of an electronic device, enables the electronic device to perform the various steps of the Spring-based multi-business field state transition method disclosed in this application.
[0340] In the context of this application, a computer-readable storage medium refers to a tangible medium capable of storing computer program code and related data. Specific examples include, but are not limited to, the following:
[0341] (1) Portable computer disk: such as floppy disks and other removable magnetic storage media.
[0342] (2) Hard disk: including mechanical hard disks and solid-state hard disks and other fixed storage devices.
[0343] (3) Random Access Memory (RAM): A volatile storage medium used for temporary storage of data and program code.
[0344] (4) Read-only memory (ROM): a non-volatile storage medium used to store fixed programs and data.
[0345] (5) Erasable programmable read-only memory (EPROM) or flash memory: non-volatile storage media that supports multiple erasures and reprogrammings.
[0346] (6) Fiber optic storage devices: storage media based on fiber optic technology.
[0347] (7) Portable compact disc read-only memory (CD-ROM): a read-only medium that stores data in the form of an optical disc.
[0348] (8) Optical storage devices: such as DVDs, Blu-ray discs and other storage media based on optical principles.
[0349] (9) Magnetic storage devices: such as magnetic tapes, disks and other storage media based on magnetic principles.
[0350] (10) Any suitable combination of the above: for example, combining multiple storage media to meet different storage needs.
[0351] These computer-readable storage media can be used to store the program code and related data described in this application to support program execution and persistent data storage.
[0352] Specifically, according to embodiments of this application, the processes described in the flowcharts can be implemented as computer software programs. For example, embodiments of this application relate to a computer program product comprising a computer program carried on a non-transitory computer-readable medium. This computer program includes program code for executing the Spring-based multi-business field state transition method disclosed in this application. When the computer program is executed by a processing device, it can achieve the functions defined in the embodiments of this application.
[0353] While the foregoing discussion contains several specific implementation details, these details should not be construed as limiting the scope of this application. The above description is merely a preferred embodiment of this application and an explanation of the technical principles employed. Those skilled in the art should understand that the scope of this application is not limited to technical solutions formed by specific combinations of the above-described technical features. Furthermore, this application should also cover other technical solutions formed by any combination of the above-described technical features or their equivalents without departing from the foregoing disclosed concept.
[0354] Those skilled in the art should also understand that modifications can be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features, without departing from the spirit and scope of the technical solutions of the embodiments of this application. These modifications or substitutions will not cause the essence of the corresponding technical solutions to deviate from the core spirit and scope of the technical solutions of the embodiments of this application.< / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t>
Claims
1. A state transition method for multiple business fields based on Spring, characterized in that, The method includes: S1. Define an abstract state class for each business field, and declare a state transition method in the abstract state class. The state transition method is used to transition between different states. S2. Define multiple state implementation classes. Each state implementation class inherits from the abstract state class and implements the state transition method. In the state transition method, business logic is executed and the current state is switched to the target state. S3. Define a business state machine. The business state machine inherits from the abstract state class and holds instances of all state implementation classes. The business state machine maintains the current memory state instance and routes the state transition method to the current memory state instance for execution based on external calls. S4. Define a state machine container, which aggregates multiple business state machines from different business dimensions to achieve unified registration and management of multiple state machines; S5. Use reflection mechanism to obtain the target transition method and its parameter type of the target state machine, and dynamically call the target transition method through proxy technology to complete the state transition across state machines; S6. Provide a unified state transition interface. The business layer passes in the state switching instruction through the state transition interface to trigger the proxy technology to complete the state transition.
2. The method according to claim 1, characterized in that, The state implementation class is configured as a component bean of the Spring framework. It obtains the business state machine instance through the Spring dependency injection mechanism to perform state switching. In the state transition method, it calls the state update interface of the business state machine to realize the synchronous update of business data and business state machine.
3. The method according to claim 2, characterized in that, Before executing the state transition method, the business state machine calls the initialization method to load the corresponding state implementation class instance from the Spring container according to the business identifier, and sets the instance as the current memory state instance.
4. The method according to claim 2, characterized in that, The method further includes: dynamically obtaining Bean instances from the Spring container through a Spring context holder, wherein the Spring context holder implements the ApplicationContextAware interface to hold the application context.
5. The method according to claim 1, characterized in that, The abstract state class contains abstract methods for upward and downward state transitions, and the state machine type is constrained by generics.
6. The method according to claim 1, characterized in that, Before routing the state transition method, the business state machine executes pre-verification logic to determine whether the current memory state instance meets the transition conditions. If it does not meet the conditions, the transition is terminated.
7. The method according to claim 1, characterized in that, The target transition method and its parameter types for obtaining the target state machine using reflection mechanisms include: Obtain all public methods of the target state machine object; Iterate through the public methods and match the target method name; Extract the parameter type list of the matching method, and use the parameter type list to construct the reflection call.
8. The method according to claim 1, characterized in that, The state transition interface encapsulates multi-layered proxy calls. The business layer only interacts with the outermost utility class, which sequentially calls the state proxy and the state proxy executor to complete the final migration.
9. A state transition device for multiple business fields based on Spring, characterized in that, The apparatus, when running, implements the Spring-based multi-business-field state transition method as described in any one of claims 1-8, and the apparatus includes: The abstract state class definition module is used to define the abstract state class for each business field, and declare state transition methods in the abstract state class. The state transition methods are used to switch between different states. The state implementation class definition module is used to define multiple state implementation classes. The state implementation classes inherit from the abstract state class and implement the state transition method. The state transition method executes business logic and switches the current state to the target state. The business state machine definition module is used to define a business state machine. The business state machine inherits from the abstract state class and holds instances of all state implementation classes. The business state machine maintains the current memory state instance and routes the state transition method to the current memory state instance for execution based on external calls. The state machine container definition module is used to define state machine containers, which aggregate multiple business state machines from different business dimensions to achieve unified registration and management of multiple state machines; The state proxy execution module is used to obtain the target transition method and its parameter type of the target state machine using the reflection mechanism, and dynamically call the target transition method through proxy technology to complete the state transition across state machines; The state transition interface module provides a unified state transition interface. The business layer passes in state switching instructions through the state transition interface, which triggers the proxy technology to complete the state transition.
10. An electronic device, characterized in that, include: Memory and processor; Memory: Used to store computer programs; Processor: Used to execute the computer program to implement the steps of the Spring-based multi-business field state transition method as described in any one of claims 1-8.