小编典典

使用Maven和Spring控制项目:如何使用Maven配置文件设置Spring配置文件?

spring

我正在尝试根据某个Maven配置文件是否处于活动状态来使用数据库信息配置Spring配置文件。我已经看到了有关此问题的答案,但是我很难将所有内容放在一起。

我有一个这样的Maven个人资料:

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

    <profile>
        <id>development</id>
        <activation>
            <property>
                <name>environment.type</name>
                <value>dev</value>
            </property>
        </activation>

        <!-- Database properties for Spring -->
        <properties>
            <db.driver>oracle.jdbc.driver.OracleDriver</db.driver>
            <db.type>oracle</db.type>
            <db.host>192.168.0.0</db.host>
            <db.port>1521</db.port>
            <db.name>myDb</db.name>
            <db.url>jdbc:${db.type}:thin:@${db.host}:${db.port}:${db.name}</db.url>
        </properties>

还有一个settings.xml文件,如下所示:

<servers>
  <server>
    <id>development</id>
    <username>jsmith</username>
    <password>secret</password>
  </server>
</servers>

....

<profiles>
  <profile>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>

    <properties>
      <environment.type>dev</environment.type>
    </properties>
  </profile>
</profiles>

并在servlet-context.xml中:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
    <property name="driverClassName">
        <value>${db.driver}</value>
    </property>

    <property name="url">
        <value>${db.url}</value>
    </property>

    <property name="username">
        <value>${db.username}</value>
    </property>

    <property name="password">
        <value>${db.password}</value>
    </property>

    <property name="maxActive">
        <value>10</value>
    </property>

    <property name="maxIdle">
        <value>1</value>
    </property>
</bean>

我的问题基本上是,如何将Maven属性放入servlet-context.xml文件中?我需要一个.properties文件吗?我对Spring的Maven和PropertyPlaceholderConfigurer中的过滤有所了解,但我不知道如何将它们放在一起-还是将它们放在一起?还是有更简单的方法?


阅读 421

收藏
2020-04-20

共2个答案

小编典典

使用从这两个答案中获得的知识以及我的研究,我能够得到一个由pom控制的开发/生产系统,该系统可以设置正确的数据库值。

首先,在pom中,我创建了两个配置文件。

<!-- Production and Development Profiles -->

<profiles>
    <!-- Nike profile needs go here -->
    <profile>
        <id>production</id>
        <activation>
            <property>
                <name>environment.type</name>
                <value>prod</value>
            </property>
        </activation>
    </profile>

    <!-- Catalyst profile needs go here -->
    <profile>
        <id>development</id>
        <activation>
            <property>
                <name>environment.type</name>
                <value>dev</value>
            </property>
        </activation>

        <build>

            <!-- This holds the properties for the Spring context file.
                 Database values will be changes to development. -->
            <filters>
                <filter>src/main/resources/dev.database.properties</filter>
            </filters>

            <resources>

                <!-- This is the directory that holds the Spring context
                     file.  The entire folder is searched, so either no
                     other file should have place holders or you should
                     exclude other files from being filtered. -->
                <resource>
                    <directory>src/main/webapp/WEBINF/</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
    </profile>
</profiles>

在servlet-context.xml中的WEBINF目录中,放置了占位符:

<!-- For database, uses maven filtering to fill in placeholders -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${db.driver}" />
    <property name="url"             value="${db.url}" />
    <property name="username"        value="${db.username}" />
    <property name="password"        value="${db.password}" />
    <property name="maxActive">
        <value>10</value>
    </property>
    <property name="maxIdle">
        <value>1</value>
    </property>
</bean>

然后我创建了一个属性文件,该文件位于src / main / resources中

#
# Development database properties file
#
db.driver=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:[USER/PASSWORD]@[HOST][:PORT]:SID
db.username=jsmith
db.password=s3cr3t

然后我可以用

mvn -P developement clean install

或有一个settings.xml可以为我设置正确的配置文件:

<settings>
  <profiles>
    <profile>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>

      <properties>
        <environment.type>dev</environment.type>
      </properties>
    </profile>
  </profiles>   
</settings>
2020-04-20
小编典典

需要一个.properties文件吗?
一般来说,你需要使用.properties文件,这是我们通常要做的,尤其是在处理Spring上下文文件中的数据库连接配置时。

.properties文件的目的是提供在应用程序运行时配置数据库连接的功能(对于Web应用程序,通常需要在.properties文件更改后重新启动应用程序容器/服务器)。这通常是在不同环境(DEV / TEST / UAT / PROD)中的应用程序部署/安装步骤中完成的。

将这些数据库连接设置存储在pom.xml中不是一个好习惯,因为pom.xml的目的是用于项目描述,并且仅在应用程序构建时使用一次(例如mvn deploy)。而且在大多数情况下,即使将其打包到最终的jar / war文件中,我们也不在乎在构建应用程序后对其进行触摸。

要在spring上下文中使用.properties文件,请在applicationContext中定义一个propertyConfigurer bean,例如:

<bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
    <list>
      <!-- Default location inside war file -->
      <value>classpath:db.properties</value>
      <!-- Environment specific location, a fixed path on server -->
      <value>file:///opt/my-web-app/conf/db.properties</value>
    </list>
  </property>
  <property name="ignoreResourceNotFound" value="true"/>
</bean>

希望这有意义。

2020-04-20