jdbc properties in Spring 3 using maven

I am developing a maven multi-module projects in spring 3. I have two projects. I have one property file called jdbc.properties(it contains the database username, password) in Project A but project B doesn't have jdbc.properties . I have included project a as dependency in Project b to get some common code of Project a in Projet b. My question is how can i use properties of Project A jdbc.properties in Project b spring-context.xml file using maven

application-context.xml (SNIPPET)

<property name="jdbcUrl" value="${jdbc.url}"/>
 <property name="user" value="${jdbc.username}"/>
 <property name="password" value="${jdbc.password}"/>

Here is a Structure of the my projects

**Projects**
 - Project A
src/main/java
  +src/main/resource
    -Database.properties
    Database file has following properties 
    jdbc.driverClassName=org.h2.Driver
    jdbc.url=jdbc:h2:tcp://localhost/~/tododb
    jdbc.username=sa
    jdbc.password=
    jpa.database=H2
    jpa.showSql=true
    pom.xml

 - Project b
  +src/main/java
  src/main/resource (don't want to have jdbc.properties file in this project)
  pom.xml (I have added Project as dependencies in Project B because 
  i am using some  common classes of project a in project b) 

Here is POM files for both projects POM Configuration for both Projects

 - Project A (pom.xml)
  <parent>
    <groupId>com.ambientideas</groupId>
    <artifactId>sample08-multimodulejava-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.ambientideas1</groupId>
  <artifactId>sample-multimodulejava-module1</artifactId>
  <name>sample application 1</name>
  <packaging>jar</packaging> 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

 - Project B (Pom.xml)
  <parent>
    <groupId>com.ambientideas</groupId>
    <artifactId>sample08-multimodulejava-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sample-multimodulejava-module2</artifactId>
    <packaging>jar</packaging> 
<dependencies> (using Project a common classes in project b)
        <dependency>
            <groupId>com.ambientideas1</groupId>
            <artifactId>sample-multimodulejava-module1</artifactId>
            <version>${version}</version>
        </dependency>
</dependencies> 
</project>

Here is the both jdbc-context.jdbc files of Spring configuration of my projects. Now Spring Configuration

In Both Projects (Project a, Project b), i have two jdbc.content.xml files( Spring xml's)

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<context:property-placeholder location="/META-INF/classes/jdbc.properties" /> 
    <property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        </bean> 

Question: I don't want to have to two database.proprties files in project A and Project b. What i want is that i Just want to have only one jdbc.properties file in project A, and then want to use properties of JDBC.propties file of Project A in Project B.

    <property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        </bean>

Is there any way to do it? Here is stricture of Project B pom.xml

    <project>
    <parent>
    <groupId>com.ambientideas</groupId>
    <artifactId>sample08-multimodulejava-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>sample-multimodulejava-module2</artifactId>
    <packaging>jar</packaging> 
    <dependencies>
    <dependency>
    <groupId>com.ambientideas1</groupId>
    <artifactId>sample-multimodulejava-module1</artifactId>
    <version>${version}</version>
    <description>Project B<description>
</dependency>
    </dependencies> 
    </project>

Thanks in Advance.


If your properties file is built into the jar of Project A, then it'll be available on the classpath for any other code to load as well, so you should just be able to use it from Project B in the same way as you use it from Project A.

Note that this is just a convenient accident and definitely not how I'd recommend setting up your project. Configuration should always be externalized so that your artifact is independent of any environment.

PS Ambient Ideas? Isn't that Matthew McCullough's company? If so, you have plenty of in-house expertise on Java in general and Maven in particular. You shouldn't need a lot of outside guidance.

链接地址: http://www.djcxy.com/p/62952.html

上一篇: 如何动态地将属性注入到maven的settings.xml中

下一篇: Spring 3中使用maven的jdbc属性