Spring 注解@Controller 和@Service 一样吗? [英] Is Spring annotation @Controller same as @Service?

查看:29
本文介绍了Spring 注解@Controller 和@Service 一样吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring 注解 @Controller@Service 一样吗?

Is Spring annotation @Controller same as @Service?

我有关于 @Controller 的想法,它可以用于 URL 映射和调用业务逻辑.

I have idea about @Controller which can be used for URL mapping and invoking business logic.

while @Service 用于注解包含业务逻辑的服务类.

while @Service used to annotate service class which contains business logic.

我可以用@Controller代替@Service来注解Service类吗?

Can I use @Controller instead of @Service to annotate Service class?

推荐答案

不,它们彼此非常不同.

No, they are pretty different from each other.

两者都是 @Component 注释的不同特化(实际上,它们是同一接口的两种不同实现),因此可以通过类路径扫描发现两者(如果您在 XML 中声明它)配置)

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 注释用于您的服务层,用于注释执行服务任务的类,通常您不使用它,但在许多情况下,您使用此注释来表示最佳实践.例如,您可以直接调用 DAO 类将一个对象持久化到您的数据库,但这太可怕了.调用一个调用 DAO 的服务类是很不错的.这是执行关注点分离模式的一件好事.

@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注解是Spring MVC框架(用于实现Web应用的Spring框架的组件)中使用的注解.@Controller 注释指示特定类充当控制器的角色.@Controller 注解作为注解类的构造型,表明它的作用.调度程序扫描这些带注释的类以查找映射方法并检测@RequestMapping 注释.

@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.

因此,查看 Spring MVC 架构,您有一个 DispatcherServlet 类(您在 XML 配置中声明),它代表一个前端控制器,将所有 HTTP 请求分派到适当的控制器类(由 @Controller 注释).此类通过其方法执行业务逻辑(并可调用服务).这些类(或其方法)通常也使用 @RequestMapping 批注进行批注,以指定控制器及其方法处理的 HTTP 请求.

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.

例如:

@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();
    }

这个类是一个控制器.

此类处理所有指向/appointments"文件夹"的 HTTP 请求,特别是 get 方法是处理所有指向/appointments"文件夹的 GET HTTP 请求的方法.

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.

这篇关于Spring 注解@Controller 和@Service 一样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆