Can @Component, @repository, @service annotations on a class be interchangeable in spring’s integrated framework? Or what’s the difference between these notes? Answer 1 quotes a paragraph from spring’s official documentation: Prior to Spring2.0, the @repository annotation could be used to tag any class to indicate that it was used to perform database-related operations (i.e. DAO objects), and to automatically handle exceptions generated by database operations. In Spring2.5, more Spring class annotations were introduced: @ Component, @ Service, @ Controller. Component is a generic Spring container-managed singleton bean Component. The @repository, @Service, and @Controller annotation components are functional annotations for different usage scenarios. Therefore, when you annotate a class with @Component, it means that you can also use @repository, @Service, and @Controller instead, and these annotations have a lot more functionality. Finally, if you don’t know whether to use @Service or @Component annotations in the business layer of your project. Then @service is a better choice. As mentioned above, @repository is already supported as a marker in your persistence layer to automatically handle exceptions generated by database operations. Because the native Java operation database generated by the exception of only several definitions, but there are many reasons for the database exception, so the database operation error detection caused a certain impact; Spring extends the native persistence layer with more exceptions described for different reasons. Therefore, the @repository annotated class will handle any exceptions thrown by a database operation, and will instead throw spring-specific exceptions for troubleshooting.)

Answer 2 These annotations are almost the same: classes decorated by these annotations are scanned by Spring and injected into Spring’s bean container. Here, there are two annotations that are not interchangeable with other annotations: ● Beans annotated by @controller are used by the Spring-MVC framework. ● @repository will be used as a bean for persistence layer operations (database). If you want to use custom Component annotations, add @Component to the new annotations you define: @Component @Scope(“prototype”) public @interface ScheduleJob {… }

In this way, all @ScheduleJob annotated classes can be injected into the Spring container for management. All we need to do is write some new code to handle this custom annotation and do what we want. Component can be hosted in the Spring container for management. @service, @Controller, @repository = {@Component + some specific functionality}. This means that the annotations are partially functionally the same. Of course, the following three annotations are used to layer our application: ● The @Controller annotation class handles front-end requests, forwards, and redirects. The @Service annotation class handles the business logic. The @Repository annotation class acts as DAO Objects (Data Access Objects). These classes can operate directly on the database. Loose coupling is realized between codes, and the call between codes is clear and clear, which is convenient for project management; Imagine how hard it would be to extend and maintain the @Controller annotation if all the request forwarding, business processing, and database manipulation code were all in one place. @Component, @service, @Controller, @repository are spring annotations that can be scanned by the Spring framework and injected into the Spring container for management. The other three annotations are extensions of this annotation with specific functionality. The @repository annotation in the persistence layer translates native exceptions thrown by database operations into Spring’s persistence layer exceptions. ● The @Controller layer is the annotation of Spring-MVC, with the function of forwarding and redirecting requests. ● The @service layer is a business logic layer annotation, which simply notes that the class is in the business logic layer. ● Use these annotations to layer the application, you can separate the request processing, obligation logic processing, database operation processing, decouple the code, and facilitate the maintenance and development of future projects. Stackoverflow links: stackoverflow.com/questions/6… Welcome Java engineers who have worked for one to five years to join Java Architects: 697558955 group provides free Java architecture learning materials (which have high availability, high concurrency, high performance and distributed, Jvm performance tuning, Spring source code, MyBatis, Netty, Redis, Kafka, Mysql, Zookeeper, Tomcat, Docker, Dubbo, multiple knowledge architecture data, such as the Nginx) reasonable use their every minute and second time to learn to improve yourself, don’t use “no time” to hide his ideas on the lazy! While young, hard to fight, to the future of their own account!