ksn*_*tum 10 java spring configuration-files maven
我正在尝试根据某个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>
Run Code Online (Sandbox Code Playgroud)
还有一个这样的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>
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
我的问题基本上是,如何将Maven属性放入servlet-context.xml文件中?我需要.properties文件吗?我对Spring中Maven和PropertyPlaceholderConfigurer的过滤有所了解,但我不知道如何将它们放在一起 - 或者它们是否一起使用?或者有更简单的方法吗?
yor*_*rkw 10
一般来说,YES,您需要使用.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>
Run Code Online (Sandbox Code Playgroud)
希望这是有道理的.
使用我从两个答案和我的研究中学到的东西,我得到了一个由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>
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个属性文件,放在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
Run Code Online (Sandbox Code Playgroud)
然后,我可以启动Maven
mvn -P developement clean install
Run Code Online (Sandbox Code Playgroud)
或者有一个settings.xml为我设置正确的配置文件:
<settings>
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<environment.type>dev</environment.type>
</properties>
</profile>
</profiles>
</settings>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19859 次 |
| 最近记录: |