根据 Spring 3 文档,IoC容器,@Named
注解是标准等价于@Component
注解.
Per Spring 3 document, The IoC container, the @Named
annotation is a standard equivalent to the @Component
annotation.
由于@Repository
、@Service
、@Controller
都是@Component
,所以我尝试使用@Named
用于我的 Spring MVC 应用程序中的所有这些.它工作正常.但我发现 @Controller
的替换似乎有一个错误.在控制器类中,原来是
Since @Repository
, @Service
, and @Controller
are all @Component
, I tried to used @Named
for all of them in my Spring MVC application. It works fine. But I found the replacement of @Controller
seems to have a bug. In the controller class, originally, it was
@Controller
public class MyController{
...
}
它工作正常.当我将 @Controller
更改为 @Named
It works fine. When I changed @Controller
to @Named
@Named
public class MyController{
...
}
失败并出现错误:
没有为带有 URI ... 的 HTTP 请求找到映射".
"No mapping found for HTTP request with URI ...".
但是如果我将 @RequestMapping
添加到类中,如下所示
But if I added @RequestMapping
to the class as follow
@Named
@RequestMapping
public class MyController{
...
}
它会按预期工作.
对于 @Repository
和 @Service
,我可以简单地用 @Named
替换它们,没有任何问题.但是 @Controller
的替换需要额外的工作.配置中有什么我遗漏的吗?
For @Repository
and @Service
, I can simply replace them with @Named
with no issue. But the replacement of @Controller
needs extra work. Is there anything I am missing in the configuration?
@Named
与 @Component
的作用相同.但是,注释 @Controller
、@Service
和 @Repository
更具体.
@Named
works the same as @Component
. However, the annotations @Controller
, @Service
, and @Repository
are more specific.
来自 Spring 文档:
From the Spring docs:
@Component
是任何 Spring 管理的组件的通用构造型.@Repository
、@Service
和 @Controller
是@Component
用于更具体的用例,例如,在分别是持久层、服务层和表示层.
@Component
is a generic stereotype for any Spring-managed component.@Repository
,@Service
, and@Controller
are specializations of@Component
for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.
例如,这些原型注释是理想的目标切入点.@Repository
、@Service
和@Controller
可能会在未来版本的春天框架.因此,如果您在使用 @Component
之间进行选择或 @Service
为您的服务层,@Service
显然更好选择.同样,如上所述,@Repository
已经被支持作为持久性中自动异常翻译的标记层.
For example, these stereotype annotations make ideal targets for
pointcuts. It is also possible that @Repository
, @Service
, and
@Controller
may carry additional semantics in future releases of the
Spring Framework. Thus, if you are choosing between using @Component
or @Service
for your service layer, @Service
is clearly the better
choice. Similarly, as stated above, @Repository
is already supported
as a marker for automatic exception translation in your persistence
layer.
本部分解释了与 @Named
的区别.
This section explains the difference with @Named
.
许多组件,例如 Spring 的 DispatcherServlet
(WebApplicationContext
中的 MVC 配置)不是在寻找 Component
,而是在寻找 @控制器
.所以当它扫描你的类时,它不会在 @Named
中找到它.以类似的方式,使用 @Transactional
的事务管理查找 @Service
和 @Repository
,而不是更通用的 @Component代码>.
Many components, like Spring's DispatcherServlet
(MVC configuration in WebApplicationContext
) aren't looking for Component
, they are looking for @Controller
. So when it scans your class, it won't find it in @Named
. In a similar fashion, transaction management with @Transactional
looks for @Service
and @Repository
, not for the more generic @Component
.
这篇关于Spring MVC 中的 @Named 注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!