问题
I have created a environment variable entry of profile to be active in my web application: In setenv.bat file in Tomcat server JAVA_OPTS=%JAVA_OPTS% -Dspring.profiles.active=prod
And when my application tries to load datasource using active profile it gives exception as no active profile is set.
Entry in applicationcontext.xml:
<beans profile="dev">
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:books"></property>
<property name="username" value="system"></property>
<property name="password" value="xyz"></property>
</bean>
</beans>
<beans profile="prod">
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="system"></property>
<property name="password" value="abc"></property>
</bean>
</beans>
</beans>
However when i do the same thing via web.xml entry it works:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
Can anyone please tell me what is the problem in loading profile using environment variable.
回答1:
Because that command-line option defines a system property for Tomcat itself, which is not automatically picked up by the web contexts it is running.
Something like this would probably work:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>${spring.profiles.active}</param-value>
</context-param>
Or, depending on what kind of change you want to make to your deployments, you might want to look at Spring Boot, which simplifies configuring and launching a single Spring application.
来源:https://stackoverflow.com/questions/40723818/profile-not-getting-picked-from-environment-variable-set-in-tomcat-but-is-pickin