Methods, devices, and media for obtaining generic beans in the Spring Framework

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

Patent Information

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

AI Technical Summary

Technical Problem

[0003]现有技术中,由于Java泛型的类型擦除机制,类型安全方式的泛型参数只能为具体类,而不能是参数化类型

Benefits of technology

[0034]本申请中,可以通过预设对象获取方法从预设容器中获取初始Bean对象,并将所述预设对象获取方法的泛型类型存入预设期望类型变量;所述预设容器为SpringFramework框架中的Bean对象管理容器;对所述预设期望类型变量进行封装,以得到封装后对象,并验证所述封装后对象与所述初始Bean对象的泛型类型是否一致;若所述初始Bean对象与所述封装后对象的泛型类型一致,则对所述初始Bean对象进行强制转换,并返回所述初始Bean对象;若所述初始Bean对象与所述封装后对象的泛型类型不一致,则基于所述初始Bean对象的原始类型进行异常报错。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120743286B_ABST
    Figure CN120743286B_ABST
Patent Text Reader

Abstract

This application discloses methods, apparatus, devices, and media for obtaining generic beans in the Spring Framework, relating to the field of computer technology. The methods include: obtaining an initial object from a predefined container and storing the generic type of the predefined object acquisition method in a predefined expected type variable; encapsulating the predefined expected type variable to obtain an encapsulated object, and verifying whether the generic type of the encapsulated object is consistent with that of the initial object; if consistent, performing a type cast on the initial object and returning the initial object; if inconsistent, throwing an exception based on the original type of the initial object. This enables truly type-safe acquisition of generic objects in the Spring Framework, filling the functional gap in generic dependency injection within the framework and completely avoiding the risk of type conversion errors caused by forced type casting.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a method, apparatus, device, and medium for obtaining generic beans in the Spring Framework. Background Technology

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

[0003] In existing technologies, due to Java's type erasure mechanism, type-safe generic parameters can only be concrete classes, not parameterized types. Therefore, the existing `getBean` method cannot directly support retrieving generic type beans. In summary, how to securely retrieve generic bean objects within the Spring Framework remains a problem to be solved. Summary of the Invention

[0004] In view of this, the purpose of this invention is to provide a method, apparatus, device, and medium for obtaining generic beans in the Spring Framework, enabling truly type-safe generic bean retrieval within the Spring Framework, filling the functional gap in generic dependency injection within the framework, and completely avoiding the ClassCastException risk caused by forced type conversion. The specific solution is as follows:

[0005] Firstly, this application discloses a method for obtaining generic beans in the Spring Framework, including:

[0006] The initial Bean object is obtained from the preset container through the preset object retrieval method, and the generic type of the preset object retrieval method is stored in the preset expected type variable; the preset container is the Bean object management container in the Spring Framework.

[0007] The preset expected type variable is encapsulated to obtain an encapsulated object, and the generic type of the encapsulated object is verified to be consistent with that of the initial Bean object.

[0008] If the initial Bean object has the same generic type as the encapsulated object, then the initial Bean object is forcibly cast and the initial Bean object is returned.

[0009] If the initial Bean object and the generic type of the encapsulated object are inconsistent, an exception will be reported based on the original type of the initial Bean object.

[0010] Optionally, the step of obtaining the initial Bean object from the preset container through the preset object acquisition method and storing the generic type of the preset object acquisition method into a preset expected type variable includes:

[0011] Based on the preset Bean object name and generic type definition, the object retrieval method is obtained to obtain the preset object retrieval method;

[0012] Based on the preset object acquisition method, an initial Bean object corresponding to the preset Bean object name is obtained from the preset container;

[0013] 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.

[0014] Optionally, before encapsulating the preset expected type variable to obtain the encapsulated object and verifying whether the encapsulated object is consistent with the generic type of the initial Bean object, the method further includes:

[0015] If the preset container does not contain an initial Bean object corresponding to the preset Bean object name, an exception error will be reported that the Bean object does not exist.

[0016] Optionally, the step of encapsulating the preset expected type variable to obtain the encapsulated object includes:

[0017] The preset encapsulation method is invoked 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.

[0018] Optionally, verifying whether the generic type of the encapsulated object is consistent with that of the initial Bean object includes:

[0019] The dynamic type checking method in the preset encapsulation method determines 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.

[0020] Optionally, the step of performing a type cast on the initial Bean object and returning the initial Bean object if the initial Bean object has the same generic type as the encapsulated object includes:

[0021] If the initial Bean object has the same generic type as the encapsulated object, then the initial Bean object is forcibly cast based on the generic type of the preset object acquisition method, so as to provide the current compiler with a safety notification for the initial Bean object through the casting operation, and then the initial Bean object is returned.

[0022] Optionally, if the initial Bean object and the generic type of the encapsulated object are inconsistent, an exception is reported based on the original type of the initial Bean object, including:

[0023] Determine the actual type of the encapsulated object; if the actual type is inconsistent with the generic type, retain the object information of the encapsulated object.

[0024] The object information is used to fill the preset alarm template to obtain the target abnormal error information, and an abnormal error is reported based on the target abnormal error information.

[0025] Secondly, this application discloses a device for obtaining generic beans in the Spring Framework, comprising:

[0026] The object retrieval module is used to retrieve an initial Bean object from a preset container through a preset object retrieval method, and store the generic type of the preset object retrieval method into a preset expected type variable; the preset container is a Bean object management container in the Spring Framework.

[0027] The type verification module is used to encapsulate the preset expected type variable to obtain the encapsulated object, and to verify whether the encapsulated object is consistent with the generic type of the initial Bean object;

[0028] The object return module is used to perform a type cast 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.

[0029] 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.

[0030] Thirdly, this application discloses an electronic device, including:

[0031] Memory, used to store computer programs;

[0032] A processor is used to execute the computer program to implement the methods for obtaining generic beans in the Spring Framework as described above.

[0033] Fourthly, this 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.

[0034] In this application, an initial Bean object can be obtained from a preset container through a preset object acquisition method, and the generic type of the preset object acquisition method is stored in a preset expected type variable; the preset container is a Bean object management container in the Spring Framework; the preset expected type variable is encapsulated to obtain an 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 forcibly cast and returned; if the generic type of the initial Bean object is inconsistent with that of the encapsulated object, an exception is reported based on the original type of the initial Bean object.

[0035] Therefore, the method described in this application allows the generic type of the predefined object retrieval method to be stored in a predefined expected type variable. This predefined expected type variable is then encapsulated to ensure that the generic type stored in it remains unchanged. After retrieving the initial Bean object from the predefined container using the predefined object retrieval method, it's necessary to verify whether the generic type of the encapsulated object matches that of the initial Bean object. If the types match, a type cast is performed on the initial Bean object to avoid compiler warnings, and then the initial Bean object can be returned to achieve the retrieval of the generic Bean. If the types do not match, an exception is thrown. In this way, the method described in this application enables a truly type-safe generic Bean retrieval solution in the Spring Framework, filling a functional gap in the framework's generic dependency injection capabilities. Developers can precisely specify complete type information, including generic parameters, completely avoiding the ClassCastException risk caused by forced type casting. Attached Figure Description

[0036] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.

[0037] Figure 1 This application discloses a flowchart of a method for obtaining a generic bean in the Spring Framework.

[0038] Figure 2 This is a schematic diagram of an object acquisition method disclosed in this application;

[0039] Figure 3 This is a schematic diagram of an abnormal error message disclosed in this application;

[0040] Figure 4 This is a sequence diagram of a method for obtaining a generic bean in the Spring Framework disclosed in this application;

[0041] Figure 5 This is a schematic diagram of the structure of a generic bean acquisition device in the Spring Framework disclosed in this application;

[0042] Figure 6 This is a structural diagram of an electronic device disclosed in this application. Detailed Implementation

[0043] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0044] In existing technologies, due to Java's type erasure mechanism, type-safe generic parameters can only be concrete classes, not parameterized types. Therefore, the existing `getBean` method cannot directly support retrieving generic type beans. In summary, how to securely retrieve generic bean objects within the Spring Framework remains a problem to be solved.

[0045] To overcome the aforementioned technical problems, this application discloses a method, apparatus, device, and medium for obtaining generic beans in the Spring Framework, which enables truly type-safe generic bean acquisition in the Spring Framework, fills the functional gap in the framework for generic dependency injection, and completely avoids the risk of ClassCastException caused by forced type conversion.

[0046] See Figure 1 As shown in the figure, this embodiment of the invention discloses a method for obtaining generic beans in the Spring Framework, including:

[0047] Step S11: Obtain the initial Bean object from the preset container through the preset object acquisition method, and store the generic type of the preset object acquisition method into the preset expected type variable; the preset container is the Bean object management container in the Spring Framework.

[0048] In this embodiment, it is necessary to obtain the initial Bean object from the preset container through the preset object retrieval method, and then store the generic type of the preset object retrieval method into a preset expected type variable. Specifically, for example... Figure 2 As shown, firstly, an object retrieval method needs to be defined based on the preset Bean object name and generic type to obtain the preset object retrieval method. That is, a method named "" needs to be added to the org.springframework.beans.factory.BeanFactory interface provided by the Spring Framework. <t>T getBean(String name, ParameterizedTypeReference <t>The default method of "typeReference"), and as can be seen from the above, <t>For generic methods, the return type is determined by ParameterizedTypeReference. <t>The decision can then be made, and the initial Bean object corresponding to the preset Bean object name can be retrieved from the preset container according to the preset object retrieval method, such as... Figure 2 As shown, the method needs to retrieve the `name` parameter from the predefined object as an argument, and then call the existing `getBean` method of `BeanFactory` to retrieve the Bean object by name. Furthermore, calling `getBean(name)` retrieves the initial Bean object from the container; the type of the retrieved Bean object is not limited, but it returns an `Object` type, which cannot directly guarantee generic safety. Then, the generic type needs to be extracted from the predefined object retrieval method and stored in a predefined expected type variable. Specifically, the `typeReference` parameter needs to be retrieved from the predefined object retrieval method, and the `getType` method needs to be used to convert the `typeReference` parameter to `ParameterizedTypeReference`. <t>The generic type T is stored in the predefined expected type variable, i.e., the requiredType variable, where T can be either User or List. <user>This type has generics.

[0049] It should be noted that if the default container does not contain an initial Bean object corresponding to the default Bean object name, that is, if there is no Bean object corresponding to the defined name, then an exception error should be reported for the Bean object not existing.

[0050] In this way, the traditional getBean(name) method will fail in generic scenarios due to type erasure, such as with List. <string>and List <integer>All of them will be erased to a List. Therefore, by preserving the generic information through ParameterizedTypeReference, we can ensure that the returned Bean type matches exactly and avoid type erasure issues.

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

[0052] In this embodiment, it is necessary to encapsulate the preset expected type variable, and then compare whether the generic type of the encapsulated object is consistent with that 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 then encapsulate the preset expected type variable to obtain an encapsulated object containing complete generic parameter information. That is, as shown... Figure 2 As shown, `requiredType` needs to be encapsulated using the `forType` method of `ResolvableType`. Then, the dynamic type checking method within the pre-defined encapsulation method needs 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. The dynamic type checking method is the `isInstance` method; that is, the `isInstance` method of the encapsulated object is used to determine whether the bean's type is consistent with `requiredType`. It should be noted that encapsulating the pre-defined expected type variable `requiredType` ensures that the generic parameter information in `requiredType` is completely preserved, ensuring the correctness of subsequent generic type comparisons. Furthermore, during the comparison, `isInstance` is used to verify whether the actual type of the Bean is compatible with `requiredType`. In this way, by encapsulating the pre-defined expected type variable, the complete preservation of generic parameter information is guaranteed.

[0053] Step S13: If the initial Bean object has the same generic type as the encapsulated object, then the initial Bean object is forcibly cast and the initial Bean object is returned.

[0054] In this embodiment, if the initial Bean object and the encapsulated object have the same generic type, a type casting operation is performed on the initial Bean object based on the generic type of the preset object acquisition method. This type casting operation serves as a safety notification to the current compiler regarding the initial Bean object, and the initial Bean object is returned. That is, if the initial Bean object, the encapsulated object, and the encapsulated object all have the same generic type, it indicates that the collected generic object meets expectations. Figure 2 As shown, a type cast is required via `return (T) bean`, and since the type check has passed, the cast is safe, ultimately returning the initial Bean object.

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

[0056] In this embodiment, if the initial Bean object and the generic type of the encapsulated object are inconsistent, an exception needs to be reported. 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 object information is used to fill a preset alarm template to obtain the target exception information, and an exception is reported based on the target exception information. It should be noted that, as Figure 3 As shown, the exception is thrown by defining a Spring framework exception class "BeanNotOfRequiredTypeException". This exception is thrown when retrieving a Bean from the Spring container if the actual type of the Bean does not match the expected type. It's worth noting that the constructed exception class "BeanNotOfRequiredTypeException" inherits from "BeansException", a Spring framework exception used to represent errors that occur during Bean operations. Furthermore, the first constructor method of this class is `public BeanNotOfRequiredTypeException(String beanName, Class...)`.<?> requiredType, Class<?> The `actualType` constructor takes three arguments: `beanName` (the name of the bean object), `requiredType` (the required type), and `actualType` (the actual runtime type of the bean object). This constructor is primarily reserved for backward compatibility, and it handles exceptions by calling the second constructor. This maintains the backward compatibility of the Spring framework and significantly improves the framework's applicability and security in complex generic scenarios. The second constructor is `public BeanNotOfRequiredTypeException(String beanName, Type requiredType, Class...)`.<?> The `actualType` method provides broader type compatibility with the `Type` interface as the expected type, not just `Class`, allowing it to handle generic types. Furthermore, during type checking, if the instance of `requiredType` is of type `Class`, it is directly assigned to the `this.requiredType` field. If it is an instance of `ParameterizedType` (i.e., generic), its primitive type is obtained and assigned to the `this.requiredType` field. If neither is true, an `IllegalArgumentException` is thrown, indicating an unsupported type. Finally, an error is reported using the template "IllegalArgumentException(requiredType + " is not supported")".

[0057] Therefore, the method of this application allows the generic type of the predefined object retrieval method to be stored in a predefined expected type variable, and this variable is encapsulated to ensure that the generic type stored in the predefined expected type variable does not change. After retrieving the initial Bean object from the predefined container using the predefined object retrieval method, it is necessary to verify whether the generic type of the encapsulated object matches that of the initial Bean object. If the types match, a type cast of the initial Bean object is required to avoid compiler warnings, and then the initial Bean object can be returned to achieve the retrieval of the generic Bean. If the types do not match, an exception is thrown. In this way, the method of this application, on the one hand, preserves generic information through ParameterizedTypeReference, ensuring that the returned Bean type matches precisely and avoiding type erasure issues. On the other hand, without compromising the compatibility of existing code, only a type-safe generic bean retrieval default method needs to be added to the BeanFactory interface, without changing all the implementation classes. This enables a truly type-safe generic bean retrieval solution in the Spring Framework, filling the functional gap in the framework for generic dependency injection. Developers can accurately specify complete type information, including generic parameters, completely avoiding the risk of ClassCastException caused by forced type conversion.

[0058] See Figure 4 As shown in the figure, this embodiment of the invention discloses a sequence diagram of a method for obtaining a generic Bean in the Spring Framework. First, the `getBean` method of the BeanFactory is called with the `name` parameter of the custom method as the parameter to obtain the Bean object by name. Then, the `getType` method is used to convert the `typeReference` parameter of the custom method to `ParameterizedTypeReference`. <t>The generic type T is stored in the `requiredType` variable. Then, the `requiredType` needs to be wrapped using the `forType` method of `ResolvableType`. Next, the dynamic type checking method in the predefined wrapper method needs to determine whether the type of the initial Bean object matches the type of `requiredType`. Finally, if they match, the bean's type is forcibly cast, and the bean is returned; otherwise, a `BeanNotOfRequiredTypeException` is thrown.

[0059] See Figure 5 As shown in the figure, this embodiment of the invention discloses a device for obtaining generic beans in the Spring Framework, including:

[0060] The object acquisition module 11 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 a Bean object management container in the Spring Framework.

[0061] The type verification module 12 is used to encapsulate the preset expected type variable to obtain the encapsulated object, and to verify whether the encapsulated object is consistent with the generic type of the initial Bean object.

[0062] The object return module 13 is used to perform a type cast on the initial Bean object and return the initial Bean object if the initial Bean object has the same generic type as the encapsulated object.

[0063] The exception reporting module 14 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.

[0064] Therefore, the method of this application allows the generic type of the predefined object retrieval method to be stored in a predefined expected type variable, and this variable is encapsulated to ensure that the generic type stored in the predefined expected type variable does not change. After retrieving the initial Bean object from the predefined container using the predefined object retrieval method, it is necessary to verify whether the generic type of the encapsulated object matches that of the initial Bean object. If the types match, a type cast of the initial Bean object is required to avoid compiler warnings, and then the initial Bean object can be returned to achieve the retrieval of the generic Bean. If the types do not match, an exception is thrown. In this way, the method of this application, on the one hand, preserves generic information through ParameterizedTypeReference, ensuring that the returned Bean type matches precisely and avoiding type erasure issues. On the other hand, without compromising the compatibility of existing code, only a type-safe generic bean retrieval default method needs to be added to the BeanFactory interface, without changing all the implementation classes. This enables a truly type-safe generic bean retrieval solution in the Spring Framework, filling the functional gap in the framework for generic dependency injection. Developers can accurately specify complete type information, including generic parameters, completely avoiding the risk of ClassCastException caused by forced type conversion.

[0065] In some embodiments, the object acquisition module 11 may specifically include:

[0066] The method definition unit is used to define an object retrieval method based on a preset Bean object name and generic type, so as to obtain the preset object retrieval method;

[0067] An object retrieval unit is used to retrieve an initial Bean object corresponding to the preset Bean object name from a preset container based on the preset object retrieval method.

[0068] The 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.

[0069] In some embodiments, the device for obtaining generic beans in the Spring Framework may further include:

[0070] The first exception reporting unit is used to report an exception if there is no initial Bean object in the preset container that corresponds to the preset Bean object name, indicating that the Bean object does not exist.

[0071] In some embodiments, the type verification module 12 may specifically include:

[0072] The variable encapsulation unit is used to call a preset encapsulation method to parse the preset expected type variable, 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.

[0073] In some embodiments, the type verification module 12 may specifically include:

[0074] The 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 the generic type subclass corresponding to the encapsulated object through the dynamic type checking method in the preset encapsulation method.

[0075] In some embodiments, the object return module 13 may specifically include:

[0076] The object return unit is used to perform a type casting operation on the initial Bean object based on the generic type of the preset object acquisition method if the initial Bean object has the same generic type as the encapsulated object, so as to provide a safety notification to the current compiler for the initial Bean object through the type casting operation, and return the initial Bean object.

[0077] In some embodiments, the error reporting module 14 may specifically include:

[0078] The actual type determination unit is used to determine the actual type of the encapsulated object. If the actual type is inconsistent with the generic type, the object information of the encapsulated object is retained.

[0079] The second error reporting unit is used to fill the preset alarm template with the object information to obtain the target error reporting information, and to report the error according to the target error reporting information.

[0080] Furthermore, embodiments of this application also disclose an electronic device, Figure 6 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content of the diagram should not be construed as limiting the scope of this application.

[0081] Figure 6 This is a schematic diagram of the structure of an electronic device 20 provided in an embodiment of this application. Specifically, the electronic device 20 may 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 stores a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the generic Bean acquisition method of the Spring Framework disclosed in any of the foregoing embodiments. Alternatively, the electronic device 20 in this embodiment may specifically be a computer.

[0082] 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 external devices, and the communication protocol it follows can be any communication protocol applicable to the technical solution of this application, and is not specifically limited here; the input / output interface 25 is used to acquire external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs, and is not specifically limited here.

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

[0084] The operating system 221 is used to manage and control the various hardware devices on the electronic device 20 and the computer program 222, which may be Windows Server, Netware, Unix, Linux, etc. In addition to including a computer program capable of performing the method for obtaining generic beans in the Spring Framework disclosed in any of the foregoing embodiments, the computer program 222 may further include computer programs capable of performing other specific tasks.

[0085] Furthermore, this application also discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, it 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 foregoing embodiments, and will not be repeated here.

[0086] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to in the method section.

[0087] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can 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.

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

[0089] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0090] The technical solutions provided in this application have been described in detail above. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the methods and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this 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: The initial Bean object is 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 a Bean object management container in the Spring Framework. The preset expected type variable is encapsulated to obtain an encapsulated object, and the generic type of the encapsulated object is verified to be consistent with that of the initial Bean object. If the initial Bean object has the same generic type as the encapsulated object, then the initial Bean object is forcibly cast and the initial Bean object is returned. If the initial Bean object and the generic type of the encapsulated object are inconsistent, an exception error will be reported based on the original type of the initial Bean object; The step of obtaining an initial Bean object from a preset container through a preset object acquisition method and storing the generic type of the preset object acquisition method into a preset expected type variable includes: Based on the preset Bean object name and generic type definition, the object retrieval method is obtained to obtain the preset object retrieval method; Based on the preset object acquisition method, an initial Bean object corresponding to the preset Bean object name is obtained from the preset container; 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; Wherein, if the initial Bean object and the generic type of the encapsulated object are consistent, then the initial Bean object is forcibly cast and the initial Bean object is returned, including: If the initial Bean object has the same generic type as the encapsulated object, then the initial Bean object is forcibly cast based on the generic type of the preset object acquisition method, so as to provide the current compiler with a safety notification for the initial Bean object through the casting operation, and then the initial Bean object is returned.

2. The method for obtaining generic beans in the Spring Framework according to claim 1, characterized in that, Before encapsulating the preset expected type variable to obtain the 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 preset container does not contain an initial Bean object corresponding to the preset Bean object name, an exception error will be reported that the Bean object does not exist.

3. The method for obtaining generic beans in the Spring Framework according to claim 1, characterized in that, The encapsulation of the preset expected type variable to obtain the encapsulated object includes: The preset encapsulation method is invoked 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.

4. The method for obtaining generic beans in the Spring Framework according to claim 3, characterized in that, The verification of whether the generic type of the encapsulated object is consistent with that of the initial Bean object includes: The dynamic type checking method in the preset encapsulation method determines 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.

5. The method for obtaining generic beans in the Spring Framework according to any one of claims 1 to 4, characterized in that, If the initial Bean object and the generic type of the encapsulated object are inconsistent, an exception is reported based on the original type of the initial Bean object, including: Determine the actual type of the encapsulated object; if the actual type is inconsistent with the generic type, retain the object information of the encapsulated object. The object information is used to fill the preset alarm template to obtain the target abnormal error information, and an abnormal error is reported based on the target abnormal error information.

6. A device for obtaining generic beans in the Spring Framework, characterized in that, include: The 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 a Bean object management container in the Spring Framework. The type verification module is used to encapsulate the preset expected type variable to obtain the encapsulated object, and to verify whether the encapsulated object is consistent with the generic type of the initial Bean object; The object return module is used to perform a type cast 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 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. The object acquisition module includes: The method definition unit is used to define an object retrieval method based on a preset Bean object name and generic type, so as to obtain the preset object retrieval method; An object retrieval unit is used to retrieve an initial Bean object corresponding to the preset Bean object name from a preset container based on the preset object retrieval 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; The object return module (13) includes: The object return unit is used to perform a type casting operation on the initial Bean object based on the generic type of the preset object acquisition method if the initial Bean object has the same generic type as the encapsulated object, so as to provide a safety notification to the current compiler for the initial Bean object through the type casting operation, and return the initial Bean object.

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

8. A computer-readable storage medium, characterized in that, Used to store a computer program, wherein the computer program, when executed by a processor, implements the method for obtaining a generic bean in the Spring Framework as described in any one of claims 1 to 5.

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