4

ここで質問があります。「applicationContext.xml」で定義されたBeanを、たとえば「spring-servlet.xml」で定義されたコントローラーで使用できるようにすることで、この種のエラーをスキップできます。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/home' defined in ServletContext resource [/WEB-INF/mmapp-servlet.xml]: Cannot resolve reference to bean 'equipementService' while setting bean property 'equipementService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'equipementService' is defined

applicationContext.xml

<?xml version="1.0" ?>
<!DOCTYPE beans PUBLIC 
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean name="equipementService"
        class="mmapp.service.SimpleEquipementService" />
    <bean name="equipement1"
        class="mmapp.domain.Equipement" />

</beans>

mmapp-servlet.xml

<?xml version="1.0" ?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean name="/home" class="mmapp.web.HelloController">
        <property name="equipementService" ref="equipementService" />
    </bean>
</beans>
4

2 に答える 2

7

通常、Spring ベースの Web アプリケーションには、複数のランタイム Spring アプリケーション コンテキストがあります。

  1. ルート アプリケーション コンテキスト (サーブレット コンテナーの起動時にロードされる)。通常、サービスの Bean を配置する場所です。ルート アプリケーション コンテキストは、ContextLoaderListener を使用してロードされます。通常、これらのエントリをweb.xml ファイル:

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

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

  1. ルート アプリケーション コンテキストの子と見なされ、UI に関連する Bean (コントローラーなど) を保持する 1 つ以上の Web アプリケーション コンテキスト。適切に Spring Container に追加します。ルート アプリケーション コンテキストの Bean は、ここで定義された Bean に表示されますが、その逆ではありません。この方法では、UI レイヤーとサービス レイヤーの間にレベルの分離が提供されます。これは、web.xml ファイルの一般的な構成エントリです。

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

この方法で Bean を定義した場合はequipementService、コントローラーから見えるはずです。

于 2012-05-07T21:53:05.950 に答える
1

私は専門家ではないので、これが問題になるかどうかはわかりませんが、提案があります。Web アプリケーション記述子 (web.xml) を投稿していただけますか? context-param が含まれていますか?

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
</context-param>
于 2012-05-07T21:28:17.520 に答える