在Eclipse上的服务器上运行Maven webapp,并从配置文件中注入属性

时间:2014-04-22 13:10:50

标签: java eclipse spring maven tomcat

我使用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进行部署可以正常工作,但是当我选择&#34时,右键单击&gt;以&gt;运行在服务器&#34; 上运行,我得到了很多与占位符相关的例外:

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中获取日志?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

发现它!

当我使用Maven Eclipse插件时,我有一个&#34; Maven&#34;项目属性下的条目(项目&gt;属性&gt; Maven)。我只需要激活&#34;开发&#34;简介,它工作!

Project Properties