问题描述
Spring注释 @Controller
与 @Service
相同吗?
我有关于 @Controller
的想法,可以用于 URL
映射和调用业务逻辑。 p>
而 @Service
用于注释包含业务逻辑的服务类。
我可以使用 @Controller
而不是 @Service
来注释Service类吗?
两者都是不同的专业化
strong> @Component 注释(实际上,它们是同一个接口的两个不同的实现),因此可以通过类路径扫描发现(如果您在XML配置中声明它)@Service 注释用于服务层,并注释执行服务任务的类,通常不使用它,但在许多情况下,您使用此注释来表示最佳实践。例如,您可以直接调用DAO类来将对象持久化到数据库中,但这是可怕的。调用一个调用DAO的服务类是非常好的。这是执行关注模式分离的好东西。
@Controller 注释是Spring MVC框架中使用的注释Spring框架用于实现Web应用程序)。 @Controller注释指示特定类充当控制器的角色。 @Controller注释充当注释类的构造型,指示其角色。分配器扫描这些注释类的映射方法和检测@RequestMapping注释。
看看Spring MVC架构,你有一个DispatcherServlet类(你在XML配置),它代表一个前端控制器,它将所有的HTTP请求发送到相应的控制器类(由@Controller注释)。这个类通过其方法执行业务逻辑(并且可以调用服务)。这些类(或其方法)通常还使用 @RequestMapping 注释注释,用于指定控制器及其方法处理的HTTP请求。
例如:
@Controller
@RequestMapping(/ appointments)
public class AppointmentsController {
私人会议预约书
@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请求的方法。
我希望现在对你更清楚。
Is Spring annotation @Controller
same as @Service
?
I have idea about @Controller
which can be used for URL
mapping and invoking business logic.
while @Service
used to annotate service class which contains business logic.
Can I use @Controller
instead of @Service
to annotate Service class?
No, they are pretty different from each other.
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.
这篇关于Spring注释@Controller和@Service是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!