[weld-dev] How to make method injection when bean subclass is required in Weld?

Martin Kouba mkouba at redhat.com
Tue Aug 29 11:22:18 EDT 2017


Hi Alex,

that's an interesting question. Indeed, qualifiers are the way to go if 
you need to keep the method signature.

Another way could be to override the setFoo() method so that the Child 
initializer is ignored and add a new method to inject AdvancedFoo:

@Override
protected void setFoo(SimpleFoo foo) { // Do nothing }

@Inject
void setAdvancedFoo(AdvancedFoo foo) {
   super.setFoo(foo);
}

However, note that right now there are the following beans:

SimpleFoo with bean types Object, SimpleFoo
AdvancedFoo -> Object, SimpleFoo, AdvancedFoo

So if you do @Inject SimpleFoo you get ambiguous dependency exception 
because both SimpleFoo and AdvancedFoo are eligible for injection.

To resolve this you need to use qualifiers or restrict the bean types of 
AdvancedFoo:

@Typed(AdvancedFoo.class)
class AdvancedFoo extends SimpleFoo {}

HTH

Martin


Dne 29.8.2017 v 15:09 Matej Novotny napsal(a):
> Hi Alex,
> 
> no need to be sorry, you have come to the right place :)
> As for your question, the simplest thing is probably to use qualifiers.
> 
> Create your own like this:
> 
> @Qualifier
> @Retention(RetentionPolicy.RUNTIME)
> @Target({ ElementType.TYPE, ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD })
> public @interface MyQualifier {}
> 
> 
> And then change your AdvancedFoo class to use the qualifier:
> 
> @Dependent
> @MyQualifier
> public class AdvancedFoo extends SimpleFoo {
> }
> 
> And accordingly, the init method which uses injection should then look like this:
> 
> @Dependent
> public class Parent extends Child {
> 
>    @Inject
>    @Override
>    protected void setFoo(@MyQualifier SimpleFoo foo) {
>      super.setFoo(foo);
>    }
> }
> 
> Does this answer your question?
> 
> Matej
> 
> ----- Original Message -----
>> From: "Alex Sviridov" <ooo_saturn7 at mail.ru>
>> To: "weld-dev" <weld-dev at lists.jboss.org>
>> Sent: Tuesday, August 29, 2017 1:46:23 PM
>> Subject: [weld-dev] How to make method injection when bean subclass is required in Weld?
>>
>> Hi all,
>>
>> I am really sorry for writing to this mailing list, but I checked all user
>> forums and chats and saw that they are very old.
>>
>> I would be very thankful if someone gives suggestion for solving the
>> following problem.
>> I have a child and parent class. Child has SimpleFoo, Parent needs Advaced
>> foo. So,
>>
>> @Dependent
>> public class SimpleFoo {
>> }
>>
>> @Dependent
>> public class AdvancedFoo extends SimpleFoo {
>> }
>>
>> @Dependent
>> public class Child {
>>
>> private SimpleFoo foo;
>>
>> @Inject
>> protected void setFoo(SimpleFoo foo) {
>> this.foo = foo;
>> }
>> }
>>
>> @Dependent
>> public class Parent extends Child {
>>
>> @Inject
>> @Override
>> protected void setFoo(SimpleFoo foo) { //How to inject here AdvancedFoo?
>> super.setFoo(foo);
>> }
>> }
>>
>> How to inject in Parent AdvancedFoo? I know that I can do it via constructor
>> injection
>> but I need method injection. How to do it? Can it be done without using names
>> (like MyBean1)
>> but only using classes (AdvancedFoo)?
>>
>> Best regards, Alex
>>
>>
>>
>>
>>
>> --
>> Alex Sviridov
>>
>> _______________________________________________
>> weld-dev mailing list
>> weld-dev at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/weld-dev
> _______________________________________________
> weld-dev mailing list
> weld-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/weld-dev
> 

-- 
Martin Kouba
Senior Software Engineer
Red Hat, Czech Republic


More information about the weld-dev mailing list