@Service, @Repository are also @Service,@Repository也是@Component, but they are given different semantics to facilitate code reading. In fact, it doesn’t matter if you use these three casually, Spring will not report an error.
@ControllerSpecial, exclusively handled by Spring MVC and cannot be replaced by the other three.
These four annotations have the same operating effect on the machine. There is only a semantic difference, which tells people who read the code which level component this class is
are all used to define
Bean
@Component
is the most versatile@Service
,@Repository
are also@Service
,@Repository
也是@Component
, but they are given different semantics to facilitate code reading. In fact, it doesn’t matter if you use these three casually, Spring will not report an error.@Controller
Special, exclusively handled by Spring MVC and cannot be replaced by the other three.@Service is used to annotate business layer components
@Controller is used to annotate control layer components (controller in spring)
@Repository is used to annotate data access components, namely DAO components
@Component generally refers to components. When components are difficult to classify, we can use this annotation to annotate them
@Controller is used in controller, @Service is used in service, @Repository is used in dao, and @Component is used in other unclear places.
It’s just that the semantics are different, but the functions are the same
The effect is the same**
These four annotations have the same operating effect on the machine. There is only a semantic difference, which tells people who read the code which level component this class is