【发布时间】:2014-10-14 11:49:34
【问题描述】:
我想为各种数据库创建配置文件,我可以根据我使用的数据库加载这些配置文件。
这是我的 application-context.xml 文件的相关部分:
<beans>
<!-- ...other beans in common profile-->
<beans profile="dev">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driverClassName}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<beans profile="mariadb">
<context:property-placeholder location="classpath*:databases/mariadb.properties"
ignore-unresolvable="true"/>
</beans>
<beans profile="mysql">
<context:property-placeholder location="classpath*:databases/mysql.properties"
ignore-unresolvable="true"/>
</beans>
<beans profile="hsql">
<context:property-placeholder location="classpath*:databases/hsql.properties"
ignore-unresolvable="true"/>
</beans>
</beans>
<beans profile="production">
<jee:jndi-lookup id="dataSource" jndi-name="${jndi.name}"/>
</beans>
我的 dispatcher-servlet.xml
<beans>
<mvc:default-servlet-handler/>
<import resource="classpath*:profile-context.xml"/>
<mvc:annotation-driven/>
<beans/>
和 profile-context.xml
<beans>
<beans profile="dev">
<import resource="application-context.xml"/>
</beans>
<beans profile="production">
<import resource="application-context.xml"/>
</beans>
<beans/>
最后,我的 web.xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev, mysql</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
虽然这对使用 @ActiveProfile({"dev", "mysql"}) 的 JUnit 测试有效,但它不适用于我的 Tomcat 容器。 我在配置文件上下文中的开发配置文件中包装应用程序上下文的方式可能做错了吗?
【问题讨论】: