Detailed description of the invention
Because Groovy grammer is similar with Java grammer, therefore in the embodiment of the present application, pass through Spring scriptLoader is loaded into Spring context using the Groovy script of writing as a JavaBean(SpringContext) in, and because the multiple JavaBean in Spring context all can draw mutuallyWith, therefore this Groovy script can directly be quoted other JavaBean in Spring context, otherJavaBean also can quote this Groovy script as JavaBean equally, thereby has improved GroovyThe extensibility of script and the flexibility of use.
Below in conjunction with Figure of description, the embodiment of the present application is described in detail.
Fig. 1 is the Groovy script loading procedure that the embodiment of the present application provides, and specifically comprises the following steps:
The Groovy script of storing in S101:Spring Script loader reading database.
In the embodiment of the present application, the Groovy script of writing can be stored in database, certainly,Also can be kept in other memories. Groovy script cache management list in Spring Script loaderUnit reads this Groovy script, specifically can in the time that the system at this Spring Script loader place starts, readThe Groovy script of storing in storehouse that fetches data, also can regularly read Groovy script, can also work as dataGroovy script in storehouse reads Groovy script while changing, for example, in database, newly increased oneGroovy script, or upgraded a Groovy script.
S102: obtain the attribute information of this Groovy script, generate corresponding according to the attribute information that obtainsExtend markup language (ExtensibleMarkupLanguage, XML) configuration file, and be loaded onIn Spring context.
When a JavaBean is loaded in Spring context, first to load JavaBean'sAttribute information, comprises that the attribute such as Bean mark or class title (ClassName) of this JavaBean is believedBreath, therefore in the embodiment of the present application, the configuration of the SpringXML in Spring Script loader module units obtainsGet the attribute information of this Groovy script. It is upper and lower due to the attribute information of JavaBean is loaded on to SpringIn literary composition time, need to load by XML configuration file, therefore SpringXML configuration module units basisThe attribute information of this Groovy script obtaining generates corresponding XML configuration file, and is loaded on SpringIn context.
For example, according to the ClassName of the Groovy script obtaining, generate corresponding XML configuration fileAs follows:
Generate after XML configuration file as above, SpringXML configuration module units can be upperState XML configuration file and be loaded in Spring context, wherein, the SpringXML in the embodiment of the present applicationConfiguration module units can be the XmlBeanDefinitionReader of Spring.
S103: this Groovy script is loaded in Spring context as JavaBean.
In the embodiment of the present application, the attribute information of this Groovy script is being loaded on to Spring contextIn after, Bean processor (Processor) unit in Spring Script loader can be by this GroovyScript is loaded in Spring context as a JavaBean, need to expand this Groovy scriptWhen function, other JavaBean that load in Spring context are offered to this Groovy pin of loadingOriginally quote, when other JavaBean in Spring context need to expand the function of this Groovy script,This Groovy script loading being offered to other JavaBean that load in Spring context quotes.
A Spring container can be regarded as in this Spring context, loaded multiple JavaBean in container,Each JavaBean can complete corresponding function, and for one of them JavaBean, shouldOther one or several JavaBean that JavaBean loads in can reference container, also can be added in containerCarry other one or several JavaBean quote, therefore, in the embodiment of the present application by Groovy scriptBe loaded in Spring context as a JavaBean, just other loadings by referenceThe mode of JavaBean is expanded the function of this Groovy script easily, also can make other JavaBean spiritsThat lives quotes this Groovy script, thereby has improved the extensibility of Groovy script and the flexibility of use.
For example, the function of this Groovy script is for local by the format conversion of the message of external system transmissionThe form that system is supported, if be first external system to be sent by the Function Extension of this Groovy scriptMessage is decrypted, then the message after deciphering is carried out to format conversion, should in the embodiment of the present applicationAfter Groovy script is loaded in Spring context as JavaBean, just can directly quote SpringAnother JavaBean with decipher function loading in context, and without again writing whole GroovyScript, thus the extensibility of Groovy script improved. Accordingly, if added in Spring contextWhen other JavaBean that carry want amplifying message format conversion function, also can directly quote same conductThis Groovy script of JavaBean, thus the flexibility that Groovy script uses improved.
In the embodiment of the present application, in order to improve the loading velocity of Groovy script as far as possible, in above-mentioned stepsIn S101, when in Spring Script loader reading database, store Groovy script time, by databaseThis Groovy script of middle storage reads in buffer memory, thereby is carrying out follow-up step S102 and S103Time, generating and load the attribute information of Groovy script and loading Groovy script is all also to hold in buffer memoryRow is also that whole Spring context is present in buffer memory. So not only can improve Groovy scriptLoading velocity, can also be loading when new script, without restarting the whole of this Spring Script loader placeIndividual system, only needs flush buffers.
In the embodiment of the present application, in order to improve the accuracy of this Groovy script that follow-up use loads,Spring Script loader in this Groovy script is loaded on Spring context as JavaBean itBefore, also before the step S103 shown in Fig. 1, also to carry out syntax check to this Groovy script,And determine the syntax check of this Groovy script is passed through. Wherein, can pass through Spring Script loaderIn syntax check unit this Groovy script is carried out to syntax check, also this Groovy script is carried outCompiling, compiling by after again this Groovy script is loaded on as JavaBean in Spring context,Otherwise prompting compile error.
And, to consider in actual applications, common syntax check only checks the language of this Groovy scriptWhether method is correct, the GroovyClassLoader that for example Groovy carries. If at this Groovy scriptIn only defined a name, be for example test, its grammer does not have mistake, therefore common syntax checkCan think that the syntax check that this Groovy script that has defined a name is carried out passes through. But in factThis Groovy script does not have any other function except this name of definition, does not namely haveTo the function of a JavaBean, therefore this Groovy script is loaded on to Spring as a JavaBeanIn context without any meaning.
Lack of standardization for fear of above-mentioned Groovy script in the embodiment of the present application, make to load this Groovy pinThe problem that this is beyond all doubt, the method that this Groovy script is carried out to syntax check is specially, and judgement shouldIn Groovy script, whether comprise at least one and inherit in setting the subclass of base class, when judged result is when being,Determine the syntax check of this Groovy script is passed through, otherwise, determine the grammer inspection to this Groovy scriptLook into and do not pass through.
That is to say, in the embodiment of the present application except Groovy script is carried out common syntax check,Also to check that in this Groovy script, whether comprising at least one inherits in the subclass of setting base class, wherein,This setting base class can be set as required. For example,, when this Groovy script is used for external systemThe message format sending is while carrying out format conversion, and this setting base class can be set as having format conversion functionBase class, but this base class does not comprise specifically message which external system is sent or external systemThe message which port sends or the message of which kind of form carry out this format conversion. This Groovy scriptIn comprise the subclass of inheriting in above-mentioned setting base class and just refer to, the format conversion function tool that this base class is hadThe message that is applied to which external system transmission of body, or the message of which port transmission of external system,Or the message of which kind of form carries out the subclass of this format conversion.
Continue along using example, suppose that this Groovy script is for sending external system A and external system BMessage format carry out format conversion, wherein, external system A with form a send message, external system BSend message with form b, and local system can only be resolved the message of form c, can set two base class,First is the method that form a is converted to form c, and second is the side that form b is converted to form cMethod. A subclass in this Groovy script is inherited in first base class, be, by external system A withThe message that form a sends is converted to the message of form c by the method for first base class, another subclass continuesHold in second base class, be the message that external system B is sent with form b by the side of second base classMethod is converted to the message of form c. Thereby, adopting the method for the embodiment of the present application to this Groovy scriptWhile carrying out syntax check, determine that in this Groovy script, having comprised at least one inherits in the son of setting base classClass, syntax check is passed through. And if in this Groovy script, only defined a name, be for example defined asTest, while adopting the method for the embodiment of the present application to carry out syntax check, although common grammer is not wrongMistake, but any succession in the subclass of setting base class in Groovy script, do not comprised, and therefore syntax check is notPass through.
The above-mentioned syntax check providing by the embodiment of the present application, obstructed out-of-date when syntax check, prompting is compiledTranslate mistake, in the time that syntax check is passed through, Spring Script loader is using this Groovy script as JavaBeanBe loaded in Spring context. In order to improve the response speed of system at this Spring Script loader placeDegree, adopts the lazy mode loading to load this Groovy script in the embodiment of the present application, also adopt lazy loadingMode this Groovy script is loaded in Spring context as JavaBean.
The detailed process that the Groovy script that Fig. 2 provides for the embodiment of the present application loads, specifically comprises followingStep:
S201:Spring Script loader reads the Groovy script of storing in database in buffer memory.
S202: obtain the attribute information of this Groovy script, generate corresponding according to the attribute information that obtainsXML configuration file, and be loaded in Spring context.
S203: judge that whether the grammer in this Groovy script is correct, if the determination result is YES, carry outStep S204, otherwise execution step S206.
S204: judge that in this Groovy script, whether comprising at least one inherits in the subclass of setting base class,If judged result is to perform step S205, otherwise perform step S206.
S205: adopt the lazy mode loading that this Groovy script is loaded on Spring as JavaBeanHereinafter.
S206: prompting compile error.
Wherein, the execution sequence of the step S203 in said process and step S204 in no particular order.
Fig. 3 is the Groovy script charger that the embodiment of the present application provides, and specifically comprises:
Groovy script memory management unit 301, the Groovy script of storing for reading database;
SpringXML configures module units 302, for obtaining the attribute information of described Groovy script,Generate corresponding XML configuration file according to the attribute information obtaining, and be loaded in Spring context;
BeanProcessor unit 303, for being loaded on described Groovy script as JavaBeanIn Spring context, and other JavaBean that load in Spring context are offered to the institute of loadingState Groovy script reference, or the described Groovy script loading is offered in Spring context and addedOther JavaBean that carry quote.
Described Groovy script memory management unit 301 specifically for, described in storing in databaseGroovy script reads in buffer memory.
Corresponding device also comprises:
Syntax check unit 304, for making described Groovy script in described BeanProcessor unitFor before JavaBean is loaded in Spring context, described Groovy script is carried out to syntax check,And determine the syntax check of described Groovy script is passed through.
Described syntax check unit 304 specifically for, judge in described Groovy script and whether comprise at leastInherit in setting the subclass of base class for one, when judged result is when being, definite to described Groovy scriptSyntax check is passed through, otherwise, determine the syntax check of described Groovy script is not passed through.
Described BeanProcessor unit 303 specifically for, adopt the lazy mode loading by described GroovyScript is loaded in Spring context as JavaBean.
Concrete above-mentioned Groovy script charger can be arranged in Spring Script loader.
The Spring Script loader specific implementation structural representation that Fig. 4 provides for the embodiment of the present application, as figureShown in 4, in the Groovy script memory management unit reading database in Spring Script loaderGroovy script is to buffer memory, and SpringXML configuration module units obtains the attribute information of Groovy script,And generate corresponding XML configuration file and be loaded in Spring context, the inspection of syntax check unit shouldWhether the grammer of Groovy script makes mistakes, and checks in this Groovy script whether comprise at least one successionIn the subclass of setting base class, pass through if check, the lazy mode loading of BeanProcessor unit employing willThis Groovy script is loaded in Spring context as a JavaBean.
The embodiment of the present application provides a kind of Groovy script loading method and device, the method Spring scriptThe Groovy script of storing in loader reading database, obtains the attribute information of this Groovy script, rawBecome corresponding XML configuration file and be loaded in Spring context, finally this Groovy script being doneFor JavaBean is loaded in Spring context. Because the multiple JavaBean under Spring framework canTo quote mutually as required, therefore in the embodiment of the present application using Groovy script as a JavaBeanBe loaded in Spring context, can make this Groovy script in the time of needs expanded function, directly quoteOther JavaBean in Spring context, other JavaBean need to expand this Groovy scriptWhen function, also can directly quote this Groovy script as JavaBean equally, thereby improveThe extensibility of Groovy script and the flexibility of use.
Obviously, those skilled in the art can carry out various changes and modification and not depart from this Shen the applicationSpirit and scope please. Like this, if the application these amendment and modification belong to the application's claim andWithin the scope of its equivalent technologies, the application be also intended to comprise these change and modification interior.