Spring 可以将@Service对象用作@controller吗?
我有一个主要提供JSON API的服务器。目前我们有@controller,它直接将REST端点委托给@Service类中的方法Spring 可以将@Service对象用作@controller吗?,spring,spring-mvc,Spring,Spring Mvc,我有一个主要提供JSON API的服务器。目前我们有@controller,它直接将REST端点委托给@Service类中的方法 这导致了大量不必要的样板代码。如果我只是使用@Controller和@RequestMapping注释来注释@Service类(例如,事务调用出错?服务之间的依赖性问题?),我可能会遇到问题吗?MVC模式的存在有一个很好的理由,让每一层都完成它们的工作,并且它们之间的耦合度较低。每个刻板印象都有存在的理由 你在说什么样的样板代码@控制器和@RequestMapping
这导致了大量不必要的样板代码。如果我只是使用@Controller和@RequestMapping注释来注释@Service类(例如,事务调用出错?服务之间的依赖性问题?),我可能会遇到问题吗?MVC模式的存在有一个很好的理由,让每一层都完成它们的工作,并且它们之间的耦合度较低。每个刻板印象都有存在的理由
你在说什么样的样板代码@控制器和@RequestMapping非常简单 MVC模式的存在有一个很好的理由,让每一层都完成它们的工作,并且它们之间的耦合度很低。每个刻板印象都有存在的理由
你在说什么样的样板代码@控制器和@RequestMapping非常简单 如果在同一个bean上同时使用
@Service
和@Controller
,则很有可能为一个类创建两个bean,这通常不是您想要的
这是因为@Controller
通常在子上下文(DispatcherServlet)中加载
如果您的所有bean都加载到DispatcherServlet上下文中,您就可以了
编辑:我在iPhone上回答了这个问题,所以我无法详细说明
我认为这很可能是一个问题,因为它实际上与您使用的注释类型无关,而是与组件扫描以及如何将组件扫描分配给应用程序上下文有关 大多数人所做的是扫描根
WebApplicationContext
加载的带有@Service
的组件,它可以与dispatcher servlet上下文相同,但通常不是
让我举个例子:
通常,您会看到具有以下内容的applicationContext.xml:
<context:component-scan base-package="com.blah">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<context:component-scan base-package="com.blah" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
然后是具有以下内容的分派servlet上下文XML:
<context:component-scan base-package="com.blah">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<context:component-scan base-package="com.blah" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
请注意第一个如何排除@Controller
,第二个如何明确包括
也就是说,如果您只有一个WebApplicationContext,并且它与DispatcherServlet共享,那么您可以使用
@RequestMapping
对@Service
进行注释,它应该可以很好地工作,而不是我建议的那样。如果您在同一个bean上同时使用@Service
和@Controller
,那么这是一个好的选择您可能会为一个类创建两个bean,这通常不是您想要的
这是因为@Controller
通常在子上下文(DispatcherServlet)中加载
如果您的所有bean都加载到DispatcherServlet上下文中,您就可以了
编辑:我在iPhone上回答了这个问题,所以我无法详细说明
我认为这很可能是一个问题,因为它实际上与您使用的注释类型无关,而是与组件扫描以及如何将组件扫描分配给应用程序上下文有关 大多数人所做的是扫描根
WebApplicationContext
加载的带有@Service
的组件,它可以与dispatcher servlet上下文相同,但通常不是
让我举个例子:
通常,您会看到具有以下内容的applicationContext.xml:
<context:component-scan base-package="com.blah">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<context:component-scan base-package="com.blah" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
然后是具有以下内容的分派servlet上下文XML:
<context:component-scan base-package="com.blah">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<context:component-scan base-package="com.blah" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
请注意第一个如何排除@Controller
,第二个如何明确包括
也就是说,如果您只有一个WebApplicationContext,并且它与DispatcherServlet共享,那么您可以使用
@RequestMapping
注释@Service
,它应该可以正常工作,而不是我建议的那样。@控制器将入口和端点委托给一个请求/响应
@服务是向这些@Controllers提供逻辑,这些逻辑必须是@Controllers所未知的
在@Controllers中,您必须@Autowired您的@Services依赖项。在@Services中,还有其他逻辑依赖项,如DAO等
一个典型的Rest@控制器可能如下所示:
@Controller
public class UserWSController {
@Autowired
private UserService userService;
@RequestMapping(value = "/{userId}")
@ResponseBody
public User getUserPublicInfo(@PathVariable int userId) {
return this.userService.getUserById(userId);
}
}
@控制器将入口和终点委托给一个请求/响应 @服务是向这些@Controllers提供逻辑,这些逻辑必须是@Controllers所未知的 在@Controllers中,您必须@Autowired您的@Services依赖项。在@Services中,还有其他逻辑依赖项,如DAO等 一个典型的Rest@控制器可能如下所示:
@Controller
public class UserWSController {
@Autowired
private UserService userService;
@RequestMapping(value = "/{userId}")
@ResponseBody
public User getUserPublicInfo(@PathVariable int userId) {
return this.userService.getUserById(userId);
}
}
如果我只是简单地注释我的@服务
带有@Controller
和@RequestMapping
注释的类
如果使用@Service
注释类型,则不需要使用@Controller
注释该类型。只需确保类型具有@RequestMapping
注释。处理控制器处理程序方法映射的RequestMappingHandlerMapping
在类型级别检查@Controller
和@RequestMapping
注释。你两者都需要
除此之外,这只是一个设计和关注点分离的问题。在Spring技术层面,您不会有任何其他问题
(请注意,您必须通过DispatcherServlet
的WebApplicationContext
加载bean,以便RequestMappingHandlerMapping
获取它。)
如果我只是简单地注释我的@服务
带有@Controller
和@RequestMapping
注释的类
如果使用@Service
注释类型,则不需要使用@Controller
注释该类型。只是妈妈