1) I created environment file as mentioned here
I created a file called
Prod.env
and entered followingSPRING_DATASOURCE_URL="jdbc:mysql://5.6.7.8:3306/ab?autoReconnect=true&characterEncoding=utf8" SPRING_DATASOURCE_USERNAME="root" SPRING_DATASOURCE_PASSWORD="IWin"
And then I executed this command
export $(cat Prod.env | xargs)
2) Then I created application.properties
under WEB-INF
with following code
spring.datasource.driver-class-name:com.mysql.jdbc.Driver
spring.datasource.url=${SPRING_DATASOURCE_URL}
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
3) Then in spring-security.xml
<b:bean id="mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<b:property name="location">
<b:value>/WEB-INF/application.properties</b:value>
</b:property>
</b:bean>
<b:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<b:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<b:property name="url" value="${spring.datasource.url}" />
<b:property name="username" value="${spring.datasource.username}" />
<b:property name="password" value="${spring.datasource.password}" />
</b:bean>
Now, when I save everything and restart I get
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in ServletContext resource [/WEB-INF/spring/appServlet/spring-security.xml]: Could not resolve placeholder 'SPRING_DATASOURCE_URL' in string value "jdbc:mysql:${SPRING_DATASOURCE_URL}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'SPRING_DATASOURCE_URL' in string value "jdbc:mysql:${SPRING_DATASOURCE_URL}"
What I'm trying to achieve is: Access an environment variable via application.properties (or doing some configuration in spring-security.xml ) , but not with java code. What am I missing here?
I have searched several questions, but none explains it clearly. Please help
Note: Mine is not a spring boot project. It is normal Spring 4.0.3 web app.