Spring bean複数のプロファイルをロードする
大きなプロジェクト構造で、Spring beanプロファイルは異なるフォルダに位置しており、メンテナンスとモジュール化を容易にする.例えば、Spring-Common.xmlはcomonにあります. フォルダ内、Spring-Connection.xml connectionフォルダ、Spring-MaoduleA.xmlはModuleAフォルダなどです.
複数のSpringをロードすることができます. beanのプロファイルは以下のコードの中にあります.
以上の方法は組織が不足していて、間違いやすいです.より良い方法はすべてのSpringを組織するべきです. bean XMLファイルにプロファイルを設定します.例えば、Spring-All-Maodule.xmlファイルを作成し、Spring全体をインポートします. beanのファイルは以下の通りです.
File:Spring-All-Maodule.xml
Spring 3において、代替ソリューションは使用される. JavaConfig@Import.
複数のSpringをロードすることができます. beanのプロファイルは以下のコードの中にあります.
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"Spring-Common.xml",
"Spring-Connection.xml","Spring-ModuleA.xml"});
すべてのSpringを XMLファイルはプロジェクトクラスのパスに入れます.project-classpath/Spring-Common.xml
project-classpath/Spring-Connection.xml
project-classpath/Spring-ModuleA.xml
解決方法以上の方法は組織が不足していて、間違いやすいです.より良い方法はすべてのSpringを組織するべきです. bean XMLファイルにプロファイルを設定します.例えば、Spring-All-Maodule.xmlファイルを作成し、Spring全体をインポートします. beanのファイルは以下の通りです.
File:Spring-All-Maodule.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<import resource="common/Spring-Common.xml"/>
<import resource="connection/Spring-Connection.xml"/>
<import resource="moduleA/Spring-ModuleA.xml"/>
</beans>
このようなXMLファイルを読み込むことができます.ApplicationContext context =
new ClassPathXmlApplicationContext(Spring-All-Module.xml);
このファイルをプロジェクトのクラスパスに入れます.project-classpath/Spring-All-Module.xml
注意Spring 3において、代替ソリューションは使用される. JavaConfig@Import.