XDR
XDR

Reputation: 4480

In Grails, specifying which of two classes implementing the same interface should be used for injection

In Grails 2.5.0, there's Grails class, let's call it G, that implements a certain interface, let's call it I; I've also created a new class myself, let's call it M, that also implements I.

Unfortunately, a third-party Grails plugin injects using:

@Autowired
I i

which throws a NoUniqueBeanDefinitionException, because both G & M implement I, and Grails doesn't know which to use.

How can I specify that for all injections of I, class G should be used?

I only want to inject class M for injections like:

def m

or

M m

Some solutions mention using an @Qualifier annotation at the injection site, but, because the injection site is in a third-party plugin, I don't want to modify that code if I can avoid it. I also shouldn't modify the source of G, since it is from Grails itself. So, I'd prefer either to configure this in some config file, or to somehow annotate M such that it either isn't a candidate for I injection, or that it's a lower priority candidate for I injection than default priority, which is what I assume has been applied to G.

I know that this must be documented somewhere in Grails and/or Spring, it's just that I've gotten many spurious results searching for an answer.

A possibly simpler converse question is how to specify that M should be used for injections of type I, instead of my original question about how to specify that M should not be used for injections of type I.

Upvotes: 1

Views: 381

Answers (1)

XDR
XDR

Reputation: 4480

In grails-app/conf/spring/resources.groovy, add:

beans = {springConfig.addAlias 'i', 'g'}

Upvotes: 1

Related Questions