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 @Namedannotation is a standard equivalent to the @Componentannotation.
根据 Spring 3 文档The IoC container,@Named注解是与注解等效的标准@Component。
Since @Repository, @Service, and @Controllerare all @Component, I tried to used @Namedfor all of them in my Spring MVC application. It works fine. But I found the replacement of @Controllerseems 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 @Controllerto @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 @RequestMappingto the class as follow
但是如果我@RequestMapping按如下方式添加到课程中
@Named
@RequestMapping
public class MyController{
...
}
It would work as expected.
它会按预期工作。
For @Repositoryand @Service, I can simply replace them with @Namedwith no issue. But the replacement of @Controllerneeds extra work. Is there anything I am missing in the configuration?
对于@Repositoryand @Service,我可以简单地将它们替换@Named为没有问题。但是更换@Controller需要额外的工作。配置中有什么我遗漏的吗?
采纳答案by Sotirios Delimanolis
@Namedworks the same as @Component. However, the annotations @Controller, @Service, and @Repositoryare more specific.
@Named与@Component. 但是,注释@Controller、@Service和@Repository更具体。
From the Spring docs:
来自 Spring文档:
@Componentis a generic stereotype for any Spring-managed component.@Repository,@Service, and@Controllerare specializations of@Componentfor 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@Controllermay carry additional semantics in future releases of the Spring Framework. Thus, if you are choosing between using@Componentor@Servicefor your service layer,@Serviceis clearly the better choice. Similarly, as stated above,@Repositoryis 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 @Transactionallooks for @Serviceand @Repository, not for the more generic @Component.
许多组件,例如 Spring 的DispatcherServlet(MVC 配置WebApplicationContext)不是在寻找Component,而是在寻找@Controller. 所以当它扫描你的类时,它不会在@Named. 以类似的方式,事务管理@Transactional寻找@Serviceand @Repository,而不是更通用的@Component。
回答by Abhilash reddy
All @Repository, @Serviceand @Controllerare 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的,除了它提供了有关豆状控制器,刀等类型的额外信息春

