From 4dc16dace839e1f12473cfa1c75443a258dafc9d Mon Sep 17 00:00:00 2001 From: Vladimir Orany Date: Thu, 4 Oct 2018 10:55:35 +0200 Subject: [PATCH] updated readme, fail if there is more then one bean definition --- README.adoc | 4 +-- build.gradle | 2 +- .../grails/GrailsMicronautBeanProcessor.java | 10 ++++-- .../GrailsMicronautBeanProcessorSpec.groovy | 31 ++++++++++--------- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/README.adoc b/README.adoc index 041b0d79e..38f56d0b7 100644 --- a/README.adoc +++ b/README.adoc @@ -262,12 +262,12 @@ public class GrailsConfig { @Bean public GrailsMicronautBeanProcessor widgetProcessor() { - new GrailsMicronautBeanProcessor(Widget.class, Prototype.class); // <1> + GrailsMicronautBeanProcessor.builder().addType('widget', Widget.class).build(); // <1> } } ---- -<1> List all classes of beans you want to include into Spring application context. Alternatively, you can declare the common annotation of these beans such as `@Prototype`. +<1> List all classes of beans you want to include into Spring application context. Second, create `spring.factories` descriptor which will automatically load the configuration once on classpath. diff --git a/build.gradle b/build.gradle index e071ab2d9..1912c1baa 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ allprojects { group 'com.agorapulse' - version "${micronautVersion}.3" + version "${micronautVersion}.4" repositories { jcenter() diff --git a/micronaut-grails/src/main/groovy/com/agorapulse/micronaut/grails/GrailsMicronautBeanProcessor.java b/micronaut-grails/src/main/groovy/com/agorapulse/micronaut/grails/GrailsMicronautBeanProcessor.java index ceff0e6ac..cb5faee4e 100644 --- a/micronaut-grails/src/main/groovy/com/agorapulse/micronaut/grails/GrailsMicronautBeanProcessor.java +++ b/micronaut-grails/src/main/groovy/com/agorapulse/micronaut/grails/GrailsMicronautBeanProcessor.java @@ -67,8 +67,7 @@ public Builder addByType(String grailsBeanName, Class... types) { } public Builder addByStereotype(String grailsBeanName, Class type) { - micronautBeanQualifiers.put(grailsBeanName, Qualifiers.byStereotype(type)); - return this; + return addByQualifiers(grailsBeanName, Qualifiers.byStereotype(type)); } public Builder addByName(String name) { @@ -80,7 +79,8 @@ public Builder addByName(String grailsBeanName, String micronautName) { return this; } - public Builder addByQualifiers(String grailsBeanName, Qualifier... qualifiers) { + @SafeVarargs + public final Builder addByQualifiers(String grailsBeanName, Qualifier... qualifiers) { micronautBeanQualifiers.put(grailsBeanName, Qualifiers.byQualifiers(qualifiers)); return this; } @@ -150,6 +150,10 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) Qualifier micronautBeanQualifier = entry.getValue(); Collection> beanDefinitions = micronautContext.getBeanDefinitions((Qualifier) micronautBeanQualifier); + if (beanDefinitions.size() > 1) { + throw new IllegalArgumentException("There is too many candidates for " + micronautBeanQualifier + "! Candidates: " + beanDefinitions); + } + Optional> firstBean = beanDefinitions.stream().findFirst(); BeanDefinition definition = firstBean.orElseThrow(()-> new IllegalArgumentException("There is no candidate for " + micronautBeanQualifier)); diff --git a/micronaut-grails/src/test/groovy/com/agorapulse/micronaut/grails/GrailsMicronautBeanProcessorSpec.groovy b/micronaut-grails/src/test/groovy/com/agorapulse/micronaut/grails/GrailsMicronautBeanProcessorSpec.groovy index 3004e5409..02f26e793 100644 --- a/micronaut-grails/src/test/groovy/com/agorapulse/micronaut/grails/GrailsMicronautBeanProcessorSpec.groovy +++ b/micronaut-grails/src/test/groovy/com/agorapulse/micronaut/grails/GrailsMicronautBeanProcessorSpec.groovy @@ -73,14 +73,15 @@ class GrailsConfig { @Bean GrailsMicronautBeanProcessor widgetProcessor() { - GrailsMicronautBeanProcessor.builder() - .addByType(Widget) - .addByType('someInterface', SomeInterface) - .addByStereotype('prototype', Prototype) - .addByName('gadget') - // see https://github.com/micronaut-projects/micronaut-core/issues/679 - // .addByQualifiers('otherMinion', Qualifiers.byName('other'), Qualifiers.byType(Minion)) - .build() + GrailsMicronautBeanProcessor + .builder() + .addByType(Widget) + .addByType('someInterface', SomeInterface) + .addByStereotype('prototype', Prototype) + .addByName('gadget') + // see https://github.com/micronaut-projects/micronaut-core/issues/679 + // .addByQualifiers('otherMinion', Qualifiers.byName('other'), Qualifiers.byType(Minion)) + .build() } } @@ -101,13 +102,6 @@ class TestWidget extends Widget { } interface Minion {} -@Singleton -@Named('other') -class OtherMinion implements Minion {} - -@Singleton -class NormalMinion implements Minion {} - @Prototype class PrototypeBean { @@ -129,3 +123,10 @@ class PrototypeBean { @Singleton @Named('gadget') class SomeGadget { } + +@Singleton +@Named('other') +class OtherMinion implements Minion {} + +@Singleton +class NormalMinion implements Minion {}