What is the purpose of the @Autowired annotation in Spring?
@Autowired is used to tell Spring to automatically inject a dependency (a class or an interface) into another class. Instead of creating objects manually using new, Spring can handle the object creation and management for you. It helps to achieve loose coupling between components by letting Spring manage dependencies.How does @Autowired work internally in Spring Boot?
@Service, @Repository, etc.). When @Autowired is used, Spring checks the container to see if there's an object (bean) of that type available and automatically injects it into the class where it's needed.Can you @Autowired a private field? Is it a good practice?
@Autowired, and Spring will still inject the dependency. However, it's not a good practice because it makes the code hard to test. Constructor-based injection is preferred because it makes it easier to write unit tests and ensures that dependencies are provided when the object is created.What are the different ways to autowire dependencies in Spring?
What happens if Spring finds multiple beans of the same type while autowiring?
If there are multiple beans of the same type and Spring can't decide which one to inject, it will throw a NoUniqueBeanDefinitionException. To resolve this, you can use @Qualifier to specify which exact bean to use. For example:
@Autowired
@Qualifier("specificBeanName")
private MyBean myBean;
What is the difference between @Autowired and @Resource?
@Autowired works by type, meaning it tries to find a matching bean based on the class type.@Resource works by name, meaning it tries to find a bean with the exact name of the field or property. In general, @Autowired is more commonly used in Spring, especially with Spring Boot.How do you handle circular dependencies with @Autowired in Spring?
A circular dependency occurs when two or more beans depend on each other, creating a loop. To fix this, you can:
@Lazy annotation on one of the beans to delay its initialization until it's really needed.Example using @Lazy:
@Autowired
@Lazy
private BeanA beanA;
Can you autowire a collection of beans in Spring? How?
Yes, you can inject a collection (like List, Set, or Map) of beans. Spring will automatically inject all the beans of that type into the collection. For example:
@Autowired
private List<MyBean> myBeans; // Injects all beans of type MyBean
This is useful when you have multiple beans of the same type and need to inject all of them at once.
What is the difference between @Autowired(required = false) and @Autowired?
@Autowired expects a dependency to be present, and if it's missing, it will throw an error. However, with @Autowired(required = false), Spring will skip the injection if the dependency isn't found and will not throw an error. This is useful when the dependency is optional.Is @Autowired mandatory in Spring Boot?
@Autowired is not mandatory in constructors when using Spring Boot. If a class has only one constructor, Spring automatically wires the dependencies even without the @Autowired annotation. However, for clarity and to follow good practices, many developers still include the @Autowired annotation.Scenario: Multiple Beans of the Same Type
Question: You have two beans of the same type, MyServiceImpl1 and MyServiceImpl2, and both implement the MyService interface. How would you use @Autowired to inject a specific bean into your class?
Expected Answer: Use @Qualifier with @Autowired to specify the exact bean to inject:
@Autowired
@Qualifier("myServiceImpl1")
private MyService myService;
Scenario: Optional Dependency Injection
Question: Suppose your service class has an optional dependency that may or may not be present at runtime. How can you handle this using @Autowired?
Expected Answer: Use @Autowired(required = false) to mark the dependency as optional. If the bean is not available, Spring won't throw an exception:
@Autowired(required = false)
private OptionalService optionalService;
Scenario: Circular Dependency
Question: Two beans, BeanA and BeanB, depend on each other (i.e., BeanA depends on BeanB and vice versa). How would you resolve this circular dependency using @Autowired?
Expected Answer: Use the @Lazy annotation on one of the beans to delay its initialization until it's actually needed:
@Autowired
@Lazy
private BeanB beanB;