I want to create profiles for various databases which I can load depending on which one I'm using.
This is my the relevant part of my application-context.xml file:
<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>
My dispatcher-servlet.xml
<beans>
<mvc:default-servlet-handler/>
<import resource="classpath*:profile-context.xml"/>
<mvc:annotation-driven/>
<beans/>
and profile-context.xml
<beans>
<beans profile="dev">
<import resource="application-context.xml"/>
</beans>
<beans profile="production">
<import resource="application-context.xml"/>
</beans>
<beans/>
finally, my 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>
Although this would work lets say with JUnit tests with @ActiveProfile({"dev", "mysql"}) it doesnt work for my Tomcat container. Am I doing probably doing something wrong in the way I am wrapping application-context in a dev profile in profile-context?
Spring parses the value of spring.profiles.active
using StringUtils. commaDelimitedListToStringArray
StringUtils. commaDelimitedListToStringArray
. This method is not very forgiving, and parses your profile string as {"dev", " mysql"}
– note the space before mysql
.
Remove the space after the comma from your param-value
element, and your code should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.