ページの先頭行へ戻る
Interstage Business Application Server オープンJavaフレームワークユーザーズガイド
FUJITSU Software

3.1.15 bean定義プロファイル

bean定義ファイルを書き換えることなく、テスト用の定義と本番運用用の定義を切り替えることができます。

applicationContext.xml

<beans ...>

<bean id="employeeDao" class="hoge.EmployeeDaoImpl ">

<constructor-arg ref="dataSource"/>

</bean>

<import resource="classpath:develop-datasource-config.xml" />

<import resource="classpath:runtime-datasource-config.xml" />

</beans>

dev-datasource-config.xml
<beans profile="develop">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <context:property-placeholder location="jdbc.properties"/>
</beans>
runtime-datasource-config.xml
<beans profile="runtime">
    <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/datasource"/>
</beans>
定義の切り替え

テストでは“develop”プロファイルを設定して、テスト用のデータベースを使用するデータソースを取得します。

package hoge;
@ActiveProfiles(profiles="develop") 
@ContextConfiguration(locations = {"applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
		        DirtiesContextTestExecutionListener.class })
public class EmployeeDaoTest {
     @Autowired
     EmployeeDao employeeDao;
@Test
public void TestExecDaoInsert(){
     Assert.assertTrue(employeeDao.execDaoInsert(..));
}}

本番運用では、“runtime” プロファイルを設定して、本番運用データベースを使用するデータソースを取得します。

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>runtime</param-value>
    </init-param>
</servlet>