Sitemap
CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Member-only story

Spring boot @Controller VS @Component

--

Spring has different annotations for different purposes. Often the stereotype annotations are contradicted by the umbrella @Components annotation. Let’s start with the basic definitions, a Component is a generic stereotype for any spring-managed bean. It provides an optional parameter value of String type which indicated the logical name for that component, to be turned into a spring bean in the case of auto-detection, like by @ComponentScan.

Press enter or click to view image in full size
Spring stereotypes and web annotations

The component annotation from the org.springframework.stereotype package is further extended into specialized stereotypical annotations defining conceptually the roles of these types/methods in the overall architecture.

@Repository: Defines that the annotated class encapsulates the CRUD mechanisms as the DAO classes with the possible spring exceptions DataAccessException and PersistenceExceptionTranslationPostProcessor.

@Service: Defines that the annotated class offers some operation like supporting the business logic with no encapsulation methods. In most cases, it uses the repository classes to perform the CRUD functionality.

@Controller: Defines that the annotated class is mostly used in the presentation layer like a web controller in combination with other annotated handler methods. With Spring MVC namespace, @RestController annotation is furthermore…

--

--