【问题标题】:Run Maven webapp on server from Eclipse with properties injection from profile从 Eclipse 使用配置文件中的属性注入在服务器上运行 Maven webapp
【发布时间】:2014-04-22 13:10:50
【问题描述】:

我使用 Eclipse 和 Maven 开发 Spring + Hibernate webapp,在我的 pom.xml 中,我有几个具有不同属性的配置文件,我将这些配置文件注入到 .properties 文件中。这个文件在applicationContext.xml 中用于配置 Spring,当我想启动应用程序时,我不得不将它部署在 Eclipse 外部正在运行的 tomcat 上。这是我的代码的一些部分:

pom.xml:

...
<profile>
    <id>development</id>
    <properties>
        <db.driverClassName>com.mysql.jdbc.Driver</db.driverClassName>
        <db.name>mydbname</db.name>
        <db.params>?createDatabaseIfNotExist=true&amp;amp;useUnicode=true&amp;amp;characterEncoding=utf-8</db.params>

        <hibernate.hbm2ddl.auto>update</hibernate.hbm2ddl.auto>
        <hibernate.show_sql>true</hibernate.show_sql>

        <log4j.spring.level>WARN</log4j.spring.level>
        <log4j.hibernate.level>WARN</log4j.hibernate.level>
    </properties>
</profile>
...

application.properties:

...
# JDBC Information
# Some properties are in my .m2/settings.xml
jdbc.driver.classname=${db.driverClassName}
jdbc.url=${db.url}/${db.name}${db.params}
jdbc.user=${db.username}
jdbc.pass=${db.password}
...

applicationContext.xml:

...
<!-- JDBC Data Source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.driver.classname}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.user}" />
    <property name="password" value="${jdbc.pass}" />
</bean>
...

使用 Tomcat7 Maven 插件,通过mvn tomcat7:deploy 部署工作正常,但是当我选择“右键单击 > 运行方式 > 在服务器上运行” 时,我得到了很多与占位符:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'db.driverClassName' in string value "${db.driverClassName}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:161)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:180)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:145)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:167)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:204)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82)
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:206)
    ... 17 more

如何在 Eclipse 嵌入式 Tomcat 中运行我的项目并在控制台而不是 logs/catalina.out 中获取日志?

感谢您的帮助。

【问题讨论】:

  • 为什么你不把属性放在属性文件中而不是注入它们?
  • 因为我有多个配置文件,并且值取决于用于构建的配置文件:)

标签: java eclipse spring maven tomcat


【解决方案1】:

找到了!

当我使用 Maven Eclipse 插件时,我在项目属性下有一个“Maven”条目(项目 > 属性 > Maven)。我只需要激活“开发”配置文件就可以了!

【讨论】:

    猜你喜欢
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 2016-03-21
    • 1970-01-01
    • 2016-02-02
    相关资源
    最近更新 更多