#Role of Annotations _ I've never considered 'Why' when using @Controller, @Service, @Repository ….

#While Enrolling Spring Bean, How Does Annotation Work

#Foreword

I've been using this annotation on each class, but I've never considered "Why". So, I want to know How Does Annotation Works. Before we begin, we should define class, object, fields, methods or anything related to Java Bean(Java Object), In order to understand spring bean, I think we need to define those concepts first precisely. Shortly, I can define them like below.

(I tried to desribe this with my words)

A.Class - Blueprint / template

: Class is a blueprint or template which have method and fields, Class is a type of object.

B.Object - instances created by 'new'

: Object is the instances of that class by using "create with 'new' keyword".

C.methods - behavior of objects

: Method is functions defined within a class that represent the behavior of objects

D.fields(member variables) - state of objects

: Fields(member variables) can be described data members of a class that represent the state of objects

#We have two options to enroll class as spring bean

  1. @Component

    : When we use specific annotation like @Controller, @Service, @Repository, we can see @Component annotation, by using it, we can enroll class as Spring bean.

  2. @Configuration + @Bean

    : We create class to enroll class as spring bean with @Configuration + @Bean

To specify the conclusion first, What's important is @Component.

#@Controller, @RestController, @Service, @Repository

: When we click annotation with ctrl + left click, we can see the statement for each annotation.

  1. @Controller - Used for handling web request

@Controller
public class ProductController {
}
@RestController // this is @Controller + @ResponseBody
public class ProductController {
}

: @Controller has @Component, @RestController = @Controller + @ResponseBody

  1. @Service - Used for holding business logic

@Service
public class ProductService {
}

  1. @Repository - Interacts with database that is responsible for data access

@Repository
public class ProductRepository {
}

#@Configuration + @Bean

: @Configuration also have "@Component"

@Configuration
public class ApplicationConfig {
    @Bean
    public ProductService productService() {
        return new ProductService();
    }
}

@Bean have no @Component annotation, it's used with @Configuration

#Automated Way(automatic) vs Hand-Operated Way(manual) @Component vs @Configuration+@Bean

: Is there specific reason why people prefer @Component over @Configuration + @Bean..............................?

#Automated > Hand-Operated

To specify the conclusion in advance, simply put, it's to prevent human error.

  1. For Convenience

    : When we use @Configuration + @Bean, Developer needs to create another class for configuration, requiring more effort, which expands the maintenance scope.

  2. To Prevent Typos

    : while configuring @Configuration + @Bean, we are more likely to make typos. By using the automated way(@Component - @Controller, @Service, @Repository), We can prevent such errors.(making typos)

In Conclusion, By using specific annotaitons that are automated (@Controller, @Service, @Repository), we can prevent making human errors and reduce the maintenance scope.