@Controller ~ @Service
来源:互联网 发布:手机自动升级软件 编辑:程序博客网 时间:2024/06/01 09:34
Both are different specializations of @Component annotation (in practice, they're two different implementations of the same interface) so both can be discovered by the classpath scanning (if you declare it in your XML configuration)
@Service annotation is used in your service layer and annotates classes that perform service tasks, often you don't use it but in many case you use this annotation to represent a best practice. For example, you could directly call a DAO class to persist an object to your database but this is horrible. It is pretty good to call a service class that calls a DAO. This is a good thing to perform the separation of concerns pattern.
@Controller annotation is an annotation used in Spring MVC framework (the component of Spring Framework used to implement Web Application). The @Controller annotation indicates that a particular class serves the role of a controller. The @Controller annotation acts as a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations.
So looking at the Spring MVC architecture you have a DispatcherServlet class (that you declare in your XML configuration) that represent a front controller that dispatch all the HTTP Request towards the appropriate controller classes (annotated by @Controller). This class perform the business logic (and can call the services) by its method. These classes (or its methods) are typically annotated also with @RequestMapping annotation that specify what HTTP Request is handled by the controller and by its method.
For example:
@Controller@RequestMapping("/appointments")public class AppointmentsController { private final AppointmentBook appointmentBook; @Autowired public AppointmentsController(AppointmentBook appointmentBook) { this.appointmentBook = appointmentBook; } @RequestMapping(method = RequestMethod.GET) public Map<String, Appointment> get() { return appointmentBook.getAppointmentsForToday(); }
This class is a controller.
This class handles all the HTTP Request toward "/appointments" "folder" and in particular the get method is the method called to handle all the GET HTTP Request toward the folder "/appointments".
I hope that now it is more clear for you.
- @Controller ~ @Service
- @Component @Service @Controller @Repository
- :@Repository、@Service 和 @Controller。
- Controller Service Dao总结
- service 和 Controller 区别
- dao、service、controller、view
- @Component、@Repository @Service、@Controller
- Action(controller) service DAO
- @Component @Service @Controller @Repository
- @Component,@Service,@Controller,@Repository
- @Repository @Service @Controller @Component
- Controller->Service->Repository
- controller-service-dao-entity
- @Repository、@Service、@Controller、@Component
- @Component、@Repository、@Service、@Controller
- 解释@Component @Controller @Service @Repository
- 注解@Component @Controller @Service @Repository
- @Repository、@Service、@Controller 和 @Component
- android studio build.gradle说明
- 二叉搜索树的C++实现
- bash脚本,自动输入sudo的密码
- 5、TabPanel控件使用
- 四处踩坑的http通讯
- @Controller ~ @Service
- Ubuntu 16.04 安装使用MySQL
- static class 静态类(Java)
- 解决Win7下Jdeveloper(OAF)花屏的方法
- 大数据文本相似去重方案
- 深入分析 Java 中的中文编码问题
- 移动支付-支付宝篇
- Jmeter error: jmeter.samplers.SampleResult: sampleEnd called twice java.lang.Throwable: Invalid call
- Java学习提要——包装类与数据类型转换