From 10c333ea10b7b290eb9f2659ea7b3897d0b6a71a Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 26 Apr 2013 12:02:49 +0100 Subject: [PATCH] [bs-73] Tweak algorithm for detecting anonymous classes @Bean definitions in Groovy that contain closures have the bean name in the class name. Ugh. Added regex match to catch that. [#48718891] --- .../samples/integration.groovy | 14 +++++++------ spring-bootstrap-cli/samples/worker.groovy | 20 +++++++++++++++++++ ...ingBootstrapCompilerAutoConfiguration.java | 2 ++ ...gIntegrationCompilerAutoConfiguration.java | 19 ++++++++++++++++-- .../bootstrap/BeanDefinitionLoader.java | 2 +- 5 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 spring-bootstrap-cli/samples/worker.groovy diff --git a/spring-bootstrap-cli/samples/integration.groovy b/spring-bootstrap-cli/samples/integration.groovy index 5d5ce230393..ae746b0415d 100644 --- a/spring-bootstrap-cli/samples/integration.groovy +++ b/spring-bootstrap-cli/samples/integration.groovy @@ -1,15 +1,17 @@ package org.test @Component +@EnableIntegrationPatterns class SpringIntegrationExample implements CommandLineRunner { - def builder = new IntegrationBuilder() - def flow = builder.messageFlow { - transform {"Hello, $it!"} + @Bean + MessageFlow flow(ApplicationContext context) { + def builder = new IntegrationBuilder(context) + builder.messageFlow { transform {"Hello, $it!"} } } - @Override - public void run(String... args) { - print flow.sendAndReceive("World") + @Override + void run(String... args) { + print flow().sendAndReceive("World") } } diff --git a/spring-bootstrap-cli/samples/worker.groovy b/spring-bootstrap-cli/samples/worker.groovy new file mode 100644 index 00000000000..c96696f5937 --- /dev/null +++ b/spring-bootstrap-cli/samples/worker.groovy @@ -0,0 +1,20 @@ +package org.test + +@Grab("org.springframework.bootstrap:spring-bootstrap-service:0.0.1-SNAPSHOT") +@Grab("org.springframework.integration:spring-integration-dsl-groovy-amqp:1.0.0.M1") + +@Component +@EnableIntegrationPatterns +class SpringIntegrationExample implements CommandLineRunner { + + @Bean + MessageFlow flow(ApplicationContext context) { + def builder = new IntegrationBuilder(context) + builder.messageFlow { transform {"Hello, $it!"} } + } + + @Override + void run(String... args) { + print flow().sendAndReceive("World") + } +} diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBootstrapCompilerAutoConfiguration.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBootstrapCompilerAutoConfiguration.java index 6243b114954..880bbb01b6f 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBootstrapCompilerAutoConfiguration.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBootstrapCompilerAutoConfiguration.java @@ -72,6 +72,8 @@ public class SpringBootstrapCompilerAutoConfiguration extends CompilerAutoConfig "org.springframework.context.annotation.Scope", "org.springframework.context.annotation.Configuration", "org.springframework.context.annotation.Bean", + "org.springframework.context.ApplicationContext", + "org.springframework.context.MessageSource", "org.springframework.core.io.ResourceLoader", "org.springframework.bootstrap.CommandLineRunner", "org.springframework.bootstrap.context.annotation.EnableAutoConfiguration"); diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringIntegrationCompilerAutoConfiguration.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringIntegrationCompilerAutoConfiguration.java index a07fe762561..9940c5d13b8 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringIntegrationCompilerAutoConfiguration.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringIntegrationCompilerAutoConfiguration.java @@ -16,6 +16,12 @@ package org.springframework.bootstrap.cli.compiler.autoconfigure; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + import org.codehaus.groovy.ast.ClassNode; import org.codehaus.groovy.control.customizers.ImportCustomizer; import org.springframework.bootstrap.cli.compiler.AstUtils; @@ -33,8 +39,8 @@ public class SpringIntegrationCompilerAutoConfiguration extends CompilerAutoConf public boolean matches(ClassNode classNode) { // Slightly weird detection algorithm because there is no @Enable annotation for // Integration - return AstUtils.hasLeastOneAnnotation(classNode, "MessageEndpoint") - || classNode.getName().contains("SpringIntegration"); + return AstUtils.hasLeastOneAnnotation(classNode, "MessageEndpoint", + "EnableIntegrationPatterns"); } @Override @@ -59,6 +65,15 @@ public class SpringIntegrationCompilerAutoConfiguration extends CompilerAutoConf "org.springframework.integration.annotation.Headers", "org.springframework.integration.annotation.Payload", "org.springframework.integration.annotation.Payloads", + EnableIntegrationPatterns.class.getCanonicalName(), + "org.springframework.integration.dsl.groovy.MessageFlow", "org.springframework.integration.dsl.groovy.builder.IntegrationBuilder"); } + + @Target(ElementType.TYPE) + @Documented + @Retention(RetentionPolicy.RUNTIME) + public static @interface EnableIntegrationPatterns { + + } } diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/BeanDefinitionLoader.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/BeanDefinitionLoader.java index 39f4d43c781..e90e5e00d72 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/BeanDefinitionLoader.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/BeanDefinitionLoader.java @@ -163,7 +163,7 @@ class BeanDefinitionLoader { } // Nested anonymous classes are not eligible for registration, nor are groovy // closures - if (type.isAnonymousClass() || type.getName().contains("$_closure") + if (type.isAnonymousClass() || type.getName().matches(".*\\$_.*closure.*") || type.getConstructors() == null || type.getConstructors().length == 0) { return false; }