Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.
exam questions

Exam 2V0-72.22 All Questions

View all questions & answers for the 2V0-72.22 exam

Exam 2V0-72.22 topic 1 question 10 discussion

Actual exam question from VMware's 2V0-72.22
Question #: 10
Topic #: 1
[All 2V0-72.22 Questions]

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) {…}
  • D. @Autowired private Foo foo;
  • E. @Autowired private Foo foo2;
  • F. @Autowired public void setFoo(Foo foo2) {…}
Show Suggested Answer Hide Answer
Suggested Answer: BCE 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
Berny123
Highly Voted 1 year, 9 months ago
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.
upvoted 7 times
...
stefumies
Most Recent 1 month ago
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.
upvoted 1 times
...
Evoila_TrainingMaterial
3 months, 1 week ago
Selected Answer: BCE
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.
upvoted 1 times
stefumies
1 month ago
I don't believe these calls are all in the same class, I think these are options for the answer
upvoted 1 times
...
...
Eymet
1 year, 1 month ago
Selected Answer: BCE
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.
upvoted 3 times
...
Azuni
1 year, 3 months ago
Selected Answer: BCF
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.
upvoted 1 times
stefumies
1 month ago
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.
upvoted 1 times
...
Azuni
1 year, 3 months ago
*remove the ability to initialize*
upvoted 1 times
...
...
zakupower
1 year, 8 months ago
Selected Answer: BCE
@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.
upvoted 2 times
...
Berny123
1 year, 9 months ago
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.
upvoted 1 times
...
Berny123
1 year, 9 months ago
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
...
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.

SaveCancel
Loading ...