問題描述
根據 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模板網!