[英]Spring datasources multiple profiles
我想为各种数据库创建配置文件,我可以根据我正在使用的数据库加载。
这是我的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容器不起作用。 我是否正在做一些错误的方式我将应用程序上下文包装在profile-context中的dev配置文件中?
Spring使用StringUtils. commaDelimitedListToStringArray
解析spring.profiles.active
的值StringUtils. commaDelimitedListToStringArray
StringUtils. commaDelimitedListToStringArray
。 这个方法不是很宽容,并将你的配置文件字符串解析为{"dev", " mysql"}
- 注意mysql
之前的空格。
从param-value
元素中删除逗号后的空格,您的代码应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.