我向我的应用程序添加了两个概要文件,以及它的外观:
<profiles>
<profile>
<id>dev</id>
<properties>
<db.username>root</db.username>
<db.password>root</db.password>
<db.connectionURL>localhost:3306</db.connectionURL>
<db.driverClass>com.mysql.jdbc.Driver</db.driverClass>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<db.username>prodroot</db.username>
<db.password>prodpass</db.password>
<db.connectionURL>localhost:3306</db.connectionURL>
<db.driverClass>com.mysql.jdbc.Driver</db.driverClass>
</properties>
</profile>
</profiles>在我的jdbc.properties文件中,我更改了如下值:
jdbc.driverClassName=${db.driverClass}
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql://${db.connectionURL}/dbname
jdbc.username=${db.username}
jdbc.password=${db.password}这是来自spring-container.xml的豆子
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />当我试图部署我的应用程序时,我得到了以下错误:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Could not resolve placeholder 'db.driverClass'项目结构:

知道我做错什么了吗?提前感谢!
编辑:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/webapp/WEB-INF/</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>发布于 2014-08-24 18:46:26
现在设置它的方式是,资源被过滤了,但最终出现在类路径根中,在本例中是WEB/类。
发布于 2014-08-24 17:22:47
该错误似乎表明属性文件中的值没有被替换,因为它抱怨db.driverClass不可解析,而不是jdbc.driverClassName。查看属性文件在构建的艺术品中的样子。看起来在Maven中没有启用资源过滤,或者忘记激活任何配置文件。要启用资源过滤,将类似的内容添加到您的<build>中
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>这假设过滤后的资源(如jdbc.properties)驻留在通常的src/main/resources中,如果当前不是的话。我建议您首先将所有过滤过的资源移到那里,除非您有充分的理由反对它,因为这是目前为止最简单的设置。确保您没有尝试过滤二进制资源(如图像)!请理解资源过滤(例如,将占位符转换为实际值)在构建时发生,因此您应该能够在生成的WAR中看到替换的值。
似乎Spring中的占位符解析已经配置为OK来读取属性文件,但是为了以防万一,您应该有如下所示:
<context:property-placeholder location="classpath:/jdbc.properties" ignore-resource-not-found="true"/>同样,这假设您的jdbc.properties位于类路径根中,如果您将它放入src/main/resources中,因为它将被复制到WEB-INF/classes中,那么这个根就会满足。您也可以使用其他路径(没有classpath:前缀),但我发现这是最简单的。了解Spring中的占位符解析在运行时发生,因此在尝试访问有线值之前不会得到错误。
https://stackoverflow.com/questions/25473648
复制相似问题