Last active
May 13, 2022 12:40
-
-
Save thomasdarimont/f7f2ef6f4900b9f89d58 to your computer and use it in GitHub Desktop.
Example for defining a refreshable Groovy Script in Spring Boot with Java Config - at least thats the current state of affairs :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo; | |
import java.util.concurrent.TimeUnit; | |
import org.springframework.aop.TargetSource; | |
import org.springframework.aop.framework.ProxyFactory; | |
import org.springframework.aop.support.DelegatingIntroductionInterceptor; | |
import org.springframework.beans.factory.BeanFactory; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.core.io.ClassPathResource; | |
import org.springframework.scripting.groovy.GroovyScriptFactory; | |
import org.springframework.scripting.support.RefreshableScriptTargetSource; | |
import org.springframework.scripting.support.ResourceScriptSource; | |
@SpringBootApplication | |
public class App { | |
public static void main(String[] args) throws Exception{ | |
Evaluator evaluator = SpringApplication.run(App.class, args).getBean("evaluator", Evaluator.class); | |
for (int i = 0; i < 20; i++) { | |
System.out.printf("evaluated to %s%n", evaluator.evaluate(null)); | |
TimeUnit.SECONDS.sleep(1); | |
} | |
} | |
@Bean | |
public Evaluator evaluator(BeanFactory beanFactory) throws Exception { | |
GroovyScriptFactory factory = new GroovyScriptFactory("classpath"); | |
factory.setBeanFactory(beanFactory); | |
ResourceScriptSource script = new ResourceScriptSource(new ClassPathResource("GroovyEvaluator.groovy")); | |
RefreshableScriptTargetSource rsts = new RefreshableScriptTargetSource(beanFactory, "ignored-bean-name", factory, script, false) { | |
@Override | |
protected Object obtainFreshBean(BeanFactory beanFactory, String beanName) { | |
/* | |
* we ask the factory to create a new script bean directly instead | |
* asking the beanFactory for simplicity. | |
*/ | |
try { | |
return factory.getScriptedObject(script); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
}; | |
rsts.setRefreshCheckDelay(1000L); | |
ProxyFactory proxyFactory = new ProxyFactory(); | |
proxyFactory.setTargetSource(rsts); | |
proxyFactory.setInterfaces(Evaluator.class); | |
DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(rsts); | |
introduction.suppressInterface(TargetSource.class); | |
proxyFactory.addAdvice(introduction); | |
return (Evaluator) proxyFactory.getProxy(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo; | |
public interface Evaluator { | |
boolean evaluate(Object arg); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GroovyEvaluator implements demo.Evaluator{ | |
def boolean evaluate(arg) { | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>demo</groupId> | |
<artifactId>spring-boot-groovy-reloading-demo</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>spring-boot-groovy-reloading-demo</name> | |
<description>Demo project for Groovy reloading Spring Boot</description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.3.0.M5</version> | |
<relativePath /> <!-- lookup parent from repository --> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-aop</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.codehaus.groovy</groupId> | |
<artifactId>groovy</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
<repositories> | |
<repository> | |
<id>spring-snapshots</id> | |
<name>Spring Snapshots</name> | |
<url>https://repo.spring.io/snapshot</url> | |
<snapshots> | |
<enabled>true</enabled> | |
</snapshots> | |
</repository> | |
<repository> | |
<id>spring-milestones</id> | |
<name>Spring Milestones</name> | |
<url>https://repo.spring.io/milestone</url> | |
<snapshots> | |
<enabled>false</enabled> | |
</snapshots> | |
</repository> | |
</repositories> | |
<pluginRepositories> | |
<pluginRepository> | |
<id>spring-snapshots</id> | |
<name>Spring Snapshots</name> | |
<url>https://repo.spring.io/snapshot</url> | |
<snapshots> | |
<enabled>true</enabled> | |
</snapshots> | |
</pluginRepository> | |
<pluginRepository> | |
<id>spring-milestones</id> | |
<name>Spring Milestones</name> | |
<url>https://repo.spring.io/milestone</url> | |
<snapshots> | |
<enabled>false</enabled> | |
</snapshots> | |
</pluginRepository> | |
</pluginRepositories> | |
</project> |
Would be nice to have a complete maven directory structure - at least in the download zip.
You can do it
GroovyEvaluator.groovy should be in the resource folder, FYI
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would be nice to have a complete maven directory structure - at least in the download zip.