0

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 Answers1

0

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.

Community
  • 1
  • 1
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
  • Problem is that i want to set a variable in server itself so that i can get to know if it is a development environment or prod and on the basis of that profile gets loaded. I have tried setting a variable in setenv of tomact but my application is not picking up any system property or environment variable from there. – Ankit Chaudhary Nov 24 '16 at 10:02
  • OK, and what I have suggested should solve that problem. Did you try it? – OrangeDog Nov 24 '16 at 13:54
  • Yes but even after adding `spring.profiles.active` in setenv file of tomcat it didn't work.Please note that i only want to read it from server configuration. – Ankit Chaudhary Nov 25 '16 at 12:15