applicationContext がサーブレットコンテキストのコントローラーを見つけられない 質問する

applicationContext がサーブレットコンテキストのコントローラーを見つけられない 質問する

applicationContext.xml と dispatcher-servlet.xml 構成を持つ Spring Web アプリがあります。 applicationContext.xml で定義しました<context:component-scan />が、アプリを実行すると、<context:component-scan />dispatcher-servlet.xml にも追加しない限り、コントローラーが見つかりません。 両方で同じ基本パッケージを使用しているため、これは問題ではありません。

私は混乱しています。考えapplicationContext.xml が dispatcher-servlet.xml の親であることがわかりました。<context:component-scan />applicationContext.xml を入れるだけで十分ではないでしょうか?

ウェブ

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

編集: また、dispatcher-servlet.xml で mvc:annotation-driven を使用しています。これは、コントローラーを取得するはずです (そう思っていました)。

編集 2: 設定ファイルは次のとおりです。applicationContext.xml から Spring Security と OAuth の設定をいくつか削除しました (セキュリティ上の理由と、おそらく関連性がないためです)。

アプリケーションコンテキスト.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
      http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="bar.foo"/>
<context:property-placeholder location="classpath:my.properties" />
<bean class="bar.foo.ServicesConfig" />

</beans>

ディスパッチャサーブレット.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="bar.foo.controller" />
<mvc:annotation-driven/>
<mvc:default-servlet-handler />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    <property name="order" value="2" />
</bean>

<bean id="contentViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
        </map>
    </property>
    <property name="defaultViews">
        <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
    </property>
    <property name="order" value="1" />
</bean>

</beans>

編集 3: これは興味深いですね。私のサービスと DAO クラスは、Web プロジェクトから参照する別のプロジェクト (JAR) にあります。Java ベースの構成を使用しており、applicationContext.xml から参照しています。

<bean class="bar.foo.config.ServicesConfig" />

つまり、Web プロジェクト (applicationContext.xml がある場所) には Controller アノテーションのみがあるということです。振り返ってみると、applicationContext.xml から context:component-scan を削除しても、@Controller アノテーション以外にはアノテーションがないため、影響はないはずです (修正: @Autowired アノテーションがいくつかあります)。ただし、applicationContext.xml から context:component-scan を削除すると、Controller (ディスパッチャー サーブレット スキャンで検出) が Service クラスを見つけられないというメッセージが表示されます。ServicesConfig への参照で十分ではないでしょうか。参照用の ServicesConfig クラスを次に示します。このクラスには、applicationContext.xml がスキャンしていたパッケージとは異なるサービス用の独自のコンポーネント スキャンがあります。

@Configuration
@ComponentScan({ "some.other.package", "another.package" })
@ImportResource({ "classpath:commonBeans.xml" })
@PropertySource({ "classpath:services.properties",
"classpath:misc.properties" })
public class ServicesConfig {
  // Bean definitions //
}

解決:

ルート コンテキストから context:component-scan を削除すると、コントローラーは自動接続されたサービス Bean を取得しなくなりました。これは、ルート コンテキストがサービス Java ベースの構成 Bean を参照しているものの、コンポーネントをスキャンするためのルート コンテキストが設定されていなかったためです。したがって、ルート コンテキスト (applicationContext.xml) にコンポーネント スキャンを追加すると、すべてが機能します。現在の状態は次のとおりです。

アプリケーションコンテキスト.xml:

<bean class="bar.foo.config.ServicesConfig" />
<context:component-scan base-package="bar.foo.config" />

ディスパッチャサーブレット.xml:

<context:component-scan base-package="bar.foo.controller" />

コントローラー、Autowired、およびコントローラー パッケージ内のその他の注釈を取得するために Web コンテキストを設定しましたが、これがベスト プラクティスかどうかはわかりません。

ベストアンサー1

おっしゃる通りです。2 つの異なるアプリケーション コンテキストがあります。1 つは ContextLoaderListener によってロードされるルート アプリケーション コンテキスト (ServletContext が初期化される時点)、もう 1 つは Web コンテキスト (DispatcherServlet によってロードされる) です。ルート アプリケーション コンテキストは Web コンテキストの親です。

これらは 2 つの異なるアプリケーション コンテキストであるため、異なる動作が行われます。component-scanアプリケーション コンテキストでサービスを定義すると、サービスのすべての Bean がここで作成されます。

ディスパッチャ サーブレットがロードされると、Web コンテキストの作成が開始され、ある時点で (<mvc:annotation-driven/>これによって駆動されて) URI とハンドラ メソッドのマッピングが作成され、アプリケーション コンテキスト (ルート アプリケーション コンテキストではなく、Web アプリケーション コンテキスト) 内の Bean のリストが取得されます。component-scanここで定義していないため、コントローラ関連の Bean は見つからず、マッピングは作成されません。そのため、ディスパッチャ サーブレット コンテキストでもコンポーネント スキャンを定義する必要があります。

良い方法は、ルート アプリケーション コンテキストでコントローラ関連の Bean を除外することです。

<context:component-scan base-package="package">
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

Web アプリケーション コンテキスト内のコントローラー関連のみ:

<context:component-scan base-package="package" use-default-filters="false">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>

おすすめ記事