Skip to content

Commit

Permalink
updated readme, fail if there is more then one bean definition
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Oct 4, 2018
1 parent b4ff5da commit 4dc16da
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
allprojects {
group 'com.agorapulse'
version "${micronautVersion}.3"
version "${micronautVersion}.4"

repositories {
jcenter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public Builder addByType(String grailsBeanName, Class<?>... types) {
}

public Builder addByStereotype(String grailsBeanName, Class<? extends Annotation> type) {
micronautBeanQualifiers.put(grailsBeanName, Qualifiers.byStereotype(type));
return this;
return addByQualifiers(grailsBeanName, Qualifiers.byStereotype(type));
}

public Builder addByName(String name) {
Expand All @@ -80,7 +79,8 @@ public Builder addByName(String grailsBeanName, String micronautName) {
return this;
}

public <T> Builder addByQualifiers(String grailsBeanName, Qualifier<T>... qualifiers) {
@SafeVarargs
public final <T> Builder addByQualifiers(String grailsBeanName, Qualifier<T>... qualifiers) {
micronautBeanQualifiers.put(grailsBeanName, Qualifiers.byQualifiers(qualifiers));
return this;
}
Expand Down Expand Up @@ -150,6 +150,10 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
Qualifier<?> micronautBeanQualifier = entry.getValue();
Collection<BeanDefinition<?>> beanDefinitions = micronautContext.getBeanDefinitions((Qualifier<Object>) micronautBeanQualifier);

if (beanDefinitions.size() > 1) {
throw new IllegalArgumentException("There is too many candidates for " + micronautBeanQualifier + "! Candidates: " + beanDefinitions);
}

Optional<BeanDefinition<?>> firstBean = beanDefinitions.stream().findFirst();
BeanDefinition<?> definition = firstBean.orElseThrow(()-> new IllegalArgumentException("There is no candidate for " + micronautBeanQualifier));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

}
Expand All @@ -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 {

Expand All @@ -129,3 +123,10 @@ class PrototypeBean {
@Singleton
@Named('gadget')
class SomeGadget { }

@Singleton
@Named('other')
class OtherMinion implements Minion {}

@Singleton
class NormalMinion implements Minion {}

0 comments on commit 4dc16da

Please sign in to comment.