What is the purpose of the @Component annotation?

@Component annotation is used to define a bean. This annotation is placed on a Java class. The XML file contains the <context: component-scan> tag to enable component scanning. Spring container scans the package (and its sub-packages) specified in the component-scan tag. It will register all classes marked with the @Component annotation as beans.

Using the @Component annotation can save a lot of time spent in writing lengthy bean definition code in XML.

What is the difference between @Component, @Service, @Repository, and @Controller?

A typical application is divided into layers. The @Component annotation is generic and denotes any Spring-managed bean. It can be used in any layer of the application, whereas the other three are specific to layers. @Controller is used in the web layer, @Service is used on classes in the business layer and @Repository is used in the data layer and provides the additional functionality of making unchecked exceptions translated as Spring DataAccessException.

Why is @Primary annotation used?

When two beans of the same type qualify to be autowired, then @Primary annotation is used to break the tie. Suppose there are two implementations of the Engine interface, CombustionEngine and ElectricEngine. The class Vehicle has a dependency on the Engine interface. If both implementations have the @Component annotation, then a compiler error will occur stating that more than one bean of the same type was found. In this case, we can guide Spring to use either CombustionEngine or ElectricEngine as the primary choice by using the @Primary annotation.

Why is @Qualifier annotation used?

If more than one bean of the same type exists, we can choose the bean to be autowired using the @Qualifier annotation. The bean having this annotation qualifies to be autowired.

Which annotations takes precedence: @Primary or @Qualifier?

When both the @Primary and @Qualifer annotations are present, then @Qualifier takes precedence. @Primary defines a default value, and bean marked with this annotation will be used unless otherwise indicated. @Qualifier annotation is specific and is used when a particular bean is needed.

Suppose there are two beans of the same type and one is used in 90% of the cases, then it makes sense to make it the default choice by using the @Primary annotation. The @Autowired that needs the other bean can use the @Qualifier annotation while all other @Autowired will automatically choose the bean marked with @Primary.

Why is the @Required annotation used?

@Required is a method-level annotation. It is used on setter methods and makes setter injection of the property mandatory. The BeanInitializationException is thrown if the property value is not initialized. If a setter method has @Autowired annotation on it, then @Required is not needed.

This annotation has been deprecated because constructor injection is used for setting all mandatory dependencies.

What is the purpose of @Autowired annotation?

@Autowired annotation specifies where and how autowiring is done. This annotation can be used on setter methods, with a constructor argument, on a property as well as on methods with multiple arguments. By default, autowiring is done by type.

Both @Bean and @Component annotations create beans. What is the difference between the two?

Some differences between the two annotations are:

  • @Component enables Spring to auto-detect and auto-configure beans while @Bean is used to explicitly declare a bean rather than letting Spring auto-detect it.
  • @Component is a class level annotation while @Bean is a method level annotation.
  • Since the @Component annotation is used on a class, it keeps the bean definition and class declarations together while @Bean decouples them.

What is the difference between @Inject and @Autowired in Spring? Which one to use under which condition?

Both these annotations perform the same function and are used for dependency injection by type. The order of dependency injection of both the annotations is as follows:

  1. By type
  2. Using @Qualifer annotation
  3. By name.

The only difference between both annotations is that @Inject is a CDI annotation which makes it framework-independent and @Autowired is a Spring framework annotation. Thus using @Inject may be helpful if the application is moved to another framework.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.