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

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

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

使用从这两个答案中获得的知识以及我的研究,我能够得到一个由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"  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.OracleDriverdb.url=jdbc:oracle:thin:[USER/PASSWORD]@[HOST][:PORT]:SIDdb.username=jsmithdb.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>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5488233.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存