search

Home  >  Q&A  >  body text

What is the difference between these four annotations in Java? Why are they mixed in the project?

@Component:
@Controller
@Repository
@Service

曾经蜡笔没有小新曾经蜡笔没有小新2775 days ago599

reply all(6)I'll reply

  • 世界只因有你

    世界只因有你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.

    • @ControllerSpecial, exclusively handled by Spring MVC and cannot be replaced by the other three.

    reply
    0
  • 阿神

    阿神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

    reply
    0
  • 巴扎黑

    巴扎黑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.

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-17 10:02:19

    It’s just that the semantics are different, but the functions are the same

    reply
    0
  • 某草草

    某草草2017-05-17 10:02:19

    The effect is the same**

    reply
    0
  • 習慣沉默

    習慣沉默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

    reply
    0
  • Cancelreply