Exam 2v0-7222 All QuestionsBrowse all questions from this exam
Question 9

Which two statements are true concerning constructor injection? (Choose two.)

    Correct Answer: A, C

    Constructor injection is preferred over field injection to support unit testing because it makes dependencies explicit and easier to manage. Also, if there is only one constructor, the @Autowired annotation is not required, as the dependency injection framework can automatically use that constructor without additional annotations.

Discussion
Ancient1Options: AC

A: @Autowired is not necessary if you have one constructor that instantiates the fields C: Constructor injection is preferred over field injection, as it helps with unit testing. Most IDE's will warn you against field injection anyways.

zakupowerOptions: AC

Contructor injection is preferred because it allows for easier unit testing. If only one constructor is available it will be used to inject dependencies without the need of @Autowired annotation.

faciorysOptions: AC

As of Spring Framework 4.3, an @Autowired annotation on such a constructor is no longer necessary if the target bean only defines one constructor to begin with. Answer A and C

EymetOptions: AC

See other comments why

antpao86

Field injection is ambiguous and doesn't really tell you which dependencies are mandatory.