CN115904551A - Method and device for dynamically replacing Class in spring by bean - Google Patents

Method and device for dynamically replacing Class in spring by bean Download PDF

Info

Publication number
CN115904551A
CN115904551A CN202211450678.0A CN202211450678A CN115904551A CN 115904551 A CN115904551 A CN 115904551A CN 202211450678 A CN202211450678 A CN 202211450678A CN 115904551 A CN115904551 A CN 115904551A
Authority
CN
China
Prior art keywords
file
class
replaced
spring
inherited
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202211450678.0A
Other languages
Chinese (zh)
Inventor
张湉湉
李西林
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Zhongke Shitong Hengqi Beijing Technology Co ltd
Original Assignee
Zhongke Shitong Hengqi Beijing Technology Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Zhongke Shitong Hengqi Beijing Technology Co ltd filed Critical Zhongke Shitong Hengqi Beijing Technology Co ltd
Priority to CN202211450678.0A priority Critical patent/CN115904551A/en
Publication of CN115904551A publication Critical patent/CN115904551A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Stored Programmes (AREA)

Abstract

The invention provides a method and a device for dynamically replacing Class in spring by bean, wherein the method comprises the following steps: acquiring an update file, and inquiring a corresponding replaced byte code file according to the update file; acquiring first attribute information of the replaced byte code file by using a reflection mechanism; inheriting the first attribute information using an update file, and replacing the replaced bytecode file using the inherited update file. The invention realizes the dynamic substitution of bean for the Class file.

Description

Method and device for dynamically replacing Class in spring by bean
Technical Field
The invention relates to the technical field of operation and maintenance, in particular to a method for dynamically replacing Class in spring by bean.
Background
In the development and production of java systems, the situation of file emergency modification is frequently encountered, but online services cannot be stopped to modify, or a data source needs to be switched without stopping service, as is known, java per se can be operated after being compiled, bytecode files which can be understood by the JVM can be obtained after the java is compiled, namely Class files, and the Class files exist in a memory (ram) or do not exist in storage. Therefore, the problem of dynamically modifying the java file is how to dynamically replace the original bean in the spring container to realize the switching of the code logic under the condition that the JVM is running.
Disclosure of Invention
The technical problem to be solved by the invention is how to provide a method and a device for dynamically replacing Class in spring by bean so as to solve the problems existing in the prior art.
The invention solves the technical problems through the following technical means:
the invention provides a method for dynamically replacing Class in spring by bean, which comprises the following steps:
acquiring an update file, and inquiring a corresponding replaced byte code file according to the update file;
acquiring first attribute information of the replaced byte code file by using a reflection mechanism;
inheriting the first attribute information using an update file, and replacing the replaced bytecode file using the inherited update file.
Optionally, the obtaining the first attribute information of the replaced bytecode file by using the reflection mechanism includes:
and calling getBeanInfo to acquire first attribute information of the replaced byte code file in the spring container.
Optionally, the updating the file inherits the first attribute information, and includes:
the first attribute information of the replaced bytecode file is inherited by setSuperClass.
Optionally, the replacing the replaced bytecode file with the inherited update file includes:
generating a new Class file by using a makeClass method in a javasist frame;
inheriting the first attribute information of the replaced byte code file by the new Class file to obtain an inherited update file;
replacing the replaced byte code file with the inherited update file by using a replayBeanByClass method;
and removing the replaced byte code file through the factory class of the DefaultListableBeanFactory, and registering the inherited update file into a spring container.
Optionally, the method further includes:
acquiring the information of the referenced class in the replaced bytecode file by using javasist;
for the information of the reference class, judging whether the reference class exists by using an AntotatedElementUtils tool class;
if yes, deleting the Mapping method in the Controller, and registering the inherited update file into the Controller.
The invention also provides a device for dynamically replacing Class in spring by bean, which comprises:
the first acquisition module is used for acquiring the update file and inquiring a corresponding replaced byte code file according to the update file;
the second acquisition module is used for acquiring the first attribute information of the replaced byte code file by using a reflection mechanism;
and the updating module is used for inheriting the first attribute information by using the updating file and replacing the replaced byte code file by using the inherited updating file.
Optionally, the second obtaining module is configured to:
and calling getBeanInfo to acquire first attribute information of the replaced byte code file in the spring container.
Optionally, the update module is configured to:
the first attribute information of the replaced bytecode file is inherited by setSuperClass.
Optionally, the update module is configured to:
generating a new Class file by using a makeClass method in a javasist frame;
inheriting the first attribute information of the replaced byte code file by the new Class file to obtain an inherited update file;
replacing the replaced byte code file with the inherited update file by using a replayBeanByClass method;
and removing the replaced byte code file through the factory class of the DefaultListableBeanFactory, and registering the inherited update file into a spring container.
Optionally, the apparatus further comprises:
the third acquisition module is used for acquiring the information of the referred class in the replaced bytecode file by using javasist;
the judging module is used for judging whether the reference class exists by using an AntotatedElementUtils tool class aiming at the information of the reference class and triggering the registration module under the condition that the judging result of the judging module is yes;
and the registration module is used for deleting the Mapping method in the Controller and registering the inherited update file into the Controller.
The invention has the advantages that:
the invention replaces the Class file under the spring rule, and is characterized in that a reflection mechanism is combined under the spring frame, so that the Class replacement during the running period of the spring frame is realized, and the bean dynamic replacement of the Class file is realized.
Drawings
FIG. 1 is a schematic flow chart of a method for dynamically replacing a Class in spring by a bean according to an embodiment of the present invention;
FIG. 2 is a schematic diagram illustrating a principle of a method for dynamically replacing a Class in spring by using a bean according to an embodiment of the present invention;
fig. 3 is a schematic diagram of a relationship between a java source program and a Class file in a method for dynamically replacing a Class in spring by using a bean according to an embodiment of the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the embodiments of the present invention, and it is obvious that the described embodiments are some embodiments of the present invention, but not all embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
Example 1
Fig. 1 is a schematic flow diagram of a method for dynamically replacing a Class in a spring by using a bean according to an embodiment of the present invention, and fig. 2 is a schematic principle diagram of the method for dynamically replacing a Class in a spring by using a bean according to an embodiment of the present invention, as shown in fig. 1 and fig. 2, the method includes:
s101 (not shown in the figure): and acquiring the update file, and inquiring the corresponding replaced byte code file according to the update file.
The Bean is the most basic element managed by the spring container, and the Bean file and the Class file in the spring container are equivalent, so that the Bean file needing to be replaced, namely the update file, is uploaded through a console page, and the replaced byte code file can be searched through the middle method replaceBean of the springmvc according to the name of the replaced byte code file.
S102 (not shown in the figure): and acquiring the first attribute information of the replaced byte code file by using a reflection mechanism.
Specifically, the bean in the original spring container, that is, the first attribute information of the replaced bytecode file, can be obtained from getBeanInfo in the spring HookContext class, so as to inherit the original information.
The reflection (reflection) mechanism of Java means that in the running state of a program, a class to which any object belongs can be known, member variables and methods of any class can be known, and attributes and methods of any object can be called. The function of dynamically acquiring program information and dynamically calling objects is called as a reflection mechanism of Java language, and is the key of the dynamic language.
Fig. 3 is a schematic diagram of a relationship between a java source program and a Class file in a method for dynamically replacing a Class in spring by using a bean according to an embodiment of the present invention, where as shown in fig. 3, a replaced bytecode file is a compiled java code, and a file obtained after compiling is a Class file. When the Class bytecode file is loaded into the memory by Java, the JVM automatically generates a Java.
In practical applications, a Class does not have a common construction method, and its object is created by the JVM calling the defineClass () method in the Class loader when loading the Class, so a Class object cannot be created explicitly. The java.lang.class object can obtain other information of the object.
S103 (not shown in the figure): inheriting the first attribute information using an update file, and replacing the replaced bytecode file using the inherited update file.
And generating a new Class file by using a makeClass method of a Class pool tool Class in a javasist framework. And taking the original Class from the Class library pool by name, and then checking whether the Class file to be replaced is the same as the new file name or not, thereby avoiding errors in the next replacement.
And then inheriting the first attribute information of the replaced byte code file by the new Class file to obtain an inherited update file.
And then replacing the replaced byte code file with the inherited update file by using a child method replayBeanByClass of the springHookContext class.
The existing Class is removed by the factory Class DefaultListableBeanFactory of the applicable Context Class of the spring container, and the new Class is registered in the spring container.
The invention replaces the Class file under the spring rule, and the key point is that the spring frame is combined with a reflection mechanism, so that the Class replacement during the running of the spring frame is realized, and the bean dynamic replacement of the Class file is realized.
Example 2
In order to process the controller, the following steps are added to the embodiment 1 in the embodiment 2 of the invention:
obtaining the information of the referred class in the replaced bytecode file by using javasist; for the information of the reference class, judging whether the reference class exists by using an AntotatedElementUtils tool class; if yes, deleting the Mapping method in the Controller, and registering the inherited update file into the Controller.
Illustratively, calling the requestMappingUtils tool class to re-register the following information includes the following specific processes: the original Class, namely the information of the referenced Class in the replaced byte code file, is obtained through java, and whether each referenced Class exists is judged in an annotated elementary utility Class provided by spring. If the attribute information exists, reading out the second attribute information of the Mapping method of the Controller through a java reflection mechanism, then inheriting the second attribute information by using the update file, then deleting the Mapping method, and registering the update file inheriting the second attribute information into the Controller method
And refreshing the cache after the registration is finished, and waiting for the next updated file.
By applying the embodiment of the invention, a java language reflection mechanism is utilized to replace the original Class file to realize the function of dynamically switching the codes, the original bean in the spring container can be dynamically replaced during the running period of the java program, the spring container is seamlessly butted, and all functions of the spring container are perfectly compatible.
Example 3
Corresponding to embodiment 1 of the present invention, embodiment 3 of the present invention further provides an apparatus for dynamically replacing a Class in spring by a bean, where the apparatus includes:
the first acquisition module is used for acquiring the update file and inquiring a corresponding replaced byte code file according to the update file;
the second acquisition module is used for acquiring the first attribute information of the replaced byte code file by using a reflection mechanism;
and the updating module is used for inheriting the first attribute information by using the updating file and replacing the replaced byte code file by using the inherited updating file.
Further, the second obtaining module is configured to:
and calling getBeanInfo to acquire first attribute information of the replaced byte code file in the spring container.
Further, the update module is configured to:
the first attribute information of the replaced bytecode file is inherited by setSuperClass.
Further, the update module is configured to:
generating a new Class file by using a makeClass method in a javasist frame;
inheriting the first attribute information of the replaced byte code file by the new Class file to obtain an inherited update file;
replacing the replaced byte code file with the inherited update file by using a replayBeanByClass method;
and removing the replaced byte code file through the factory class of the DefaultListableBeanFactory, and registering the inherited update file into a spring container.
Further, the apparatus further comprises:
the third acquisition module is used for acquiring the information of the quoted class in the replaced bytecode file by utilizing javasist;
the judging module is used for judging whether the reference class exists by using an AntotatedElementUtils tool class aiming at the information of the reference class and triggering the registration module under the condition that the judging result of the judging module is yes;
and the registration module is used for deleting the Mapping method in the Controller and registering the inherited update file into the Controller.
The above examples are only intended to illustrate the technical solution of the present invention, and not to limit it; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.

Claims (10)

1. The method for dynamically replacing Class in spring by bean is characterized by comprising the following steps:
acquiring an update file, and inquiring a corresponding replaced byte code file according to the update file;
acquiring first attribute information of the replaced byte code file by using a reflection mechanism;
inheriting the first attribute information using an update file, and replacing the replaced bytecode file using the inherited update file.
2. The method for dynamically replacing Class in spring by bean according to claim 1, wherein said obtaining the first attribute information of the replaced bytecode file by using a reflection mechanism comprises:
and calling getBeanInfo to acquire first attribute information of the replaced byte code file in the spring container.
3. The method for dynamically replacing Class in spring by bean according to claim 1, wherein the updating file inherits the first attribute information and comprises:
the first attribute information of the replaced bytecode file is inherited by setSuperClass.
4. The method for dynamically replacing a Class in spring using beans as claimed in claim 1, wherein said replacing said replaced bytecode file with an inherited update file comprises:
generating a new Class file by using a makeClass method in a javasist frame;
inheriting the first attribute information of the replaced byte code file by the new Class file to obtain an inherited update file;
replacing the replaced byte code file with the inherited update file by using a replayBeanByClass method;
and removing the replaced byte code file through the factory class of the DefaultListableBeanFactory, and registering the inherited update file into a spring container.
5. The method for dynamically replacing a Class in spring using beans as claimed in claim 1, wherein the method further comprises:
obtaining the information of the referred class in the replaced bytecode file by using javasist;
for the information of the reference class, judging whether the reference class exists by using an AntotatedElementUtils tool class;
if yes, deleting the Mapping method in the Controller, and registering the inherited update file into the Controller.
6. The device for dynamically replacing Class in spring by bean is characterized by comprising the following steps:
the first acquisition module is used for acquiring the update file and inquiring a corresponding replaced byte code file according to the update file;
the second acquisition module is used for acquiring the first attribute information of the replaced byte code file by using a reflection mechanism;
and the updating module is used for inheriting the first attribute information by using the updating file and replacing the replaced byte code file by using the inherited updating file.
7. The apparatus for dynamically replacing a Class in spring with a bean according to claim 6, wherein the second obtaining module is configured to:
and calling getBeanInfo to acquire first attribute information of the replaced byte code file in the spring container.
8. The apparatus for dynamically replacing a Class in spring with bean as claimed in claim 6, wherein said update module is configured to:
the first attribute information of the replaced bytecode file is inherited by setSuperClass.
9. The apparatus for dynamically replacing a Class in spring with a bean according to claim 6, wherein the updating module is configured to:
generating a new Class file by using a makeClass method in a javasist frame;
inheriting the first attribute information of the replaced byte code file by the new Class file to obtain an inherited update file;
replacing the replaced byte code file with the inherited update file by using a replayBeanByClass method;
and removing the replaced byte code file through the factory class of the DefaultListableBeanFactory, and registering the inherited update file into a spring container.
10. The apparatus for dynamically replacing a Class in spring with a bean according to claim 6, further comprising:
the third acquisition module is used for acquiring the information of the quoted class in the replaced bytecode file by utilizing javasist;
the judging module is used for judging whether the reference class exists by using an AntotatedElementUtils tool class aiming at the information of the reference class and triggering the registration module under the condition that the judging result of the judging module is yes;
and the registration module is used for deleting the Mapping method in the Controller and registering the inherited update file into the Controller.
CN202211450678.0A 2022-11-18 2022-11-18 Method and device for dynamically replacing Class in spring by bean Pending CN115904551A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211450678.0A CN115904551A (en) 2022-11-18 2022-11-18 Method and device for dynamically replacing Class in spring by bean

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211450678.0A CN115904551A (en) 2022-11-18 2022-11-18 Method and device for dynamically replacing Class in spring by bean

Publications (1)

Publication Number Publication Date
CN115904551A true CN115904551A (en) 2023-04-04

Family

ID=86479154

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211450678.0A Pending CN115904551A (en) 2022-11-18 2022-11-18 Method and device for dynamically replacing Class in spring by bean

Country Status (1)

Country Link
CN (1) CN115904551A (en)

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101963914A (en) * 2010-11-12 2011-02-02 南京大学 Byte-code file reconstruction-based Java type online updating method
US20140245275A1 (en) * 2013-02-26 2014-08-28 Red Hat, Inc. Bytecode modification
CN108415834A (en) * 2018-02-12 2018-08-17 平安科技(深圳)有限公司 Explain rear end method of calibration, device, computer equipment and storage medium

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101963914A (en) * 2010-11-12 2011-02-02 南京大学 Byte-code file reconstruction-based Java type online updating method
US20140245275A1 (en) * 2013-02-26 2014-08-28 Red Hat, Inc. Bytecode modification
CN108415834A (en) * 2018-02-12 2018-08-17 平安科技(深圳)有限公司 Explain rear end method of calibration, device, computer equipment and storage medium

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
MURCIELAGOS: ""java修改源码_一个Java运行时修改代码的框架"", pages 2, Retrieved from the Internet <URL:《https://blog.csdn.net/weixin_35793018/article/details/115041087》> *

Similar Documents

Publication Publication Date Title
US10146515B1 (en) Live code updates
CN110673853B (en) Compiling method, device and system
US9430224B2 (en) Hot-update method and apparatus
CN101771762B (en) Method and system for dynamically loading services in service system
CN105022630B (en) A kind of assembly management system and assembly management method
CN110569250B (en) Management method and device for analysis library of Internet of things network element
CN114780198A (en) Client screen adapting method and device and electronic equipment
CN106569880A (en) Method and system for dynamically sharing resources between Android applications
CN110990019A (en) Java class analysis method and device, storage medium and electronic equipment
CN107239313A (en) The upgrade method and storage medium of Spring application services
CN112106024A (en) Method and platform for deploying industrial applications on an edge computing device of a machine tool
CN113360213A (en) Application program version switching method, device, equipment and system
CN117075930A (en) Computing framework management system
CN107621946B (en) Software development method, device and system
CN117707543A (en) Application installation package manufacturing and installation method, computing device and storage medium
CN111552518A (en) Control loading method and device for starting application
CN110806891B (en) Method and device for generating software version of embedded device
CN110968339B (en) Method and device for front-end building tool and electronic equipment
CN115904551A (en) Method and device for dynamically replacing Class in spring by bean
CN108694048B (en) Method for realizing batch publishing service
CN110321138B (en) Program updating and migration method and device
CN115755867A (en) Vehicle diagnosis method and related device
CN115729590A (en) Service deployment method, device, equipment and computer readable storage medium
CN107291439B (en) Target incremental data construction method and device
CN115374083A (en) Data source switching method and device, electronic equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
RJ01 Rejection of invention patent application after publication

Application publication date: 20230404

RJ01 Rejection of invention patent application after publication