@Component:
@Controller
@Repository
@Service
世界只因有你2017-05-17 10:02:19
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.
阿神2017-05-17 10:02:19
@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
巴扎黑2017-05-17 10:02:19
@Controller is used in controller, @Service is used in service, @Repository is used in dao, and @Component is used in other unclear places.
滿天的星座2017-05-17 10:02:19
It’s just that the semantics are different, but the functions are the same
習慣沉默2017-05-17 10:02:19
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