What is the difference between these four annotations in Java? Why are they mixed in the project?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-17 10:00:19
0
6
603

@Component:
@Controller
@Repository
@Service

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(6)
世界只因有你

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.

  • @ControllerSpecial, 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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template