我原本有一個xml文件與下面這種佈局:豆類使用配置文件後未找到 - 不知道爲什麼
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="FirstBean">...</bean>
<bean id="SecondBean">...</bean>
<bean id="ThirdBean">...</bean>
</beans>
這對我們運作良好。但是今天我們意識到我們只想在某些環境(生產和測試)中實例化一些bean,並且我們需要配置文件才能實現它。我們這樣做是通過嵌套一些元素,並添加配置文件,如下所示:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Beans to run commonly amongst both profiles -->
<beans>
<bean id="FirstBean">...</bean>
</beans>
<!-- Beans to run in prod -->
<beans profile="production">
<bean id="SecondBean">...</bean>
</beans>
<!-- Beans to run in test -->
<beans profile="test">
<bean id="ThirdBean">...</bean>
</beans>
</beans>
然後......爲了讓豆加載正確的豆類 - 在應用程序的初始啓動 - 我執行以下(目前硬編碼用於測試目的):
System.setProperty("spring.profiles.active", "test");
我也曾嘗試:
System.setProperty("spring.profiles.active", "production");
但現在我不斷收到「沒有這樣的bean定義」在所有三個豆。
我希望我在這裏提供了足夠的信息。我真的被困在此 - 已經遵循各種在線資源。
信息太少,只有片段,並沒有概述。如果您在啓動時嘗試在某個bean中執行該操作,那麼您何時/在哪裏設置了這些屬性,這就太遲了。 –