Defining beans and wiring

Let's address the first question; how does the Spring IoC container know which beans to create?

 

We need to tell the Spring IoC container which beans to create. This can be done using @Repository or @Component or @Service annotations on the classes for which beans have to be created. All these annotations tell the Spring Framework to create beans for the specific classes where these annotations are defined.

 

A @Component annotation is the most generic way of defining a Spring bean. Other annotations have more specific context associated with them. @Service annotation is used in business service components. @Repository annotation is used in Data Access Object (DAO) components.

We use @Repository annotation on

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.