在Spring MVC的@Named注释 [英] @Named annotation in Spring MVC

查看:606
本文介绍了在Spring MVC的@Named注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每春天3号文件,<一个href=\"http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-named\">The IoC容器,@Named注释是一个标准相当于@Component注释。
由于@Repository,@Service和@Controller都是@Component,我想在我的Spring MVC应用程序所使用@Named为他们所有。它工作正常。但我发现更换@Controller似乎有BUG。在控制器类,原来,这是

Per Spring 3 document, The IoC container, @Named annotation is a standard equivalent to the @Component annotation. 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 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请求映射......结果
但如果我说@RequestMapping以班级为后续

It failed with error "No mapping found for HTTP request with URI ...".
But if I added @RequestMapping to the class as follow

@Named
@RequestMapping
public class MyController{
     ...
 }

如预期它的工作。

It would work as expected.

有关@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.

从春天<一个href=\"http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-stereotype-annotations\">docs:

@Component为任何Spring管理组件的通用刻板印象。
  @Repository,@Service和@Controller是特例
  @Component更具体的使用情况下,例如,在
  持久性,服务和presentation层,分别为。

@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可携带的未来版本中更多语义
  Spring框架。因此,如果你正在使用@Component之间进行选择
  或@服务为您服务层,@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.

<一个href=\"http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-standard-annotations\">This部分解释了区别 @Named

很多组件,比如Spring的 DispatcherServlet的(MVC配置在的WebApplicationContext )是不是在找组件,他们正在寻找 @Controller 。因此,当它扫描你的类,它不会在 @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注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆