Automatic bean detection with stereotype annotations

In the previous section, we learned about the @Autowired annotation that handles only wiring. You still have to define the beans themselves so the container is aware of them and can inject them for you. Spring Framework provides us with some special annotations. These annotations are used to create Spring beans automatically in the application context. So, there is no need to configure the bean explicitly either using XML-based or Java-based configuration.

The following are the stereotype annotations in Spring:

  • @Component
  • @Service
  • @Repository
  • @Controller

Let's look at the following CustomerService implementation class. Its implementation is annotated with @Component. Please refer to the following code:

@Component
public class CustomerServiceImpl implements CustomerService {

@Override
public void customerService() {
System.out.println("This is call customer services");

}

}

In the previous code, the CustomerServiceImpl class is annotated with the @Component annotation. This means the class that is marked with the @Component annotation is considered the bean, and the component-scanning mechanism of Spring scans that class, creates a bean of this class, and pulls it into the application context. So, no need to configure that class explicitly as the bean is either using XML or Java. Spring automatically creates the bean of the CustomerServiceImpl class because it is annotated with @Component.

In Spring, @Service, @Repository, and @Controller are meta annotations for the @Component annotation. Technically, all annotations are the same and provide the same result, such as creating a bean in Spring context; but we should use more specific annotations at different layers of the application because it specifies the intent better, and additional behavior might rely on it in the future.

The following diagram describes the stereotype annotation with an appropriate layer:

As per the previous example, @Component is good enough to create a bean of CustomerService. But CustomerService is a service layer class, so as per bean configuration best practices, we should use @Services instead of the generic annotation @Component. Let's look at the following code for the same class annotated with the @Service annotation:

@Service
public class CustomerServiceImpl implements CustomerService {

@Override
public void customerService() {
System.out.println("This is call customer services");
}

}

Let's see another example of the @Repository annotation:

@Repository
public class JdbcCustomerRepository implements CustomerRepository {

}

In the previous example, the class is annotated with the @Repository annotation because the CustomerRepository interface is working at the Data Access Object (DAO) layer of the application. As per bean configuration best practices, we have used the @Repository annotation instead of the @Component annotation.

In a real-life scenario, you might face very rare situations where you will need to use the @Component annotation. Mostly, you will be using the @Controller@Service, and @Repository annotations. @Component should be used when your class does not fall into either of three categories: service, controller, DAO.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset