Introducing scripting capabilities into our application can be a good way to improve flexibility. Our core development work can be focused on the development of core platform capabilities, and scenario-specific functionality can be implemented through scripting. For example, jenkins can write pipelines through groovy scripts, which can be very flexible to customize the build process. spring itself provides a mechanism for groovy integration, divided into two ways, one is to use groovy development program, similar to the development with java, need to be compiled. One is to execute groovy as a script, without compilation. Here we introduce the second way, using groovy as a script.
The specific code reference sample project.
There are 2 main ways to think about integrating groovy scripts in spring. One is to define the bean in the groovy script, so that the groovy script is integrated into the whole spring system, no different from using a normal bean. One is to call the groovy script in the program, so that the groovy script becomes an executable part. Here we describe each of these 2 ways. There are two ways to declare the beans defined in the groovy script in spring, one is the traditional xml and the other is the groovy declaration introduced in spring-framework-4.
Spring official Twitter @Spring I/O tweeted yesterday that Spring will be introducing a PHP engine that will allow running PHP code in spring applications. Allows to run programs like Wordpress in a Servlet container.
But, it’s just an April Fool’s joke. Did you get tricked? 😂
2. Defining beans in groovy
First we define an interface.
Here is a way of thinking, we can write a default interface implementation in java code. If the default implementation does not meet the requirements of a particular scenario, the program can be made very flexible by using a groovy script to implement a particular scenario with the strategy pattern. With the hot-loading mechanism of the script, when the processing logic needs to be changed, we can adjust the script content at any time during the program run and it can take effect in time.
This interface is implemented in the groovy script
The following are two ways of declaring beans through xml and groovy configuration respectively.
2.1. Declaring beans implemented in groovy via xml configuration
Declaring beans via xml configuration is the traditional method of spring, which has recently been replaced by declaring beans via java code, but it is still the easiest way to declare beans defined in groovy scripts.
The above xml code declares the bean myServiceXml, and
script-source specifies that the source of the bean is the script file
classpath:MyServiceImpl.groovy. Replace classpath with file to specify a script file in any location.
refresh-check-delay defines the refresh interval of the script, which can be automatically refreshed when the content of the script changes.
The property tag allows to initialize the assignment of properties to the bean. We use xml and groovy to declare the bean and assign different initial values to the property myProp, as you can see in the subsequent demo code.
2.2. Declare the bean implemented in groovy by means of groovy configuration
spring-framework-4 introduces the groovy way of declaring beans. We use groovy to declare the bean myServiceGroovy, which is more readable than the xml way.
For details, see the official spring blog post: Groovy Bean Configuration in Spring Framework 4
GroovyScriptFactory allows you to specify the location of the groovy script that defines the bean. With the lambda expression of
bean, you can assign values to the properties of the bean, defining the scope and the script refresh time, in addition to the property we defined, myProp.
2.3. Calling the beans implemented in groovy
We have declared 2 beans in xml and groovy:
myServiceGroovy respectively. We will call these two beans in our application.
First we import the bean declaration file via
@ImportResource, then we do the usual bean dependency injection and method invocation, as you can see there is no difference between the script-defined bean and the programmed bean in terms of bean usage. In the run method, we call the fun methods of the two beans of myServiceXml and myServiceGroovy respectively. Executing the run method you can see the output to the result.
3. Executing groovy scripts
In addition to implementing beans in groovy as mentioned earlier, we can also execute groovy scripts through the GroovyScriptEngine provided by groovy. This approach does not depend on springframework and can be used in ordinary java programs.
First we initialize GroovyScriptEngine and pass the path to the script file in the constructor.
There are two ways to execute the script, one is to get the GroovyObject and execute a method in the script by invokeMethod, the parameters of the method are passed through the Object array.
The second is to run the groovy script directly, which allows you to pass variables into the groovy script via Binding.