2013-06-13 30 views
0

當我編譯我的項目,它給這個錯誤並停止編譯春天至少有一個基礎包必須被指定的異常

org.springframework.beans.factory.BeanDefinitionStoreException:從 意外的異常解析XML文檔ServletContext資源 [/WEB-INF/application-context.xml];嵌套的例外是 java.lang.IllegalArgumentException異常:至少有一個基礎包必須 指定

爲什麼這個異常發生,我該如何解決這個問題。

+0

分享您的應用程序的context.xml –

+1

只是檢查是否已正確提到的控制器或服務的完整包路徑。 – agpt

+1

問題可能是你已經配置了註解驅動的上下文配置,但沒有指定任何'component-scan'路徑 –

回答

2

添加此作爲在每個Spring文件

Spring提供自動檢測「定型」類和註冊用在ApplicationContext對應的BeanDefinitions的能力。要自動檢測這些類並註冊相應的bean,需要在XML中包含以下元素,其中'basePackage'將是這兩個類的公共父包(或者也可以指定包含每個父包的逗號分隔列表類)。

因此您的應用程序上下文應該是這個樣子:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:component-scan base-package="org.example"/> 

</beans> 

,使你的包獲得通過掃描你component-scan應該相應註釋註釋Java類中按要求。像@Controllers@Service等 查看詳細信息here

1
<context:component-scan base-package="Your package"> 

</context:component-scan> 

嘗試在你配置文件

1

從我的上述評論

原因可能是你已經註解驅動上下文配置在你的應用程序上下文啓用(<context:annotation-config></context:annotation-config>),但你必須提供的基本包掃描春豆。

添加根包你的應用爲基礎包的掃描bean定義

<context:component-scan base-package="<your-app-base-package>"/> 
相關問題