2016-04-14 9 views
0

WEB-INFフォルダに2つのコンテキストxmlがあります。 applicationContext.xmlおよびapp-servlet.xml。私はアプリ-servlet.xmlSpring個別のコンテキストがルートコンテキストを継承しない

<context:component-scan base-package="com.training.hibernate.controller"/>

でこれを宣言し、私はサービスのコンポーネントスキャニングを移動した場合、このapplicationContext.xmlを中

<context:component-scan base-package="com.training.hibernate.services"/> 
<context:component-scan base-package="com.training.hibernate.dao"/> 

は、私はこのエラー

BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; 

を得ましたとdaoをapp-servlet.xmlに入れてもエラーは出ませんでした。私はアプリ-servlet.xmlこれは私のweb.xmlここ

<?xml version="1.0" encoding="ISO-8859-1" ?> 

<web-app 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" 
    version="2.4"> 
    <display-name>Spring Web Application</display-name> 

    <servlet> 
     <servlet-name>app</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>app</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 
+1

spring.xmlファイルを読み込むためのweb.xmlエントリを共有してください。 –

答えて

0

あるapplicationContext.xmlを

を継承していないと仮定すると、異なるコンテキストを明確にするために、私はあなたを助けることができるいくつかの方向です。

  1. アプリケーションコンテキスト:通常春ベースのWebアプリで二つの主要なコンテキストがあり、サービスのような豆が含まれている状況、DAOなど

    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value> 
         classpath:spring-config/applicationContext.xml 
        </param-value> 
    </context-param> 
    <listener> 
        <listener-class> 
         org.springframework.web.context.ContextLoaderListener 
        </listener-class> 
    </listener> 
    
  2. そしてディスパッチャサーブレットのコンテキストがあります。これにはWeb固有のBeanが含まれます。このコンテキストは、上のContextLoaderListenerで定義されたBeanにアクセスできます。

    <servlet> <servlet-name>app</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-config/app-servlet.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>

確認してください、あなたはこの権利を持っています。

+0

私のweb.xmlをご覧ください。私はContextLoaderListenerを使用していません。 –

+0

既定の設定場所を使用しているので、applicationContext.xmlがアプリケーションにまったく読み込まれていないようです。私が言及したアプローチに従ってください。または、app-servlet.xml内に 'があります。 –

+0

ありがとうございます。病院に行ってみてください。 –

関連する問題