【问题标题】:Error using maven profiles使用 Maven 配置文件时出错
【发布时间】:2014-08-24 16:10:05
【问题描述】:

我在我的应用程序中添加了两个配置文件,它的外观如下:

<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

<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>

【问题讨论】:

  • 您在运行 Maven 构建时是否激活了您的配置文件之一?
  • manouti,当然,无论如何我可以让 maven clean 然后打包,当我使用 tomcat 运行我的应用程序时出现上面提供的错误

标签: java spring maven pom.xml


【解决方案1】:

按照您现在的设置方式,资源被过滤,但最终位于 classpath-root 中,在本例中为 WEB-INF/classes。

你想让maven-war-plugin do filtering on web-resources.

【讨论】:

    【解决方案2】:

    该错误似乎表明属性文件中的值没有被替换,因为它抱怨db.driverClass 无法解析而不是jdbc.driverClassName。查看构建的工件中的属性文件是什么样的。看起来您没有在 Maven 中启用资源过滤,或者您忘记激活任何配置文件。 要启用resource filtering,请将类似这样的内容添加到您的&lt;build&gt;

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    

    这假定您过滤的资源(如您的 jdbc.properties)驻留在通常的 src/main/resources 中,如果 当前不是这种情况。我建议您首先将所有过滤的资源移到那里,除非您有充分的理由反对它,因为它是迄今为止最简单的设置。确保您不要尝试过滤二进制资源(如图像)!了解资源过滤(例如将占位符转换为实际值)在构建时发生,因此您应该能够在生成的 WAR 中看到替换的值。

    似乎你在 Spring 中的占位符解析已经配置好以读取属性文件,但以防万一,你应该有这样的东西:

    <context:property-placeholder location="classpath:/jdbc.properties" ignore-resource-not-found="true"/>
    

    再次假设您的 jdbc.properties 位于类路径根目录中,如果您将它放在 src/main/resources 中,这将得到满足,因为它将被复制到 WEB-INF/classes。 您也可以使用其他路径(不带classpath: 前缀),但我发现这是最简单的。了解 Spring 中的占位符解析发生在运行时,因此在您尝试访问有线值之前不会收到错误。

    【讨论】:

    • 我已经更新了我上面的问题并从我的 pom.xml 添加了构建部分
    • 根据您的修改更新了答案。
    • 好的,在您进行第二次编辑后,我看到资源已被过滤,但根据@Maarten Winkels,重要的是要了解您的资源最终在哪里。打开包装并查看生成的 WAR,以更好地了解发生的情况。正如他所建议的,您可以配置 Maven WAR 插件以包含来自不同路径的资源,但是您还需要正确配置 Spring 占位符解析器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2013-01-06
    • 2021-11-08
    • 1970-01-01
    • 2016-10-13
    • 2012-08-06
    • 2012-12-01
    相关资源
    最近更新 更多