<legend id='B7gGO'><style id='B7gGO'><dir id='B7gGO'><q id='B7gGO'></q></dir></style></legend>

      • <bdo id='B7gGO'></bdo><ul id='B7gGO'></ul>
    1. <small id='B7gGO'></small><noframes id='B7gGO'>

      <i id='B7gGO'><tr id='B7gGO'><dt id='B7gGO'><q id='B7gGO'><span id='B7gGO'><b id='B7gGO'><form id='B7gGO'><ins id='B7gGO'></ins><ul id='B7gGO'></ul><sub id='B7gGO'></sub></form><legend id='B7gGO'></legend><bdo id='B7gGO'><pre id='B7gGO'><center id='B7gGO'></center></pre></bdo></b><th id='B7gGO'></th></span></q></dt></tr></i><div id='B7gGO'><tfoot id='B7gGO'></tfoot><dl id='B7gGO'><fieldset id='B7gGO'></fieldset></dl></div>

        <tfoot id='B7gGO'></tfoot>

        Spring MVC 中的 @Named 注解

        时间:2023-07-26

            <tbody id='qZc2S'></tbody>

            <bdo id='qZc2S'></bdo><ul id='qZc2S'></ul>

            <small id='qZc2S'></small><noframes id='qZc2S'>

          • <legend id='qZc2S'><style id='qZc2S'><dir id='qZc2S'><q id='qZc2S'></q></dir></style></legend>
            <tfoot id='qZc2S'></tfoot>
            <i id='qZc2S'><tr id='qZc2S'><dt id='qZc2S'><q id='qZc2S'><span id='qZc2S'><b id='qZc2S'><form id='qZc2S'><ins id='qZc2S'></ins><ul id='qZc2S'></ul><sub id='qZc2S'></sub></form><legend id='qZc2S'></legend><bdo id='qZc2S'><pre id='qZc2S'><center id='qZc2S'></center></pre></bdo></b><th id='qZc2S'></th></span></q></dt></tr></i><div id='qZc2S'><tfoot id='qZc2S'></tfoot><dl id='qZc2S'><fieldset id='qZc2S'></fieldset></dl></div>

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

                  问题描述

                  根据 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模板网!

                    <i id='C38GH'><tr id='C38GH'><dt id='C38GH'><q id='C38GH'><span id='C38GH'><b id='C38GH'><form id='C38GH'><ins id='C38GH'></ins><ul id='C38GH'></ul><sub id='C38GH'></sub></form><legend id='C38GH'></legend><bdo id='C38GH'><pre id='C38GH'><center id='C38GH'></center></pre></bdo></b><th id='C38GH'></th></span></q></dt></tr></i><div id='C38GH'><tfoot id='C38GH'></tfoot><dl id='C38GH'><fieldset id='C38GH'></fieldset></dl></div>
                      <tbody id='C38GH'></tbody>

                    1. <legend id='C38GH'><style id='C38GH'><dir id='C38GH'><q id='C38GH'></q></dir></style></legend>

                    2. <tfoot id='C38GH'></tfoot>
                        <bdo id='C38GH'></bdo><ul id='C38GH'></ul>
                      • <small id='C38GH'></small><noframes id='C38GH'>