Vishal
Vishal

Reputation: 724

How can I use properties in pom.xml in spring application context xml file?

I have a an spring context file which is similar to what is given below

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder
                location="classpath:application.properties, classpath:application-${env}.properties"/>

    <context:annotation-config/>
</beans>

The snippet of pom.xml where profiles are configured is as below-

        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>

I want to use the property 'env' which is decided based on selected maven profile, in my spring application-context file.

How can I do that?

Edit:- Its a Spring web app

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>

Upvotes: 1

Views: 1360

Answers (1)

Aran K
Aran K

Reputation: 289

Not directly answering your question but, if you can use Spring Boot, I think what you are trying to do is easily solved by using Spring Profiles.

Essentially you avoid manually coding in the multiple profiles in Maven and in your XML and let Spring do it for you. Here is a basic example.

Upvotes: 1

Related Questions