Java Spring MVC中的@Named注解
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18540696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@Named annotation in Spring MVC
提问by Dino Tw
Per Spring 3 document, The IoC container, the @Named
annotation is a standard equivalent to the @Component
annotation.
根据 Spring 3 文档The IoC container,@Named
注解是与注解等效的标准@Component
。
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
由于@Repository
、@Service
和@Controller
都是@Component
,我尝试@Named
在我的 Spring MVC 应用程序中使用它们。它工作正常。但是我发现替换的@Controller
似乎有一个错误。在控制器类中,最初是
@Controller
public class MyController{
...
}
It works fine. When I changed @Controller
to @Named
它工作正常。当我改变@Controller
到@Named
@Named
public class MyController{
...
}
It failed with error:
它失败并出现错误:
"No mapping found for HTTP request with URI ...".
“未找到带有 URI 的 HTTP 请求的映射...”。
But if I added @RequestMapping
to the class as follow
但是如果我@RequestMapping
按如下方式添加到课程中
@Named
@RequestMapping
public class MyController{
...
}
It would work as expected.
它会按预期工作。
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?
对于@Repository
and @Service
,我可以简单地将它们替换@Named
为没有问题。但是更换@Controller
需要额外的工作。配置中有什么我遗漏的吗?
采纳答案by Sotirios Delimanolis
@Named
works the same as @Component
. However, the annotations @Controller
, @Service
, and @Repository
are more specific.
@Named
与@Component
. 但是,注释@Controller
、@Service
和@Repository
更具体。
From the Spring docs:
来自 Spring文档:
@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.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.
@Component
是任何 Spring 管理的组件的通用构造型。@Repository
、@Service
和@Controller
是@Component
更具体用例的特化,例如,分别在持久层、服务层和表示层中。例如,这些构造型注释是切入点的理想目标。这也有可能是
@Repository
,@Service
和@Controller
可以携带Spring Framework的未来版本中为更多的语义。因此,如果您在使用@Component
或@Service
用于您的服务层之间进行选择,@Service
显然是更好的选择。同样,如上所述,@Repository
已经支持作为持久层中自动异常转换的标记。
Thissection explains the difference with @Named
.
本节说明与 的区别@Named
。
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 的DispatcherServlet
(MVC 配置WebApplicationContext
)不是在寻找Component
,而是在寻找@Controller
. 所以当它扫描你的类时,它不会在@Named
. 以类似的方式,事务管理@Transactional
寻找@Service
and @Repository
,而不是更通用的@Component
。
回答by Abhilash reddy
All @Repository
, @Service
and @Controller
are mainly for declaring Spring beans, apart from that it gives extra information to Spring about the type of bean like controller, dao etc
所有@Repository
,@Service
并@Controller
主要用于声明Spring bean的,除了它提供了有关豆状控制器,刀等类型的额外信息春