Method and device for acquiring generic Bean in Spring Framework framework, equipment and medium

By introducing ParameterizedTypeReference and ResolvableType into the Spring Framework, new type-safe generic bean acquisition methods are added, which solves the type safety issue of generic bean acquisition in the framework and achieves accurate acquisition of generic beans and code compatibility.

CN120743286AActive Publication Date: 2025-10-03湖南长银五八消费金融股份有限公司
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202511232741.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-09-01
Publication Date
2025-10-03
Estimated Expiration
2045-09-01

AI Technical Summary

Technical Problem

In the existing technology, the Spring Framework cannot directly support the acquisition of generic type beans, resulting in type security issues and ClassCastException risks.

Method used

By introducing ParameterizedTypeReference and ResolvableType in the Spring Framework framework to retain generic information, and adding type-safe generic bean acquisition methods in the BeanFactory interface, including encapsulation and verification of generic type consistency, forced conversion or exception error handling.

Benefits of technology

This implements truly type-safe generic bean retrieval in the Spring Framework, avoids the risk of ClassCastException, and ensures accurate matching of generic types and code compatibility.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120743286A_ABST
    Figure CN120743286A_ABST
Patent Text Reader

Abstract

The invention discloses a method, a device, equipment and a medium for acquiring generic Bean in Spring Framework framework, and relates to the technical field of computers, the method comprises the following steps: acquiring an initial object from a preset container, and storing a generic type of a preset object acquisition method into a preset expected type variable; packaging the preset expected type variable to obtain a packaged object, and verifying whether the generic type of the packaged object is consistent with the generic type of the initial object; if yes, performing forced conversion on the initial object, and returning to the initial object; and if not, performing exception error reporting based on the original type of the initial object. Therefore, generic object acquisition with real type security can be realized in Spring Framework, the function blank of a framework in the aspect of generic dependency injection is filled, and the risk of type conversion errors caused by forced type conversion is thoroughly avoided.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a method, device, equipment and medium for obtaining generic beans in a Spring Framework. Background Art

[0002] The Spring Framework is a popular open source framework for the Java platform. One of its core functions is to serve as a lightweight container for managing application components (Beans), implementing inversion of control through dependency injection. The core interface of the container is the BeanFactory, which allows applications to obtain required Bean objects from the container by calling a series of getBean methods on that interface.

[0003] In the prior art, due to the type erasure mechanism of Java generics, generic parameters in a type-safe manner can only be concrete classes, not parameterized types. Therefore, the existing getBean method cannot directly support the retrieval of generic beans. In summary, how to safely retrieve generic bean objects in the Spring Framework remains an unresolved problem. Summary of the Invention

[0004] In view of this, the purpose of the present invention is to provide a method, apparatus, device, and medium for obtaining generic beans in the Spring Framework. This method can achieve truly type-safe generic bean acquisition in the Spring Framework, filling the functional gap in the framework's generic dependency injection capabilities and completely avoiding the ClassCastException risk caused by forced type conversion. The specific solution is as follows: In a first aspect, the present application discloses a method for obtaining a generic Bean in the Spring Framework, comprising: Obtain an initial Bean object from a preset container through a preset object acquisition method, and store the generic type of the preset object acquisition method into a preset expected type variable; the preset container is a Bean object management container in the Spring Framework framework; Encapsulate the preset expected type variable to obtain an encapsulated object, and verify whether the encapsulated object is consistent with the generic type of the initial Bean object; If the generic type of the initial Bean object is consistent with that of the encapsulated object, the initial Bean object is forcibly converted and the initial Bean object is returned; If the generic type of the initial Bean object is inconsistent with that of the encapsulated object, an exception error is reported based on the original type of the initial Bean object.

[0005] Optionally, obtaining the initial Bean object from a preset container by using a preset object obtaining method, and storing the generic type of the preset object obtaining method into a preset expected type variable, includes: Define an object acquisition method based on the preset Bean object name and generic type to obtain the preset object acquisition method; Acquire an initial Bean object corresponding to the preset Bean object name from a preset container based on the preset object acquisition method; The generic type is extracted from the preset object acquisition method, and the generic type of the preset object acquisition method is stored in a preset expected type variable.

[0006] Optionally, before encapsulating the preset expected type variable to obtain an encapsulated object and verifying whether the encapsulated object is consistent with the generic type of the initial Bean object, the method further includes: If the initial Bean object corresponding to the preset Bean object name does not exist in the preset container, an exception error indicating that the Bean object does not exist is reported.

[0007] Optionally, encapsulating the preset expected type variable to obtain an encapsulated object includes: The preset encapsulation method is called to parse the preset expected type variable to determine the generic parameter information corresponding to the preset expected type variable, and the preset expected type variable is encapsulated to obtain an encapsulated object containing complete generic parameter information.

[0008] Optionally, verifying whether the generic type of the encapsulated object is consistent with that of the initial Bean object includes: Determine whether the type of the initial Bean object belongs to the generic type corresponding to the encapsulated object or a subclass of the generic type corresponding to the encapsulated object through the dynamic type checking method in the preset encapsulation method.

[0009] Optionally, if the generic type of the initial Bean object is consistent with that of the encapsulated object, forcibly converting the initial Bean object and returning the initial Bean object includes: If the generic type of the initial Bean object is consistent with that of the encapsulated object, a forced conversion operation is performed on the initial Bean object based on the generic type of the preset object acquisition method, so as to perform a security notification on the current compiler for the initial Bean object through the forced conversion operation and return the initial Bean object.

[0010] Optionally, if the generic type of the initial Bean object is inconsistent with that of the encapsulated object, reporting an exception based on the original type of the initial Bean object includes: Determining the actual type of the encapsulated object, and if the actual type is inconsistent with the generic type, retaining the object information of the encapsulated object; The preset alarm template is filled with the object information to obtain target abnormal error reporting information, and abnormal error reporting is performed according to the target abnormal error reporting information.

[0011] In a second aspect, the present application discloses a device for obtaining generic beans in the Spring Framework, comprising: An object acquisition module is used to acquire an initial Bean object from a preset container through a preset object acquisition method, and store the generic type of the preset object acquisition method into a preset expected type variable; the preset container is a Bean object management container in the SpringFramework framework; A type verification module is used to encapsulate the preset expected type variable to obtain an encapsulated object, and verify whether the encapsulated object is consistent with the generic type of the initial Bean object; An object returning module, configured to forcibly convert the initial Bean object and return the initial Bean object if the generic type of the initial Bean object is consistent with that of the encapsulated object; The exception reporting module is used to report an exception based on the original type of the initial Bean object if the generic type of the initial Bean object is inconsistent with that of the encapsulated object.

[0012] In a third aspect, the present application discloses an electronic device, comprising: Memory, used to store computer programs; The processor is used to execute the computer program to implement the aforementioned method for obtaining generic beans in the Spring Framework.

[0013] In a fourth aspect, the present application discloses a computer-readable storage medium for storing a computer program, wherein the computer program, when executed by a processor, implements the aforementioned method for obtaining generic beans in the Spring Framework framework.

[0014] In this application, the initial Bean object can be obtained from the preset container through the preset object acquisition method, and the generic type of the preset object acquisition method is stored in the preset expected type variable; the preset container is the Bean object management container in the SpringFramework framework; the preset expected type variable is encapsulated to obtain the encapsulated object, and it is verified whether the generic type of the encapsulated object is consistent with that of the initial Bean object; if the generic type of the initial Bean object is consistent with that of the encapsulated object, the initial Bean object is forced to be converted and the initial Bean object is returned; if the generic type of the initial Bean object is inconsistent with that of the encapsulated object, an exception error is reported based on the original type of the initial Bean object.

[0015] It can be seen that through the method of this application, the generic type of the preset object acquisition method can be stored in the preset expected type variable, and the preset expected type variable can be encapsulated to ensure that the generic type stored in the preset expected type variable will not change. After obtaining the initial Bean object from the preset container through the preset object acquisition method, it is necessary to verify whether the generic type of the encapsulated object is consistent with that of the initial Bean object. If the types are consistent, the initial Bean object needs to be forced to be converted to avoid the compiler's warning, and then the initial Bean object can be returned to achieve the acquisition of the generic Bean. If the types are inconsistent, an exception error needs to be reported. In this way, through the method in this application, a truly type-safe generic Bean acquisition solution can be implemented in SpringFramework, filling the functional gap of the framework in generic dependency injection. Developers can accurately specify complete type information including generic parameters, completely avoiding the ClassCastException risk caused by forced type conversion. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are merely embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.

[0017] Figure 1This is a flow chart of a method for obtaining generic beans in the Spring Framework disclosed in this application; Figure 2 This is a schematic diagram of an object acquisition method disclosed in this application; Figure 3 This is a schematic diagram of an abnormal error report disclosed in this application; Figure 4 This is a sequence diagram of a method for obtaining a generic Bean in the Spring Framework disclosed in this application; Figure 5 This is a schematic diagram of the structure of a generic Bean acquisition device in the Spring Framework disclosed in this application; Figure 6 This is a structural diagram of an electronic device disclosed in this application. DETAILED DESCRIPTION

[0018] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.

[0019] In the prior art, due to the type erasure mechanism of Java generics, generic parameters in a type-safe manner can only be concrete classes, not parameterized types. Therefore, the existing getBean method cannot directly support the retrieval of generic beans. In summary, how to safely retrieve generic bean objects in the Spring Framework remains an unresolved problem.

[0020] In order to overcome the above technical problems, the present application discloses a method, apparatus, equipment and medium for obtaining generic beans in the Spring Framework framework, which can realize truly type-safe generic bean acquisition in the Spring Framework, fill the functional gap of the framework in generic dependency injection, and completely avoid the ClassCastException risk caused by forced type conversion.

[0021] See also Figure 1 As shown, the embodiment of the present invention discloses a method for obtaining a generic Bean in a Spring Framework framework, including: Step S11: obtain an initial Bean object from a preset container through a preset object acquisition method, and store the generic type of the preset object acquisition method into a preset expected type variable; the preset container is a Bean object management container in the Spring Framework framework.

[0022] In this embodiment, it is necessary to obtain the initial Bean object from the preset container through the preset object acquisition method, and then the generic type of the preset object acquisition method needs to be stored in the preset expected type variable. Specifically, Figure 2 As shown, first you need to define the object acquisition method based on the preset Bean object name and generic type to get the preset object acquisition method, that is, you need to add a method named " <t>T getBean(String name, ParameterizedTypeReference <t>typeReference)" and according to the above, <t>For generic methods, the return type is determined by ParameterizedTypeReference <t>Decision, then can obtain the initial Bean object corresponding to the preset Bean object name from the preset container according to the preset object acquisition method, such as Figure 2 As shown, it is necessary to pass the parameter name of the preset object acquisition method as a parameter, and call the existing getBean method of BeanFactory to obtain the Bean object according to the name. In addition, by calling getBean(name) to obtain the initial Bean object from the container, the type of the Bean object obtained is not limited, but the returned type is Object, which cannot directly guarantee generic safety. Then it is necessary to extract the generic type from the preset object acquisition method, and store the generic type of the preset object acquisition method into the preset expected type variable. Among them, it is necessary to pass the parameter typeReference of the preset object acquisition method, and use the getType method to convert the parameter typeReference type to ParameterizedTypeReference <t>The generic type T in the expected type variable is stored in the requiredType variable, where T can be User or List <user>This type with generics.

[0023] It should be noted that if the preset container does not contain an initial Bean object corresponding to the preset Bean object name, that is, there is no Bean object corresponding to the defined name, an exception error indicating that the Bean object does not exist will be reported.

[0024] In this way, the traditional getBean(name) method will fail due to type erasure in generic scenarios, such as List <string>and List <integer>Will be erased to List, so retaining the generic information through ParameterizedTypeReference can ensure that the returned Bean type matches accurately and avoid type erasure problems.

[0025] Step S12: Encapsulate the preset expected type variable to obtain an encapsulated object, and verify whether the encapsulated object is consistent with the generic type of the initial Bean object.

[0026] In this embodiment, it is necessary to encapsulate the preset expected type variable, and then compare whether the encapsulated object is consistent with the generic type of the initial Bean object. Specifically, it is necessary to call the preset encapsulation method to parse the preset expected type variable to determine the generic parameter information corresponding to the preset expected type variable, and encapsulate the preset expected type variable to obtain an encapsulated object containing complete generic parameter information. That is, Figure 2 As shown, requiredType needs to be encapsulated through the forType method of ResolvableType, and then the dynamic type checking method in the preset encapsulation method needs to be used to determine whether the type of the initial Bean object belongs to the generic type corresponding to the encapsulated object or the generic type subclass corresponding to the encapsulated object, and the dynamic type checking method is the isInstance method, that is, the isInstance method of the encapsulated object is used to determine whether the type of the bean is consistent with the requiredType type. It should be noted that encapsulating the preset expected type variable requiredType can ensure that the generic parameter information in requiredType is fully retained to ensure the correctness of subsequent generic type comparisons. Furthermore, during comparison, it is necessary to verify whether the actual type of the Bean is compatible with requiredType through isInstance. In this way, by encapsulating the preset expected type variable, it can be ensured that the generic parameter information is fully retained.

[0027] Step S13: If the generic type of the initial Bean object is consistent with that of the encapsulated object, the initial Bean object is forcibly converted and the initial Bean object is returned.

[0028] In this embodiment, if the generic type of the initial Bean object is consistent with that of the encapsulated object, the initial Bean object is forced to be converted based on the generic type of the preset object acquisition method, so as to notify the current compiler of the security of the initial Bean object through the forced conversion operation and return the initial Bean object. That is, if the generic type of the initial Bean object is consistent with that of the encapsulated object and the modified object, then the generic object collected is in accordance with expectations. Figure 2 As shown, it is necessary to cast through return (T) bean, and because the type check has been passed, the cast is safe and the initial Bean object is finally returned.

[0029] Step S14: If the generic type of the initial Bean object is inconsistent with the generic type of the encapsulated object, an exception error is reported based on the original type of the initial Bean object.

[0030] In this embodiment, if the generic type of the initial Bean object is inconsistent with that of the encapsulated object, an exception error report is required. Specifically, the actual type of the encapsulated object is determined. If the actual type is inconsistent with the generic type, the object information of the encapsulated object is retained. The preset alarm template is filled with the object information to obtain the target exception error report information, and an exception error report is performed based on the target exception error report information. It should be noted that, if Figure 3 As shown, the exception error is reported by defining an exception class "BeanNotOfRequiredTypeException" in the Spring framework. When obtaining a bean from the Spring container, if the actual type of the bean does not match the expected type, it is thrown. It should be further explained that the constructed exception class "BeanNotOfRequiredTypeException" inherits from "BeansException" and is an exception defined in the Spring framework. It is used to indicate errors that occur during bean operations. And the first constructed method in this class is publicBeanNotOfRequiredTypeException(String beanName, Class<?> requiredType, Class<?> The second constructor public BeanNotOfRequiredTypeException(String beanName, Type requiredType,Class actualType) receives three parameters: beanName (name of the Bean object), requiredType (required type), and actualType (actual running type of the Bean object). This constructor is mainly retained for backward compatibility, and it handles exception information by calling the second constructor. This maintains the backward compatibility of the Spring framework and greatly improves the applicability and security of the framework in complex generic scenarios. ...<?> actualType) provides a wider type compatibility Type interface as the expected type, not just the Class type, so that the method can handle generic types. Furthermore, when performing type judgment, if the instance type of requiredType is Class, it is directly assigned to the this.requiredType field. If it is an instance of ParameterizedType, that is, a generic type, its original type is obtained and assigned to the this.requiredType field. If neither is the case, an IllegalArgumentException exception is thrown, indicating an unsupported type. Finally, an error is reported through the template "IllegalArgumentException(requiredType + " is not supported")".

[0031] It can be seen that, through the method of the present application, the generic type of the preset object acquisition method can be stored in the preset expected type variable, and the preset expected type variable can be encapsulated to ensure that the generic type stored in the preset expected type variable will not change. After obtaining the initial Bean object from the preset container through the preset object acquisition method, it is necessary to verify whether the generic type of the encapsulated object is consistent with that of the initial Bean object. If the types are consistent, the initial Bean object needs to be forced to be converted to avoid the compiler's warning. Then the initial Bean object can be returned to achieve the acquisition of the generic Bean. If the types are inconsistent, an exception error needs to be reported. In this way, through the method in the present application, on the one hand, the generic information can be retained through ParameterizedTypeReference to ensure that the returned Bean type is accurately matched and avoid type erasure problems. On the other hand, without destroying the compatibility of existing code, it is only necessary to add a type-safe default method for generic bean acquisition to the BeanFactory interface without changing all implementation classes, thereby implementing a truly type-safe generic bean acquisition solution in the Spring Framework, filling the functional gap of the framework in generic dependency injection. Developers can accurately specify complete type information including generic parameters, completely avoiding the risk of ClassCastException caused by forced type conversion.

[0032] See also Figure 4 As shown in the figure, the embodiment of the present invention discloses a sequence diagram of a method for obtaining a generic bean in the Spring Framework framework. First, it is necessary to call the existing getBean method of BeanFactory with the parameter name as the parameter of the custom method to obtain the bean object according to the name. Then, it is necessary to use the getType method to convert the parameter typeReference to ParameterizedTypeReference through the parameter typeReference of the custom method. <t>The generic type T in the requiredType variable is then encapsulated using the forType method of ResolvableType. The dynamic type checking method within the encapsulation method then determines whether the type of the initial bean object matches the requiredType. If they do, the bean type is cast and returned; otherwise, a BeanNotOfRequiredTypeException is thrown.

[0033] See also Figure 5 As shown, an embodiment of the present invention discloses a device for obtaining a generic Bean in a Spring Framework, comprising: The object acquisition module 11 is used to obtain the initial Bean object from the preset container through a preset object acquisition method, and store the generic type of the preset object acquisition method into a preset expected type variable; the preset container is the Bean object management container in the SpringFramework framework; A type verification module 12 is used to encapsulate the preset expected type variable to obtain an encapsulated object, and verify whether the encapsulated object is consistent with the generic type of the initial Bean object; The object returning module 13 is configured to perform forced conversion on the initial Bean object and return the initial Bean object if the generic type of the initial Bean object is consistent with that of the encapsulated object; The exception reporting module 14 is configured to report an exception based on the original type of the initial Bean object if the generic type of the initial Bean object is inconsistent with that of the encapsulated object.

[0034] It can be seen that, through the method of the present application, the generic type of the preset object acquisition method can be stored in the preset expected type variable, and the preset expected type variable can be encapsulated to ensure that the generic type stored in the preset expected type variable will not change. After obtaining the initial Bean object from the preset container through the preset object acquisition method, it is necessary to verify whether the generic type of the encapsulated object is consistent with that of the initial Bean object. If the types are consistent, the initial Bean object needs to be forced to be converted to avoid the compiler's warning. Then the initial Bean object can be returned to achieve the acquisition of the generic Bean. If the types are inconsistent, an exception error needs to be reported. In this way, through the method in the present application, on the one hand, the generic information can be retained through ParameterizedTypeReference to ensure that the returned Bean type is accurately matched and avoid type erasure problems. On the other hand, without destroying the compatibility of existing code, it is only necessary to add a type-safe default method for generic bean acquisition to the BeanFactory interface without changing all implementation classes, thereby implementing a truly type-safe generic bean acquisition solution in the Spring Framework, filling the functional gap of the framework in generic dependency injection. Developers can accurately specify complete type information including generic parameters, completely avoiding the risk of ClassCastException caused by forced type conversion.

[0035] In some embodiments, the object acquisition module 11 may specifically include: A method definition unit is used to define an object acquisition method based on a preset Bean object name and a generic type to obtain a preset object acquisition method; An object acquisition unit, configured to acquire an initial Bean object corresponding to the preset Bean object name from a preset container based on the preset object acquisition method; A type extraction unit is used to extract the generic type from the preset object acquisition method and store the generic type of the preset object acquisition method into a preset expected type variable.

[0036] In some embodiments, the generic Bean acquisition device in the Spring Framework may further include: The first exception reporting unit is configured to report an exception indicating that the Bean object does not exist if the initial Bean object corresponding to the preset Bean object name does not exist in the preset container.

[0037] In some embodiments, the type verification module 12 may specifically include: The variable encapsulation unit is used to call a preset encapsulation method to parse the preset expected type variable to determine the generic parameter information corresponding to the preset expected type variable, and encapsulate the preset expected type variable to obtain an encapsulated object containing complete generic parameter information.

[0038] In some embodiments, the type verification module 12 may specifically include: A type verification unit is used to determine whether the type of the initial Bean object belongs to the generic type corresponding to the encapsulated object or a subclass of the generic type corresponding to the encapsulated object through the dynamic type checking method in the preset encapsulation method.

[0039] In some embodiments, the object returning module 13 may specifically include: An object returning unit is configured to perform a forced conversion operation on the initial Bean object based on the generic type of the preset object acquisition method if the generic type of the initial Bean object is consistent with that of the encapsulated object, so as to perform a security notification on the current compiler for the initial Bean object through the forced conversion operation, and return the initial Bean object.

[0040] In some embodiments, the abnormal error reporting module 14 may specifically include: an actual type determining unit, configured to determine the actual type of the encapsulated object, and retain object information of the encapsulated object if the actual type is inconsistent with the generic type; The second abnormal error reporting unit is used to fill a preset alarm template through the object information to obtain target abnormal error reporting information, and perform abnormal error reporting according to the target abnormal error reporting information.

[0041] Furthermore, the embodiment of the present application also discloses an electronic device, Figure 6 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content in the diagram should not be considered as any limitation to the scope of application of the present application.

[0042] Figure 6 This is a schematic diagram of the structure of an electronic device 20 provided in an embodiment of the present application. The electronic device 20 may specifically include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 is used to store a computer program, which is loaded and executed by the processor 21 to implement the relevant steps of the method for obtaining a generic bean in the Spring Framework framework disclosed in any of the aforementioned embodiments. In addition, the electronic device 20 in this embodiment may specifically be an electronic computer.

[0043] In this embodiment, the power supply 23 is used to provide operating voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and the external device. The communication protocol it follows is any communication protocol that can be applied to the technical solution of this application and is not specifically limited here; the input and output interface 25 is used to obtain external input data or output data to the outside world. Its specific interface type can be selected according to specific application needs and is not specifically limited here.

[0044] In addition, the memory 22, as a carrier for resource storage, can be a read-only memory, random access memory, disk or CD, etc. The resources stored thereon can include an operating system 221, a computer program 222, etc., and the storage method can be temporary storage or permanent storage.

[0045] The operating system 221 is used to manage and control the hardware devices on the electronic device 20 and the computer program 222, which can be Windows Server, Netware, Unix, Linux, etc. In addition to including a computer program capable of implementing the method for obtaining a generic bean in the Spring Framework framework executed by the electronic device 20 as disclosed in any of the aforementioned embodiments, the computer program 222 can further include computer programs capable of completing other specific tasks.

[0046] Furthermore, this application discloses a computer-readable storage medium for storing a computer program. When executed by a processor, the computer program implements the aforementioned method for obtaining generic beans in the Spring Framework. The specific steps of this method can be found in the corresponding content disclosed in the aforementioned embodiments and will not be further described here.

[0047] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from the other embodiments. Reference can be made to the descriptions of the identical or similar parts between the various embodiments. For the devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the descriptions are relatively simple, and the relevant parts can be referred to the descriptions of the methods.

[0048] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the above description has generally described the components and steps of each example according to their functions. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0049] The steps of the methods or algorithms described in conjunction with the embodiments disclosed herein may be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in random access memory (RAM), internal memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.

[0050] Finally, it should be noted that, in this document, relational terms such as first and second, etc., are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the terms "comprise," "include," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus comprising a set of elements includes not only those elements but also other elements not explicitly listed, or elements inherent to such process, method, article, or apparatus. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not preclude the presence of additional identical elements in the process, method, article, or apparatus comprising the element.

[0051] The above is a detailed introduction to the technical solution provided by the present application. Specific examples are used herein to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea. At the same time, for those skilled in the art, according to the ideas of the present application, there may be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.< / t> < / integer> < / string> < / user> < / t> < / t> < / t> < / t> < / t>

Claims

1. A method for obtaining generic beans in the Spring Framework, characterized in that: include: Obtain an initial Bean object from a preset container through a preset object acquisition method, and store the generic type of the preset object acquisition method into a preset expected type variable; The preset container is the Bean object management container in the Spring Framework framework; Encapsulate the preset expected type variable to obtain an encapsulated object, and verify whether the encapsulated object is consistent with the generic type of the initial Bean object; If the generic type of the initial Bean object is consistent with that of the encapsulated object, the initial Bean object is forcibly converted and the initial Bean object is returned; If the generic type of the initial Bean object is inconsistent with that of the encapsulated object, an exception error is reported based on the original type of the initial Bean object.

2. The method for obtaining generic beans in the Spring Framework according to claim 1, characterized in that: The method of obtaining an initial Bean object from a preset container by using a preset object obtaining method and storing the generic type of the preset object obtaining method into a preset expected type variable includes: Define an object acquisition method based on the preset Bean object name and generic type to obtain the preset object acquisition method; Acquire an initial Bean object corresponding to the preset Bean object name from a preset container based on the preset object acquisition method; The generic type is extracted from the preset object acquisition method, and the generic type of the preset object acquisition method is stored in a preset expected type variable.

3. The method for obtaining generic beans in the Spring Framework according to claim 2, characterized in that: Before encapsulating the preset expected type variable to obtain an encapsulated object and verifying whether the encapsulated object is consistent with the generic type of the initial Bean object, the method further includes: If the initial Bean object corresponding to the preset Bean object name does not exist in the preset container, an exception error indicating that the Bean object does not exist is reported.

4. The method for obtaining generic beans in the Spring Framework according to claim 1, characterized in that: The encapsulating the preset expected type variable to obtain an encapsulated object includes: The preset encapsulation method is called to parse the preset expected type variable to determine the generic parameter information corresponding to the preset expected type variable, and the preset expected type variable is encapsulated to obtain an encapsulated object containing complete generic parameter information.

5. The method for obtaining generic beans in the Spring Framework according to claim 4, characterized in that: The verifying whether the generic type of the encapsulated object is consistent with that of the initial Bean object includes: Determine whether the type of the initial Bean object belongs to the generic type corresponding to the encapsulated object or a subclass of the generic type corresponding to the encapsulated object through the dynamic type checking method in the preset encapsulation method.

6. The method for obtaining generic beans in the Spring Framework according to claim 1, characterized in that: If the generic type of the initial Bean object is consistent with that of the encapsulated object, the initial Bean object is forcibly converted and the initial Bean object is returned, including: If the generic type of the initial Bean object is consistent with that of the encapsulated object, a forced conversion operation is performed on the initial Bean object based on the generic type of the preset object acquisition method, so as to perform a security notification on the current compiler for the initial Bean object through the forced conversion operation and return the initial Bean object.

7. The method for obtaining generic beans in the Spring Framework according to any one of claims 1 to 6, characterized in that: If the generic type of the initial Bean object is inconsistent with that of the encapsulated object, an exception error is reported based on the original type of the initial Bean object, including: Determining the actual type of the encapsulated object, and if the actual type is inconsistent with the generic type, retaining the object information of the encapsulated object; The preset alarm template is filled with the object information to obtain target abnormal error reporting information, and abnormal error reporting is performed according to the target abnormal error reporting information.

8. A device for obtaining generic beans in the Spring Framework, characterized in that: include: An object acquisition module is used to obtain an initial Bean object from a preset container through a preset object acquisition method, and store the generic type of the preset object acquisition method into a preset expected type variable; The preset container is the Bean object management container in the SpringFramework framework; A type verification module is used to encapsulate the preset expected type variable to obtain an encapsulated object, and verify whether the encapsulated object is consistent with the generic type of the initial Bean object; An object returning module, configured to forcibly convert the initial Bean object and return the initial Bean object if the generic type of the initial Bean object is consistent with that of the encapsulated object; The exception reporting module is used to report an exception based on the original type of the initial Bean object if the generic type of the initial Bean object is inconsistent with that of the encapsulated object.

9. An electronic device, characterized in that: include: Memory, used to store computer programs; A processor is used to execute the computer program to implement the method for obtaining generic beans in the Spring Framework framework as described in any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that Used to store a computer program, wherein when the computer program is executed by a processor, it implements the method for obtaining a generic Bean in the Spring Framework framework according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Configuration information processing method and device, electronic equipment and storage medium

    CN114706619A

  • HTTP (Hyper Text Transport Protocol) service proxy method for distributed service platform

    CN115643247A

  • Type sensitive pointer analysis method and device for generic programming

    CN115658458A

  • Transform a data object in a meta model based on a generic type

    US20180074797A1

  • Centralized data transformation in a multi-tenant computing environment

    US20230281339A1