Initialization method, device, electronic device and computer-readable storage medium

By removing the dependency between the listener class and the third-party client class in Spring Cloud and adopting serial initialization, the loop exception problem caused by the registration of FeignClient and ApplicationListener at the same time is solved, ensuring the normal initialization of Spring Cloud.

CN113568673BActive Publication Date: 2024-08-30BEIJING KUANGSHI TECHNOLOGY CO LTD
View PDF 9 Cites 0 Cited by

Patent Information

Application Number
CN202110650696.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-06-10
Publication Date
2024-08-30
Estimated Expiration
2041-06-10

AI Technical Summary

Technical Problem

When initializing Spring Cloud, the FeignClient and ApplicationListener were registered in the same configuration class, causing a loop exception problem, causing the initialization to fail.

Method used

By removing the dependency between the listener class and the third-party client class, serial initialization is used to initialize, ensuring that the listener class initializes the third-party client class after completion of initialization, thereby avoiding cyclic initialization.

Benefits of technology

This effectively avoids the problem of the listener class being initialized cyclically at the same time, ensuring the successful initialization of Spring Cloud.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113568673B
    Figure CN113568673B
Patent Text Reader

Abstract

The present invention relates to an initialization method, device, electronic device and computer-readable storage medium, the method comprising: obtaining an initialization instruction for initializing a microservice framework Spring Cloud, wherein the Spring Cloud includes an uninitialized third-party client class and an uninitialized listener class; responding to the initialization instruction, releasing the dependency between the listener class and the third-party client class, and serially initializing the third-party client class and the listener class; after completing the initialization of the third-party client class and the listener class, re-establishing the dependency between the two. By means of the method, the problem of a circular exception caused by registering a third-party client class and a listener class in the same configuration class when initializing Spring Cloud can be solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application belongs to the field of microservices, and specifically relates to an initialization method, device, electronic device, and computer-readable storage medium. Background Art

[0002] When developing based on the microservice framework Spring Cloud, you need to initialize Spring Cloud first. During the initialization process, you need to create a container AnnotationConfigEmbeddedWebApplicationContext corresponding to Spring Cloud, referred to as the first container, and execute the refresh() method on the context corresponding to the first container.

[0003] When the refresh() method is executed, the registerListeners() method is called to register all listener classes in Spring Cloud, that is, the ApplicationListener class. The listener class is generally configured in the class annotated with @Configuration in the @Bean way. Therefore, if you want to initialize the listener class, you must require the class annotated with @Configuration to be initialized before the listener class.

[0004] However, in the prior art, when initializing the class annotated with @Configuration, since it depends on the third-party client class, namely the FeignClient class, the FeignClient class needs to be initialized first. When initializing the FeignClient class, it will automatically create another container ApplicationContext, referred to as the second container, and execute the refresh() method on the context corresponding to the second container. At this time, the registerListeners() method will be called again, resulting in the registerListeners() method being called at least twice in the same time period, and all listener classes are initialized, which in turn causes the listener class to be loaded cyclically, causing the overall initialization of Spring Cloud to be abnormal, resulting in initialization failure. Summary of the invention

[0005] In view of this, the purpose of the present application is to provide an initialization method, device, electronic device and computer-readable storage medium for solving the circular exception problem caused by registering FeignClient and ApplicationListener in the same configuration class when initializing Spring Cloud.

[0006] The embodiment of the present application is implemented as follows:

[0007] In a first aspect, an embodiment of the present application provides an initialization method, the method comprising: obtaining an initialization instruction for initializing a microservice framework Spring Cloud, wherein the Spring Cloud includes an uninitialized third-party client class and an uninitialized listener class; responding to the initialization instruction, releasing the dependency between the listener class and the third-party client class, and serially initializing the third-party client class and the listener class.

[0008] In the above process, since the initialization method of the third-party client class and the listener class is serial initialization, that is, one starts to initialize after the other is completed, therefore, in the above process of initializing the listener class, the third-party client class will not be actively initialized, thereby avoiding the need to initialize the third-party client class in advance when initializing the listener class and executing the refresh() method again to initialize the listener class, thereby avoiding the listener class from being cyclically initialized at the same time and avoiding initialization failure.

[0009] In combination with the embodiment of the first aspect, in a possible implementation manner, releasing the dependency between the listener class and the third-party client class includes: releasing the dependency between the listener class and the third-party client class by deleting the @Autowired annotation of the third-party client class.

[0010] In combination with the embodiment of the first aspect, in a possible implementation manner, when performing the serial initialization, it includes: first initializing the listener class, and after the listener class completes the initialization, initializing the third-party client class.

[0011] In combination with the embodiment of the first aspect, in a possible implementation manner, the listener class is initialized first, and after the listener class is initialized, the third-party client class is initialized, including: when executing the refresh() method, the registerListeners() method is first called to initialize the listener class, and after the call is completed, the finishBeanFactoryInitialization() method is called to initialize the third-party client class in non-lazy loading mode.

[0012] In combination with the embodiment of the first aspect, in a possible implementation manner, the method further includes: after completing the initialization of the third-party client class and the listener class, re-establishing the dependency relationship between the two.

[0013] In combination with the embodiment of the first aspect, in a possible implementation manner, before re-establishing the dependency relationship between the two, the method further includes: obtaining a container that has been initialized in the Spring Cloud;

[0014] Correspondingly, the re-establishing of the dependency relationship between the two includes: establishing an association relationship between the listener class and the third-party client class by dependently searching for an instance of the third-party client class in the container.

[0015] In combination with the embodiment of the first aspect, in a possible implementation, the searching for an instance of the third-party client class by dependency in the container includes: calling the getBean() method when borrowing the container by executing the afterSingletonsInstantiated() method, thereby realizing the searching for an instance of the third-party client class by dependency in the container.

[0016] In combination with the embodiment of the first aspect, in a possible implementation manner, the method is used to initialize the Edgware version of the Spring Cloud.

[0017] In a second aspect, an embodiment of the present application provides an initialization device, which includes: an acquisition module and a response module.

[0018] An acquisition module is used to acquire initialization instructions for initializing the microservice framework Spring Cloud, including uninitialized third-party client classes and uninitialized listener classes in the Spring Cloud;

[0019] The response module is used to respond to the initialization instruction, release the dependency between the listener class and the third-party client class, and serially initialize the third-party client class and the listener class.

[0020] In combination with the embodiment of the second aspect, in a possible implementation manner, the response module is used to release the dependency between the listener class and the third-party client class by deleting the @Autowired annotation of the third-party client class.

[0021] In combination with the embodiment of the second aspect, in a possible implementation manner, the response module is used to initialize the listener class first, and then initialize the third-party client class after the listener class completes initialization.

[0022] In combination with the second aspect embodiment, in a possible implementation, the response module is used to first call the registerListeners() method to initialize the listener class when executing the refresh() method, and after the call is completed, call the finishBeanFactoryInitialization() method to initialize the third-party client class in non-lazy loading mode.

[0023] In combination with the second aspect embodiment, in a possible implementation manner, the device also includes a reconstruction module, which is used to re-establish the dependency relationship between the third-party client class and the listener class after completing the initialization of the third-party client class and the listener class.

[0024] In combination with the embodiment of the second aspect, in a possible implementation manner, the acquisition module is further used to acquire a container that has been initialized in the Spring Cloud;

[0025] Correspondingly, the reconstruction module is used to establish an association relationship between the listener class and the third-party client class by relying on searching for an instance of the third-party client class in the container.

[0026] In combination with the second aspect embodiment, in a possible implementation, the reconstruction module is used to call the getBean() method when borrowing the container by executing the afterSingletonsInstantiated() method, so as to realize the search for the instance of the third-party client class by dependency in the container.

[0027] In combination with the embodiment of the second aspect, in a possible implementation manner, the method is used to initialize the Edgware version of the Spring Cloud.

[0028] In a third aspect, an embodiment of the present application further provides an electronic device, comprising: a memory and a processor, wherein the memory and the processor are connected; the memory is used to store programs; the processor calls the program stored in the memory to execute the method provided in the above-mentioned first aspect embodiment and / or any possible implementation method in combination with the first aspect embodiment.

[0029] In a fourth aspect, an embodiment of the present application further provides a non-volatile computer-readable storage medium (hereinafter referred to as computer-readable storage medium), on which a computer program is stored. When the computer program is run by a computer, it executes the method provided by the above-mentioned first aspect embodiment and / or any possible implementation method in combination with the first aspect embodiment.

[0030] Other features and advantages of the present application will be described in the following description, and partly become apparent from the description, or understood by implementing the embodiments of the present application. The purpose and other advantages of the present application can be realized and obtained by the structures specifically pointed out in the written description and the drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0031] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can also be obtained based on these drawings without creative work. As shown in the drawings, the above and other purposes, features and advantages of the present application will be clearer. The same reference numerals indicate the same parts in all the drawings. The drawings are not deliberately scaled to the actual size, and the focus is on showing the main purpose of the present application.

[0032] Figure 1 A flowchart of an initialization method provided in an embodiment of the present application is shown.

[0033] Figure 2 A structural block diagram of an initialization device provided in an embodiment of the present application is shown.

[0034] Figure 3 A schematic structural diagram of an electronic device provided in an embodiment of the present application is shown.

[0035] Icons: 100 - electronic device; 110 - processor; 120 - memory; 400 - initialization device; 410 - acquisition module; 420 - response module. DETAILED DESCRIPTION

[0036] The technical solutions in the embodiments of the present application will be described below in conjunction with the drawings in the embodiments of the present application.

[0037] It should be noted that similar numbers and letters represent similar items in the following figures, so once an item is defined in one figure, it does not need to be further defined and explained in the subsequent figures. At the same time, in the description of this application, relational terms such as "first", "second", etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between these entities or operations. Moreover, the term "comprise" or any other variant thereof is intended to cover non-exclusive inclusion, so that the process, method, article or equipment including a series of elements includes not only those elements, but also includes other elements that are not clearly listed, or also includes elements inherent to such process, method, article or equipment. In the absence of more restrictions, the elements defined by the statement "comprise one..." do not exclude the existence of other identical elements in the process, method, article or equipment including the elements.

[0038] Furthermore, the term "and / or" in this application is merely a term used to describe the association relationship between associated objects, indicating that three relationships may exist. For example, A and / or B may represent three situations: A exists alone, A and B exist at the same time, and B exists alone.

[0039] In addition, with respect to the prior art, when a third-party client class and a listener class coexist in a @Configuration class, the defects in the initialization method of Spring Cloud (which will cause a loop exception) are the results obtained by the applicant after practice and careful research. Therefore, the process of discovering the above defects and the solutions proposed in the embodiments of this application for the above defects below should be recognized as the applicant's contribution to this application.

[0040] In order to solve the above problems, the embodiments of the present application provide an initialization method, an apparatus, an electronic device and a computer-readable storage medium, which are used to solve the circular exception problem caused by registering FeignClient and ApplicationListener in the same configuration class when initializing Spring Cloud.

[0041] This technology can be implemented using corresponding software, hardware, or a combination of software and hardware.

[0042] The following is an introduction to the initialization method provided by this application.

[0043] Please refer to Figure 1 , the embodiment of the present application provides an initialization method for initializing the microservice framework SpringCloud. Figure 1 The steps included in this method are described.

[0044] Step S110: obtaining an initialization instruction for initializing the microservice framework Spring Cloud, wherein the Spring Cloud includes an uninitialized third-party client class and an uninitialized listener class.

[0045] As a microservice framework, Spring Cloud has various classes pre-stored in it, such as listener classes (i.e. ApplicationListener classes), third-party client classes (i.e. FeignClient classes), etc.

[0046] The third-party client class is used to provide local variable parameters for the listener class, so that the listener class can complete specific business operations based on the local variable parameter values ​​provided by the third-party client during actual operation.

[0047] Of course, before Spring Cloud is initialized, all the classes stored in it are in an uninitialized state.

[0048] When you need to develop based on Spring Cloud, you need to ensure that the various classes in Spring Cloud are instantiated. Therefore, before developing based on Spring Cloud, you need to initialize Spring Cloud to instantiate the various classes stored in it.

[0049] Step S120: responding to the initialization instruction, releasing the dependency between the listener class and the third-party client class, and serially initializing the third-party client class and the listener class.

[0050] From the above description, it can be seen that in the existing initialization scheme, when creating the container AnnotationConfigEmbeddedWebApplicationContext (referred to as the first container) corresponding to Spring Cloud, the refresh() method must be executed to initialize all listener classes first.

[0051] When initializing the listener class, the corresponding third-party client class needs to be injected (@Autowired), which leads to early initialization of the third-party client class. During the initialization of the third-party client class, it will create a container context (ApplicationContext) and execute the refresh() method to initialize all listener classes, which leads to the listener class being registered in a loop. At this time, the progress of the initialized listener class is inconsistent with the progress of the previously initialized listener class, that is, at the same time, there are two progress of initializing the listener class, which leads to the listener class being loaded in a loop, thus causing the initialization failure.

[0052] In an embodiment of the present application, the dependency on the third-party client class when initializing the listener class can be released, so that when initializing the listener class, it is no longer necessary to rely on the third-party client class, that is, there is no need to initialize the third-party client class first, thereby staggering the initialization time of the third-party client class and the listener class, thereby avoiding the listener class from being cyclically initialized at the same time, thereby solving the problem of the above-mentioned listener class being cyclically loaded.

[0053] Optionally, in order to achieve the above effect, when obtaining the initialization instruction for initializing the microservice framework Spring Cloud and responding to it, the dependency on the third-party client class when initializing the listener class can be released.

[0054] Generally speaking, third-party client classes are used in Spring Cloud applications in the form of @Autowired annotations, and the existence of @Autowired annotations will lead to dependency injection of third-party client classes, that is, it will cause FeignClientFactoryBean to create the third-party client class FeignClient. The ApplicationContext (the second container in the previous article) created executes the refresh() method and then calls the registerListeners() method. Therefore, in some implementations, the dependency on the third-party client class when initializing the listener class can be removed by deleting the @Autowired annotation of the third-party client class.

[0055] After removing the dependency on the third-party client class when initializing the listener class, the container AnnotationConfigEmbeddedWebApplicationContext corresponding to Spring Cloud, that is, the first container, can be initialized according to the existing process.

[0056] During the process of initializing the first container, a refresh() method is executed on the context corresponding to the first container.

[0057] It is worth pointing out that when executing the refresh() method, a variety of different methods will be called, such as the registerListeners() method, the finishBeanFactoryInitialization() method, etc. Among them, due to the mechanism set by Spring Cloud, when executing the refresh() method, the registerListeners() method is executed before the finishBeanFactoryInitialization() method, and the finishBeanFactoryInitialization() method will not start to execute until the registerListeners() method is executed.

[0058] Due to the existence of the above mechanism, when the refresh() method is executed on the context corresponding to the first container, registerListeners() first registers all listener classes, namely, ApplicationListener classes, and manages all registered listener classes in the first container of Spring Cloud.

[0059] After all listener classes are registered, the refresh() method calls the finishBeanFactoryInitialization() method to register class objects in non-lazy loading mode, that is, third-party client classes.

[0060] Among them, if a class object is in non-lazy loading mode, the container needs to load it during initialization; if a class object is in lazy loading mode, the container does not need to load the class object during initialization, but starts loading when the class object is needed.

[0061] It is worth pointing out that, for a class object, if no special declaration is made for it, the class object is in non-lazy loading mode by default, and in the embodiment of the present application, all third-party client classes are in non-lazy loading mode.

[0062] In the above initialization process, since the dependency on the third-party client class when initializing the listener class has been removed, the third-party client class will not be initialized actively when the listener class is initialized, but will be initialized after the listener class is initialized. In other words, the initialization method of the third-party client class and the listener class is changed from parallel initialization with a loop to serial initialization.

[0063] Of course, in some implementations, if some feasible methods can be used to change the order of calling the registerListeners() and finishBeanFactoryInitialization() methods when executing the refresh() method, and it does not affect the initialization of the listener class and the third-party client class, then the listener class can be initialized after the third-party client class is initialized, that is, as long as the initialization method of both is serial initialization, it will be fine.

[0064] Since the third-party client class will not be actively initialized during the above process of initializing the listener class, it is possible to avoid the need to initialize the third-party client class in advance when initializing the listener class and execute the refresh() method again to initialize the listener class, thereby preventing the listener class from being cyclically initialized at the same time and avoiding initialization failure.

[0065] Step S130: after completing the initialization of the third-party client class and the listener class, re-establishing the dependency relationship between the two.

[0066] As mentioned earlier, the third-party client class is used to provide local variable parameters for the listener class so that the listener class can complete specific business operations based on the local variable parameter values ​​provided by the third-party client during actual operation. Therefore, in normal initialization, there is a dependency relationship, that is, an association, between the third-party client class and the listener class.

[0067] In an embodiment of the present application, after forcibly releasing the dependency relationship between the listener class and the third-party client class, although the problem of initialization failure caused by the listener class being cyclically initialized at the same time can be avoided, since there is no association between the two, the listener class will be unable to call the third-party client class to complete subsequent business operations. Therefore, in some implementations, when faced with some actual business scenarios, the dependency relationship between the two can be re-established after completing the initialization of the third-party client class and the listener class.

[0068] Optionally, before re-establishing the dependency relationship between the two, you can also first obtain the container that has been initialized in Spring Cloud, that is, the first container, and the process is as follows.

[0069] Before initializing the first container, you can obtain the ApplicationContextAware interface corresponding to the first container and override the setApplicationContext() method of the interface.

[0070] When the first container is initialized and the refresh() method is executed on the context corresponding to the first container, the prepareBeanFactory() method is called to inject the ApplicationContextAwareProcessor processor into the BeanFactory. The processor calls the postProcessBeforeInitialization() method before the Bean is instantiated to package the Spring Bean. In the postProcessBeforeInitialization() method, the setApplicationContext() method of the ApplicationContextAware interface is executed to pass the context of the created first container to the current Bean object, thereby obtaining the first container. Of course, this part of the content is prior art and will not be repeated here.

[0071] For the entire Spring Cloud, the first container is the largest parent container, and the second container corresponding to the third-party client class included in Spring Cloud is the child container of the parent container. When the third-party client class included in Spring Cloud is initialized, it can obtain the contents of each child container included in the parent container by calling the largest parent container.

[0072] After the first container is obtained, an instance of the third-party client class may be searched in the first container so as to establish an association relationship between the listener class and the third-party client class.

[0073] Among them, when relying on finding an instance of a third-party client class in the first container, you can borrow the first container when executing the afterSingletonsInstantiated() method, and call the getBean() method when borrowing the first container, so as to realize relying on finding an instance of the third-party client class in the first container.

[0074] Of course, it is worth pointing out that the initialization method involved in the embodiment of the present application is applicable to initializing the Edgware version of SpringCloud, and can also be applicable to initializing other versions of Spring Cloud.

[0075] An initialization method provided in an embodiment of the present application, when initializing Spring Cloud, removes the dependency on the third-party client class when initializing the listener class, and changes the initialization method of the third-party client class and the listener class from parallel initialization to serial initialization, and then re-establishes the dependency between the two after completing the initialization of the third-party client class and the listener class. Since the initialization method of the third-party client class and the listener class is serial initialization, that is, one starts to initialize after the other is completed, therefore, in the above-mentioned process of initializing the listener class, the third-party client class will not be actively initialized, thereby avoiding the need to initialize the third-party client class in advance when initializing the listener class and executing the refresh() method again to initialize the listener class, thereby avoiding the listener class being cyclically initialized at the same time and avoiding initialization failure.

[0076] The embodiment of the present application also provides a specific initial method, including steps 1 to 4.

[0077] Step 1: Remove @Autowired on FeignClient

[0078] The existence of the @Autowired annotation will bring about dependency injection of FeignClient, that is, it will cause FeignClientFactoryBean to create the ApplicationContext when creating the FeignClient, and then call the registerListeners() method. Therefore, the premise of solving this problem is to ensure that there is no dependency injection. Deleting @Autowired can avoid dependency injection.

[0079] Dependency injection means that when Spring initializes a Bean, if there are variables annotated with @Autowired or @Inject in the Bean, it will search by name or type in the Spring container, get the corresponding Bean (if not, it will automatically initialize and register it), and assign the Bean to the variable.

[0080] Step 2: @Configuration class implements ApplicationContextAware interface and overrides the setApplicationContext() method of the interface

[0081] In the refresh() method (initialization lisener) of ApplicationContext, the prepareBeanFactory() method is called to inject the ApplicationContextAwareProcessor processor into the BeanFactory. The processor calls the postProcessBeforeInitialization() method before the Bean is instantiated to package the Spring Bean. In this method, the setApplicationContext() method of ApplicationContextAware is executed to pass the created ApplicationContext context to the current Bean object. Therefore, the main purpose of this step is to get the context object (ApplicationContext) when the Spring container is loaded.

[0082] Step 3: @Configuration class implements SmartInitializingSingleton interface and overrides the afterSingletonsInstantiated() method of the interface

[0083] In the refresh() method of ApplicationContext, the registerListeners() method is called to load all ApplicationListener classes and register them in the Spring context for management. After that, the finishBeanFactoryInitialization() method will be called for class objects in non-lazy loading mode, which are also registered in the Spring context for management. Therefore, as long as FeignClient is not loaded during the call of the registerListeners() method, after the registerListeners() method is called, the loading of FeignClient (initialization of the client) is actively completed during the call of the finishBeanFactoryInitialization() method, because the time sequence of the registerListeners() method and the finishBeanFactoryInitialization() method calls is the order of execution in sequence. In this way, the simultaneous loading of FeignClient and ApplicationListener can be fundamentally avoided, solving the problem of circular references when the Spring container is loaded.

[0084] Step 4: In the afterSingletonsInstantiated() method, borrow the applicationContext from step 2 and rely on finding FeignClient

[0085] In step 2, the context object (ApplicationContext) of the current Spring container has been obtained, so you can directly use the getBean() method to find dependencies and obtain FeignClient.

[0086] Through steps 1 to 4, FeignClient and ApplicationListener are loaded at the same time, but ApplicationListener is loaded first, and then FeignClient is actively searched after the listener is loaded. This fundamentally solves the circular reference exception caused by the simultaneous loading of FeignClient and ApplicationListener, and solves the problem of Spring container initialization.

[0087] Please refer to Figure 2 The embodiment of the present application further provides an initialization device 400 , which may include: an acquisition module 410 and a response module 420 .

[0088] The acquisition module 410 is used to acquire the initialization instruction for initializing the microservice framework Spring Cloud, which includes an uninitialized third-party client class and an uninitialized listener class;

[0089] The response module 420 is used to respond to the initialization instruction, release the dependency between the listener class and the third-party client class, and serially initialize the third-party client class and the listener class.

[0090] In a possible implementation, the response module 420 is used to release the dependency between the listener class and the third-party client class by deleting the @Autowired annotation of the third-party client class.

[0091] In a possible implementation, the response module 420 is configured to initialize the listener class first, and then initialize the third-party client class after the listener class is initialized.

[0092] In a possible implementation, the response module 420 is used to first call the registerListeners() method to initialize the listener class when executing the refresh() method, and after the call is completed, call the finishBeanFactoryInitialization() method to initialize the third-party client class in non-lazy loading mode.

[0093] In a possible implementation manner, the device further includes a reconstruction module, which is used to re-establish the dependency relationship between the third-party client class and the listener class after completing the initialization of the second-party client class and the listener class.

[0094] In a possible implementation, the acquisition module 410 is further used to acquire the initialized container in the Spring Cloud;

[0095] Correspondingly, the reconstruction module is used to establish an association relationship between the listener class and the third-party client class by relying on searching for an instance of the third-party client class in the container.

[0096] In a possible implementation, the reconstruction module is used to call the getBean() method when borrowing the container by executing the afterSingletonsInstantiated() method, so as to find the instance of the third-party client class by dependency in the container.

[0097] In a possible implementation, the method is used to initialize the Edgware version of the Spring Cloud.

[0098] The initialization device 400 provided in the embodiment of the present application has the same implementation principle and technical effects as those of the aforementioned method embodiment. For the sake of brief description, for matters not mentioned in the device embodiment, reference may be made to the corresponding contents in the aforementioned method embodiment.

[0099] In addition, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a computer, the steps included in the above-mentioned initialization method are executed.

[0100] In addition, please refer to Figure 3 The electronic device 100 used to implement the initialization method and device of the embodiment of the present application is described.

[0101] The Spring Cloud framework can be deployed on the electronic device 100 to complete the development of microservices.

[0102] Optionally, the electronic device 100 may be, but not limited to, a personal computer (PC), a tablet computer, a mobile Internet device (MID), a server, etc. The server may be, but not limited to, a network server, a database server, a cloud server, etc.

[0103] The electronic device 100 may include: a processor 110 and a memory 120 .

[0104] It should be noted that Figure 3 The components and structures of the electronic device 100 shown are merely exemplary and non-limiting. The electronic device 100 may also have other components and structures as required.

[0105] The processor 110, the memory 120, and other components that may appear in the electronic device 100 are directly or indirectly electrically connected to each other to achieve data transmission or interaction. For example, the processor 110, the memory 120, and other components that may appear may be electrically connected to each other via one or more communication buses or signal lines.

[0106] The memory 120 is used to store programs, for example, a program corresponding to the initialization method mentioned above or an initialization device mentioned above. Optionally, when the initialization device is stored in the memory 120, the initialization device includes at least one software function module that can be stored in the memory 120 in the form of software or firmware.

[0107] Optionally, the software function modules included in the initialization device may also be embedded in the operating system (OS) of the electronic device 100 .

[0108] The processor 110 is used to execute executable modules stored in the memory 120, such as software function modules or computer programs included in the initialization device. After receiving the execution instruction, the processor 110 can execute the computer program, for example, to execute: obtaining the initialization instruction for initializing the microservice framework Spring Cloud, which includes an uninitialized third-party client class and an uninitialized listener class; responding to the initialization instruction, releasing the dependency between the listener class and the third-party client class, and serially initializing the third-party client class and the listener class; after completing the initialization of the third-party client class and the listener class, re-establishing the dependency between the two.

[0109] Of course, the method disclosed in any embodiment of the present application can be applied to the processor 110 or implemented by the processor 110.

[0110] In summary, the initialization method, device, electronic device and computer-readable storage medium proposed in the embodiments of the present invention, when initializing Spring Cloud, remove the dependency on the third-party client class when initializing the listener class, and change the initialization method of the third-party client class and the listener class from parallel initialization to serial initialization, and then re-establish the dependency between the two after completing the initialization of the third-party client class and the listener class. Since the initialization method of the third-party client class and the listener class is serial initialization, that is, one starts to initialize after the other is completed, therefore, in the above-mentioned process of initializing the listener class, the third-party client class will not be actively initialized, thereby avoiding the need to initialize the third-party client class in advance when initializing the listener class and executing the refresh() method again to initialize the listener class, thereby avoiding the listener class being cyclically initialized at the same time and avoiding initialization failure.

[0111] It should be noted that the various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.

[0112] In several embodiments provided in the present application, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely schematic. For example, the flowcharts and block diagrams in the accompanying drawings show the possible architecture, functions and operations of the devices, methods and computer program products according to multiple embodiments of the present application. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of a code, and the module, a program segment or a part of a code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flowchart, and the combination of boxes in the block diagram and / or flowchart can be implemented with a dedicated hardware-based system that performs a specified function or action, or can be implemented with a combination of dedicated hardware and computer instructions.

[0113] In addition, the functional modules in the various embodiments of the present application may be integrated together to form an independent part, or each module may exist separately, or two or more modules may be integrated to form an independent part.

[0114] If the functions are implemented in the form of software function modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a laptop, a server, or a network device, etc.) to perform all or part of the steps of the methods described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0115] The above description is only a specific implementation manner of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application.

Claims

1. An initialization method, It is characterized in that The method comprises: Obtaining an initialization instruction for initializing a microservice framework Spring Cloud, wherein the microservice framework Spring Cloud includes an uninitialized third-party client class and an uninitialized listener class; In response to the initialization instruction, the dependency relationship between the listener class and the third-party client class is released, and the third-party client class and the listener class are serially initialized; The releasing of the dependency between the listener class and the third-party client class includes: The dependency between the listener class and the third-party client class is removed by deleting the @Autowired annotation of the third-party client class. Performing the serial initialization includes: When executing the refresh() method, the registerListeners() method is first called to initialize the listener class, and after the call is completed, the finishBeanFactoryInitialization() method is called to initialize the third-party client class in non-lazy loading mode.

2. The method according to claim 1, It is characterized in that The method further comprises: After completing the initialization of the third-party client class and the listener class, the dependency relationship between the two is re-established.

3. The method according to claim 2, It is characterized in that Before re-establishing the dependency relationship between the two, the method further includes: Get the container that has been initialized in the Spring Cloud; Accordingly, re-establishing the dependency relationship between the two includes: An association relationship is established between the listener class and the third-party client class by relying on searching for an instance of the third-party client class in the container.

4. The method according to claim 3, It is characterized in that The step of searching for an instance of the third-party client class by dependency in the container includes: By calling the getBean() method when the container is borrowed by executing the afterSingletonsInstantiated() method, the instance of the third-party client class is searched through dependency in the container.

5. The method according to any one of claims 1 to 4, It is characterized in that The method is used to initialize the Edgware version of Spring Cloud.

6. An initialization device, It is characterized in that The device comprises: An acquisition module is used to acquire initialization instructions for initializing the microservice framework Spring Cloud, including uninitialized third-party client classes and uninitialized listener classes in the Spring Cloud; A response module, used to respond to the initialization instruction, release the dependency between the listener class and the third-party client class, and serially initialize the third-party client class and the listener class; The response module is specifically used for: The dependency between the listener class and the third-party client class is removed by deleting the @Autowired annotation of the third-party client class. When executing the refresh() method, the registerListeners() method is first called to initialize the listener class, and after the call is completed, the finishBeanFactoryInitialization() method is called to initialize the third-party client class in non-lazy loading mode.

7. An electronic device, It is characterized in that include: A memory and a processor, wherein the memory and the processor are connected; The memory is used to store programs; The processor calls the program stored in the memory to execute the method according to any one of claims 1 to 5.

8. A computer-readable storage medium, It is characterized in that A computer program is stored thereon, and when the computer program is executed by a computer, the method according to any one of claims 1 to 5 is executed.

Citation Information

Patent Citations

  • Calculation mission processing method and device

    CN104793924A

  • Multi-chip initialization method and multi-chip initialization apparatus

    CN108399932A

  • Micro-service optimization method, device and electronic equipment

    CN109587272A

  • Method and system for dynamically modifying micro-service configuration information and storage medium

    CN112363775A

  • Application program modularization method and device, application program component calling method and device, application program component initialization method and device

    CN112698888A