2016-04-11 55 views
0

我在項目中有3個模塊。其中之一叫common。在這個模塊中,我試圖啓動Spring容器。其中一個豆生活在另一個模塊(webapp),我也有一個模塊testapp。所以,現在,在服務器上部署項目(Web應用程序和常見)後,是一切OK,但如果我試圖部署常見和testapp失敗,出現異常:如果bean不存在則不會失敗spring容器

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class[...] 

我whant知道,如果它是任何可能忽略未知的bean和運行容器?我使用xml配置,沒有註釋。由於業務邏輯,它應該是xml

回答

1

利用Spring配置文件來確定何時應該實例化特定的bean。例如,您可以有兩個配置文件的網絡(常用+ Web應用程序)和測試(常用+ testapp)

然後在你的應用程序上下文XML配置定義豆類如下

<?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.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> 

    <!-- define common beans here--> 
    <beans profile="test"> 
     <!-- define test beans here--> 
    </beans> 
    <beans profile="web"> 
     <!-- define web beans here--> 
    </beans> 
</beans> 

然後取決於你如何啓動您的應用程序,您需要提供名爲spring.profiles.active的系統屬性以提供要使用的配置文件。

例如-Dspring.profiles.active = web或-Dspring.profiles.active = test

+0

哇!男人,這真是很酷的解決方案!謝謝! – Dante

+0

有關此解決方案的更多信息,請按照[鏈接](http://www.captaindebug.com/2012/08/using-spring-profiles-in-xml-config.html#.VwyzZzTBn1w) – Dante

相關問題