通过配置文件中的属性注入在Eclipse的服务器上运行Maven Webapp [英] Run Maven webapp on server from Eclipse with properties injection from profile

查看:142
本文介绍了通过配置文件中的属性注入在Eclipse的服务器上运行Maven Webapp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Eclipse& Maven开发了一个Spring + Hibernate Webapp,在我的pom.xml中,我有几个配置文件,这些配置文件具有不同的属性,可以注入到.properties文件中.该文件在applicationContext.xml中用于配置Spring,并且当我要启动该应用程序时,必须将其部署在Eclipse之外正在运行的tomcat上.这是我的代码的一部分:

I use Eclipse & Maven to develop a Spring + Hibernate webapp, and in my pom.xml I have several profiles with different properties that I inject to a .properties file. This file is used in applicationContext.xml to configure Spring, and when I want to launch the application, I'm obliged to deploy it on a running tomcat outside Eclipse. Here is some parts of my code :

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进行部署可以很好地工作,但是当我选择右键单击>运行方式>在服务器上运行" 时,我得到了很多与占位符:

Using the Tomcat7 Maven plugin, deployment via mvn tomcat7:deploy works fine, but when I choose "Right-click > Run as > Run on server", I get a lot of exceptions related to the placeholders :

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

How can I run my project in the Eclipse embedded Tomcat and get the logs in the console instead of in logs/catalina.out ?

感谢您的帮助.

推荐答案

找到了!

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

As I'm using the Maven Eclipse plugin, I have a "Maven" entry under the project properties (Project > Properties > Maven). I just had to activate the "development" profile and it worked !

这篇关于通过配置文件中的属性注入在Eclipse的服务器上运行Maven Webapp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆