Results 1 to 6 of 6

Thread: Define DAO-Implementation with Maven-Profile

  1. #1
    Join Date
    Aug 2007
    Posts
    12

    Question Define DAO-Implementation with Maven-Profile

    Hello folks,

    i have a strange idea ...
    i would like define the used dao-implementation in a maven-profile ...
    a example:

    spring-dao.xml:

    Code:
      <bean id="remoteDao" class="de.ahoehma.dao.Remote" scope="session">
        <aop:scoped-proxy />
        <property name="profile"value="${dao_profile}" />
      </bean>
      
      <bean id="localDao" class="de.ahoehma.dao.Local" scope="session">
        <aop:scoped-proxy />
        <property name="profile" value="${dao_profile}"/>
        <property name="license" value="/db/123456.TXT"/>
      </bean>
    spring-services.xml:

    Code:
      <bean id="FoobarService" class="de.ahoehma.service.FoobarService">
        <property name="dao" ref="${daoImpl}"/>
      </bean>
    imagine there are more dao-implementations possible ... so i would *not* include every possible dao-impl into my dao.xml.
    so how can i define "dynamic" the real bean for "${daoImpl}"?

    I want create the war-file (i have a j2ee app) with a simple maven-call:

    mvn package -Pdao-local
    or
    mvn package -Pdao-remote
    or
    mvn package -Pdao-whatever

    in the maven-profiles i would define the dao which should be used:

    Code:
    <profile>
      <id>dao-local</id>
      <properties>
         <dao-impl>localDao</dao-impl>
      </properties>
    </profile>
    <profile>
      <id>dao-remote</id>
      <properties>
         <dao-impl>remoteDao</dao-impl>
      </properties>
    </profile>
    then maven can filter my propertie-files, e.g. a dao.properties:

    Code:
    daoImpl=${dao-impl}
    so a spring PropertyPlaceholderConfigurer can read the property "daoImpl".
    but here is the problem "i can use a placeholder for a bean-ref" :-(

    ref="${daoImpl}" is not possible

    any ideas?
    regards
    andreas
    Last edited by ahoehma; Aug 29th, 2008 at 03:27 AM.

  2. #2
    Join Date
    Aug 2007
    Posts
    12

    Default

    a dynamic import will not work:

    spring-dao-remote.xml

    Code:
      <bean id="Dao" class="de.ahoehma.dao.Remote" scope="session">
        <aop:scoped-proxy />
        <property name="profile"value="${dao_profile}" />
      </bean>
    spring-dao-local.xml

    Code:
      
      <bean id="Dao" class="de.ahoehma.dao.Local" scope="session">
        <aop:scoped-proxy />
        <property name="profile" value="${dao_profile}"/>
        <property name="license" value="/db/123456.TXT"/>
      </bean>
    spring-services.xml

    Code:
      <import resource="classpath:${dao-config}"/>
    
      <bean id="FoobarService" class="de.ahoehma.service.FoobarService">
        <property name="dao" ref="Dao"/>
      </bean>
    with dao.properties

    Code:
    dao-config=spring-dao-remote.xml
    or

    Code:
    dao-config=spring-dao-local.xml

  3. #3
    Join Date
    Aug 2007
    Posts
    12

    Default

    i found a way ... but not the best solution ...

    spring-dao-remote.xml

    Code:
      <bean id="DaoRemote" class="de.ahoehma.dao.Remote" scope="session">
        <aop:scoped-proxy />
        <property name="profile"value="${dao_profile}" />
      </bean>
    spring-dao-local.xml

    Code:
      
      <bean id="DaoLocal" class="de.ahoehma.dao.Local" scope="session">
        <aop:scoped-proxy />
        <property name="profile" value="${dao_profile}"/>
        <property name="license" value="/db/123456.TXT"/>
      </bean>
    spring-services.xml

    Code:
      <import resource="spring-dao-local.xml"/>
      <import resource="spring-dao-remote.xml"/>
    
      <bean id="FoobarService" class="de.ahoehma.service.FoobarService">
        <property name="dao">
          <ref bean="${daoImpl}"/>
        </property>
      </bean>
    with

    Code:
    daoImpl=DaoLocal
    or

    Code:
    daoImpl=DaoRemote
    It works but i have to include all possible spring-dao-XXX.xml :-(

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    We have a project called JavaConfig which might be what you want to use. Joris and Alef gave a presentation about this on how to switch between environments.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Aug 2007
    Posts
    12

    Talking Found something

    http://blog.arendsen.net/index.php/2...t-with-spring/

    Works for me ... but with the lack of importing all known dao-configurations,
    a dynamic import would be nice

  6. #6
    Join Date
    Aug 2007
    Posts
    12

    Thumbs up

    Quote Originally Posted by Marten Deinum View Post
    We have a project called JavaConfig which might be what you want to use. Joris and Alef gave a presentation about this on how to switch between environments.
    Hi Marten, please post a URL. thxs

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •