vendredi 24 juillet 2015

Works without maven profile, bug with

Here is my problem,

So here is my profile in pom.xml :

<profiles>
    <profile>
            <id>dev</id>
            <activation>
                <property>
                    <name>environment</name>
                    <value>dev</value>
                </property>
            </activation>
        </profile>

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

When I use this context.xml, my application works perfectly :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
    xmlns:xsi="http://ift.tt/ra1lAU"      xmlns:aop="http://ift.tt/OpNdV1"
xmlns:context="http://ift.tt/GArMu7"
xmlns:jee="http://ift.tt/OpNaZ5" xmlns:jd="http://ift.tt/18IIlo0"
xmlns:p="http://ift.tt/1jdM0fE" xmlns:tx="http://ift.tt/OGfeU2"
xmlns:sws="http://ift.tt/KZYGcT"
xmlns:util="http://ift.tt/OGfeTW"
xsi:schemaLocation="http://ift.tt/GArMu6 http://ift.tt/18sW2ax
    http://ift.tt/GArMu7 http://ift.tt/QEDs1k
    http://ift.tt/OpNaZ5 http://ift.tt/1j5lSTg
    http://ift.tt/OGfeU2 http://ift.tt/1cQrvTl
    http://ift.tt/18IIlo0 http://ift.tt/1CYUSmd
    http://ift.tt/OGfeTW http://ift.tt/1feTls0">

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

  <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:@xxx.xxx.xxx.xxx:1521:essmh" />
            <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>    

But I want to be able to change my dataSource according if I'm in production or in development.

So here is what I've got now:

 <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:@xxx.xxx.xxx.xxx:1521:dev" />
            <property name="username" value="*****" />
            <property name="password" value="*****" />
      </bean>   
</beans>

<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:@xxx.xxx.xxx.xxx:1522:prod" />
            <property name="username" value="*****" />
            <property name="password" value="*****" />
      </bean>
</beans>

<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>

    <tx:annotation-driven />

</beans>

I check by using mvn -e -P prod help:active-profiles that the profile prod is active in every package and it is. But at the end, it doesn't work and I get the Error :

Caused by : org.springframework.beans.factory.NoSuchBeanDefinitionException : No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0.

For sure the error is there, because that's the only changed to go from "application is working" to "application has errors".

Aucun commentaire:

Enregistrer un commentaire