2013-08-30 15 views
10

Spring3の文書、The IoC container@Namedアノテーションは、@Component注釈と同等の標準です。 @Repository以来Spring MVCの@Named注釈

@Service、および@Controllerはすべて@Componentあり、私はSpring MVCのアプリケーションでそれらのすべてのために@Namedを使用してみました。それはうまく動作します。しかし、@Controllerの置き換えにバグがあるようです。コントローラクラスでは、もともとは

@Controller 
public class MyController{ 
    ... 
} 

でした。

"No mapping found for HTTP request with URI ...".

しかし、私は従う

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

としてクラスに@RequestMappingを追加した場合に予想されるように、それは働くだろう:私は@Controller

@Namedから
@Named 
public class MyController{ 
    ... 
} 

を変更した場合はエラーで失敗しました。

@Repository@Serviceについては、私は単に@Namedに置き換えても問題ありません。しかし、@Controllerの交換には余分な作業が必要です。設定に欠けているものはありますか?

答えて

16

@Namedは、@Componentと同じです。ただし、注釈はより具体的です。注釈は@Controller,@Service、および@Repositoryです。

docsから:

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

Thisセクションでは、@Namedとの違いを説明します。 SpringのDispatcherServletWebApplicationContextでMVC構成)などの

多くのコンポーネントは、Componentを探して、彼らは@Controllerを探していますされていません。だからあなたのクラスをスキャンすると、それは@Namedで見つからないでしょう。同様の方法で、@Transactionalのトランザクション管理はの代わりに@Service@Repositoryを探します。

+0

DAOなど、別にそれはコントローラのような豆の種類について春に余分な情報を与えることから、春の豆を宣言するため、主にジェネリックBeanインジェクションのために '@ Component'を置き換えることができますが、Spring MVC固有の機能で' @ Repository'、 '@ Service'、' @Controller'を使用する必要がありますか? –

+0

@dinoようこそ。あなたが他の詳細とのより多くの回答を待っていない限り、この回答を受け入れることを検討してください。 –

3

すべて@Repository@Service@Controllerそれは私が安全にNamed` @ `を使用できることを意味してい

関連する問題