読みました:
Spring MVC の ApplicationContext と WebApplicationContext の違いは何ですか?
<context:component-scan /> がディスパッチャ コンテキストではなくアプリケーション コンテキストにある場合、@RequestMapping アノテーションが機能しない(これについては後で詳しく説明します)
他にもいくつかありますが、これらのどれも質問に答えていません:
Spring MVC アプリケーションの ROOT コンテキストに存在する場合、スコープが<context:component-scan.../>
制限されるのはなぜですか?
私の理解では、指定されたパッケージ内のすべてのクラスのスキャンが発生し、ステレオタイプ化された Bean@Component
またはそのサブステレオタイプ ( @Repository
、@Service
および@Controller
) のいずれかがインスタンス化されます。
与えられた:
applicationContext.xml (ルート コンテキスト)
<beans...>
...
<context:component-scan base-package="com.myproject"/>
<context:property-placeholder
ignore-resource-not-found="true"
location="classpath:default.properties, file:///etc/gallery/gallery.properties"/>
</beans>
main-servlet.xml (サーブレット コンテキスト)
<beans ...>
...
<mvc:annotation-driven/>
<mvc:resources mapping="/image/**" location="file:/${gallery.location}" />
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
...
</beans>
com/myproject/web/MainController.java
package com.myproject.web;
@Controller
public class MainController
{
...
@RequestMapping("/gallery/**")
public String gallery(ModelMap modelMap, HttpServletRequest req, HttpServletResponse resp) throws IOException
{
...
}
}
Spring のドキュメントには、ルート コンテキストでインスタンス化されたすべての Bean が共有され、個々のサーブレット アプリケーション コンテキストで使用できると記載されています。したがって、<context:...>
ルート コンテキストでの 2 つの宣言により、サーブレット コンテキストで表示される Bean が生成されます。しかし、そうではないようです。サーブレットのコンテキストで<context:component-scan.../>
両方を繰り返す必要があります。<context:property-placeholder.../>
<context:component-scan.../>
サーブレット コンテキストでを省略すると、
Sep 15, 2015 10:08:16 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/gallery/habitat/20150813] in DispatcherServlet with name 'main'
Sep 15, 2015 10:08:16 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/error] in DispatcherServlet with name 'main'
@Controller
が解決されなかったことを示します。
処理されていないプロパティ参照を使用<context:property-placeholder.../>
した注釈の結果を省略すると、私の場合、いくつかの壊れたリンクが発生します。@Value
これらの<context:.../>
ディレクティブは両方とも Bean のインスタンス化につながるため、ドキュメントとは正反対に、Bean が子コンテキストで表示されない理由について混乱しています。また、component-scan
ステートメントが 2 つあると、コントローラー Bean が 2 回インスタンス化されることはありませんか?
<context:component-scan /> がディスパッチャ コンテキストではなくアプリケーション コンテキストにある場合、@RequestMapping アノテーションが機能しないことに関しては<mvc:annotation-driven />
、アプリ コンテキストにあり、ここでの回答では 2 つのcomponent-scan
ステートメントが必要な理由が説明されていません。
「魔法」がどのように機能するかを完全に理解し、何かを微調整したときにどのように動作するかを予測できない限り、「魔法」を使用するのは本当に不快です。したがって、「両方の場所に追加して先に進む」という「解決策」は受け入れられません。