问题描述
我创建了一个配置文件的环境变量条目以在我的Web应用程序中处于活动状态:在Tomcat服务器的setenv.bat文件中JAVA_OPTS =%JAVA_OPTS%-Dspring.profiles.active =产品
I have created a environment variable entry of profile to be active in my web application:In setenv.bat file in Tomcat serverJAVA_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.
applicationcontext.xml中的条目:
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>
但是,当我通过web.xml条目执行相同的操作时,它会起作用:
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.
推荐答案
因为该命令行选项为Tomcat本身定义了系统属性,所以运行的Web上下文不会自动选择该属性.
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.
类似的东西可能会起作用:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>${spring.profiles.active}</param-value>
</context-param>
或者,根据要对部署进行的更改类型,您可能需要查看Spring Boot,它可以简化配置和启动单个Spring应用程序的过程.
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.
这篇关于不会从tomcat中设置的环境变量中获取配置文件,而是在web.xml中提及时进行配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!