Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
Last active October 22, 2025 10:34
Show Gist options
  • Select an option

  • Save thomasdarimont/f7f2ef6f4900b9f89d58 to your computer and use it in GitHub Desktop.

Select an option

Save thomasdarimont/f7f2ef6f4900b9f89d58 to your computer and use it in GitHub Desktop.

Revisions

  1. thomasdarimont revised this gist Sep 16, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions App.java
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,11 @@ public Evaluator evaluator(BeanFactory beanFactory) throws Exception {

    @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) {
  2. thomasdarimont revised this gist Sep 16, 2015. 1 changed file with 90 additions and 0 deletions.
    90 changes: 90 additions & 0 deletions pom.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    <?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>
  3. thomasdarimont created this gist Sep 16, 2015.
    63 changes: 63 additions & 0 deletions App.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    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) {
    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();
    }
    }
    6 changes: 6 additions & 0 deletions Evaluator.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    package demo;

    public interface Evaluator {

    boolean evaluate(Object arg);
    }
    6 changes: 6 additions & 0 deletions GroovyEvaluator.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    class GroovyEvaluator implements demo.Evaluator{

    def boolean evaluate(arg) {
    return true;
    }
    }