2V0-72-22 practice question 11 of 15

Which two statements about the @Autowired annotation are true? (Choose two.)

Full set: 79 questions
Answer options
A:

A. @Autowired fields are injected after any config methods are invoked.

B:

B. Multiple arguments can be injected into a single method using @Autowired.

Correct answer
C:

C. By default, if a dependency cannot be satisfied with @Autowired, Spring throws a RuntimeException.

Correct answer
D:

D. If @Autowired is used on a class, field injection is automatically performed for all dependencies.

E:

E. @Autowired can be used to inject references into BeanPostProcessor and BeanFactoryPostProcessor.

Correct Answer:
B. B. Multiple arguments can be injected into a single method using @Autowired.; C. C. By default, if a dependency cannot be satisfied with @Autowired, Spring throws a RuntimeException.
Explanation

The @Autowired annotation can be applied to methods with multiple arguments, and the Spring container will attempt to resolve and inject a bean for each argument. By default, autowiring is required (required=true). If Spring cannot find a matching bean to inject for a required dependency, it will fail fast by throwing a NoSuchBeanDefinitionException (a RuntimeException) during the application context startup, preventing the application from running with an unsatisfied dependency.

Why Incorrect

A. @Autowired fields are injected during the "populate properties" phase, which is after bean instantiation but before initialization callbacks. The statement is imprecise about the lifecycle.
D. @Autowired is not a class-level annotation. It is used on constructors, fields, or methods to signal that a dependency should be injected.
E. Autowiring into BeanFactoryPostProcessor is not supported for regular beans, as they operate on bean definitions before any bean instances are created.

References

1. Spring Framework Documentation, 6.1.6. "Core Technologies", Section 1.4.5. "Annotation-based Container Configuration", Subsection "Autowired". States that @Autowired can be used on methods and that by default, the dependency is required.

2. Spring Framework Documentation, 6.1.6. "Core Technologies", Section 1.4.5. "Annotation-based Container Configuration", Subsection "Fine-tuning Annotation-based Autowiring with @Primary and @Qualifier". Mentions that if no matching bean is found for a required dependency, an exception is thrown.

3. Javadoc for org.springframework.beans.factory.annotation.Autowired. The required attribute documentation states it defaults to true, indicating the dependency is mandatory.

Scroll to Top