1

Here is my problem, I want to define 2 Maven profiles,

one for the dev environment and

one for the prod environment.

In my pom.xml, I so defined 2 profiles :

<!-- pom.xml -->

<profiles>

    .....

    <profile>
      <id>dev</id>
      <activation>
        <property>
          <name>environment</name>
          <value>dev</value>
        </property>
        <activeByDefault>true</activeByDefault>
      </activation>
    </profile>

    <profile>
      <id>prod</id>
      <activation>
        <property>
          <name>environment</name>
          <value>prod</value>
        </property>
      </activation>
    </profile>

</profiles>

and in my context.xml file, I want to use a different dataSource according to which profile I choosed :

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jd="http://www.springframework.org/schema/jdbc"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:sws="http://www.springframework.org/schema/web-services"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3-0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:component-scan base-package="xxx.xxx.xxx.dao" />       

    <!-- DEV -->
    <bean p:profiles="dev" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource" >     
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1522:******" />     
        <property name="username" value="******" />
        <property name="password" value="******" />
    </bean>     

    <!-- PROD -->
    <bean p:profiles="prod" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@xxxx.xxxx.xxxx.xxxx:1521:******" />
        <property name="username" value="******" />
        <property name="password" value="******" />
    </bean>

    <bean  id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="persistenceUnitName" value="carfleetPersistenceUnit" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven />

</beans>    

I for sure did something wrong in the configuration or the utilisation of theses profiles... but I don't know what...

I'm building with maven by using the command :

mvn -e -Denvironment=dev clean package tomcat7:redeploy

But I got the error :

Configuration problem : Bean name "dataSource" is already used in this <beans> element.

If someone can tell me what is wrong :/ Because I test a lot of things, but it's just giving different errors, I don't know exactly what to do to fix it.

Thanks in advance !

Valentin Montmirail
  • 2,594
  • 1
  • 25
  • 53

1 Answers1

1

Profiles in xml are defined in the following way :

Your dev profile beans here :

 <beans profile="dev">
 <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">     
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1522:******" />     
    <property name="username" value="******" />
    <property name="password" value="******" />
</bean>    
</beans>

Your Prod profile bean here :

<beans profile="prod">
  <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@xxxx.xxxx.xxxx.xxxx:1521:******" />
    <property name="username" value="******" />
    <property name="password" value="******" />
  </bean>
</beans>

and Common beans here :

<beans profile="dev,prod">
  <bean  id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="persistenceUnitName" value="carfleetPersistenceUnit" />
    <property name="dataSource" ref="dataSource" />
</bean>

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
  </bean>
</beans>

For activating a particular profile you need to set System Property as spring.profiles.active as <your profile name>. It can also be achieved through web.xml by :

 <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
      <param-name>spring.profiles.active</param-name>
      <param-value>production</param-value>
  </init-param>

Utkarsh
  • 589
  • 3
  • 19
  • I got now the error : "Cause by org.springframework.beans.factory.NoSuchBeanDefinitionException : No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found : 0. Can you confirm that my call of Maven is initializing the profil ? Because i'm not even sure of that :/ – Valentin Montmirail Jul 24 '15 at 11:32
  • http://stackoverflow.com/questions/31612216/works-without-maven-profile-bug-with I put this error in a new question, if you would like to take a look :/ – Valentin Montmirail Jul 24 '15 at 13:48