Given an ApplicationContext containing three bean definitions of type Foo with bean ids foo1, foo2, and foo3, which three @Autowired scenarios are valid and will allow the ApplicationContext to initialize successfully? (Choose three.)
A.
@Autowired public void setFoo (Foo foo) {…}
B.
@Autowired @Qualifier (“foo3”) Foo foo;
C.
@Autowired public void setFoo (@Qualifier (“foo1”) Foo foo) {…}
A and D is not correct at all. I defined 3 beans definitions with those ids and in my service class used setter injection or simple Foo foo definition of Autowired-> gives error:
Could not autowire. There is more than one bean of 'Foo' type.
Beans:
foo1 (MyConfiguration.java), foo2 (MyConfiguration.java), foo3 (MyConfiguration.java)
Working cases per single case: B,C,E,F
But not works all 4 together, C and F say the setFoo method name is already defined.
So B, E, F will work -> but will inject only 2 beans, foo3 and foo2 -> foo2 in 2 places, but is at the end the same bean defined once through setter injection and second time through autowired filed injection.
Or B, E, C will work -> will inject foo1, foo2, foo3
So the only correct answer to inject beans foo1,foo2,foo3 with working program is B, E, C.
A D and F will fail as they are ambiguous calls to Foo by type, when there is more than one bean of that type.
Thus BCE will work due to the use of the @Qualifier annotation specifying which Foo to use. @Qualifier can be used
on constructors, setters and fields.
in both option C and F, we are using the same method setFoo() to autowire the beans, which would lead to a method redeclaration error upon compilation.
Although for E field injection is not recommended, F will conflict with C because of two methods setFoo with the same signature. So BCE will initialize an ApplicationContext without any errors.
I believe it is BCF. You can interchange E and F, because they will do the same thing, so the comments below are not wrong either, but here is why I say BCF instead of BCE.
Do not perform field injection on a private field (as illustrated in E). It will work when you run the application as per normal, but you remote the ability to initialize that field it in a unit test. In the VMWare Spring Framework Essentials course on the topic of Annotation-based configurations, the lecturer warned not to do it.
F will fail as it is ambigulously attempting to inject a Foo when there is more than one Foo, the parameter name is irrelevant, it can be anything, the Type (Foo) without qualification will cause the problem here.
@Qualifier annotation is used to specify a bean id when injecting. @Qualifier can be used both on fields and method parameters. If it is a field injection the name of the field can qualify the bean.
The very first injection was foo1 setter injection (@5293), after that foo3 and foo2 (the upper definition in class gets injected first, so in my code, I had defined the order of injections as follows: B, E, C, but setter injection (C) happens first, then B and E). There is no problem with the same "foo" variable name of two different beans in one class (C and B injections), as the variable scopes are different, and in practice, in case C the foo1 bean can be used and processed immediately (first) or reassigned to another variable name, therefore with this injection configuration the injection of all three beans foo1, foo2, foo3 is possible.
Explanation of my previous comment/answer:
Spring injection happens first by type definition, but the type is the same in all answers, so it looks for @Primary annotation, which is not found, then for @Qualifier annotations, and the last lookup, if @Qualifier was not found, is the resolution by bean name. If not successful, after all, the exception is thrown.
In my demo app, when I run B,E,C, my break point first stops at setter method setFoo where I can see in debug already injected beans:
Foo@5293 - foo1 - C
Foo@5297 - foo3 - B
Foo@5298 - foo2 - E
upvoted 1 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
Berny123
Highly Voted 1 year, 9 months agostefumies
Most Recent 1 month agoEvoila_TrainingMaterial
3 months, 1 week agostefumies
1 month agoEymet
1 year, 1 month agoAzuni
1 year, 3 months agostefumies
1 month agoAzuni
1 year, 3 months agozakupower
1 year, 8 months agoBerny123
1 year, 9 months agoBerny123
1 year, 9 months ago