在Spring 3应用程序中Eclipse,Tomcat和JUnit之间的类路径问题 [英] Problems with classpath between Eclipse, Tomcat and JUnit in Spring 3 app

查看:153
本文介绍了在Spring 3应用程序中Eclipse,Tomcat和JUnit之间的类路径问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有基于Spring 3.0.3的Web应用程序,我一直在使用Eclipse 3.4进行开发。在这样做的时候,我一直在Eclipse中运行Tomcat 6.0.18中的网络应用程序。也就是说,我有Eclipse使用Tomcat安装,这意味着Tomcat会根据需要修改文件等(至少这是我对它的工作的理解)。



我的问题是指定web.xml中的contextConfigLocations的值。当在Eclipse内运行时,这个工作正常:

 < context-param> 
< param-name> contextConfigLocation< / param-name>
< param-value>
classpath:applicationContext.xml
classpath:applicationContext-security.xml
< / param-value>
< / context-param>

但是,当我将应用程序打包成一个war文件(ROOT.war),然后将其添加到Tomcat的webapp目录和尝试启动Tomcat,我收到一个错误,既不能找到这些applicationContext文件。但是当我将其更改为以下时,Tomcat可以找到文件:

 < context-param> 
< param-name> contextConfigLocation< / param-name>
< param-value>
/WEB-INF/config/applicationContext.xml
/WEB-INF/config/applicationContext-security.xml
< / param-value>
< / context-param>

我应该注意,applicationContext.xml包括其他也使用classpath的applicationContext文件:short hand。在Tomcat中运行时,我需要删除所有使用classpath:相对路径让Tomcat看到这些文件。



很棒。 Tomcat和Eclise正在相处得很好。但是JUnit 4.7已经不再开心了。无论什么原因,除非使用classpath:short hand,否则无法找到在测试类中使用@ContextConfiguration指定的文件。以下是一个例子:

  @ContextConfiguration(locations = {classpath:applicationContext.xml,classpath:applicationContext-security。 xml})
public class UserDaoTest extends AbstractTransactionalJUnit4SpringContextTests {

@Test
public void testCreateUser(){
}

所以applicationContext.xml和applicationContext-security.xml被发现没有问题;但是,找不到在applicationContext.xml中指定的属性文件。

 < bean id =appPropertiesclass =org.springframework.beans.factory.config.PropertiesFactoryBean> 
< property name =singletonvalue =true/>
< property name =ignoreResourceNotFoundvalue =true/>
< property name =locations>
< list>
< value> /WEB-INF/config/spring/base.spring-config.properties< / value>
< value> /WEB-INF/config/spring/local.spring-config.properties< / value>
< / list>
< / property>
< / bean>

但是,如果我使用类路径指定这些文件的位置:短手,找到属性文件。如果我这样做,那么从Tomcat中的war文件运行时,文件将不会被发现。



现在我创建了一个applicationContext-test.xml是所有其他applicationContext文件的剪切和粘贴集合,其中我正在使用类路径:short hand。这似乎是黑客和容易出错的,我想知道所有这些技术可能出现什么问题。



反馈最受欢迎!

解决方案

web.xml内容应该像

 < context-param> 
< description>
弹簧上下文配置。
< / description>
< param-name> contextConfigLocation< / param-name>
<! - 弹簧加载全部 - >
< param-value>
classpath *:spring / *。xml,
classpath *:spring / persistence / *。xml,
classpath *:spring / webapp / *。xml< / param-value>
< / context-param>

请参阅 http://static.springsource.org/spring/docs/2.5.x/reference/resources .html#resources-app-ctx-wildcards-in-resource-paths 以供进一步参考



junit配置应遵循与classpath *相同的惯例:



但请注意,spring可能会加载您不希望的.xml上下文文件


I have web app, based on Spring 3.0.3, that I've been developing using Eclipse 3.4. While doing so I've been running the web app in Tomcat 6.0.18 from Eclipse. That is, I have Eclipse use the Tomcat installation meaning that Tomcat will, as need, modify files etc. (at least, that's my understanding of what it's doing).

My problem is specifying the values for the contextConfigLocations in the web.xml. When running from within Eclipse this worked fine:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
        classpath:applicationContext-security.xml
    </param-value>
</context-param>

However, when I package the app into a war file (ROOT.war) and then added it to Tomcat's webapp directory and the try to start Tomcat, I get an error that neither of these applicationContext files can be found. But when I change it to below, Tomcat can find the files:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/config/applicationContext.xml
        /WEB-INF/config/applicationContext-security.xml
    </param-value>
</context-param>

I should note that applicationContext.xml includes other applicationContext files that also use the classpath: short hand. When running within Tomcat, I need to drop all use of classpath: in favor of relative paths to get Tomcat to see these files.

Great. Tomcat and Eclise are getting along nicely. But JUnit 4.7 is no longer happy. For whatever reason, files specified using @ContextConfiguration in a test class can't be found unless the classpath: short hand is used. Here is an example:

@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:applicationContext-security.xml"})
public class UserDaoTest extends AbstractTransactionalJUnit4SpringContextTests {

    @Test
    public void testCreateUser() {
    }

So applicationContext.xml and applicationContext-security.xml are found without a problem; however, property files that are specified in applicationContext.xml are not found.

<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="singleton" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
            <list>
                    <value>/WEB-INF/config/spring/base.spring-config.properties</value>
                    <value>/WEB-INF/config/spring/local.spring-config.properties</value>
            </list>
    </property>
</bean>

But if I specify the location of these files using the classpath: short hand, the property files are found. If I do this though, the files won't be found when running from a war file in Tomcat.

For now I've created a applicationContext-test.xml that is a cut-and-paste conglomeration of all of the other applicationContext files wherein I'm using the classpath: short hand. This seems hacky and error prone and I'm wondering what the issue might be across all of these technologies.

Feedback most welcome!

解决方案

web.xml content should look like

    <context-param>
    <description>
                 Spring Context Configuration.
            </description>
    <param-name>contextConfigLocation</param-name>
    <!-- spring loads all -->
    <param-value>
                classpath*:spring/*.xml,
                classpath*:spring/persistence/*.xml,
                classpath*:spring/webapp/*.xml</param-value>
    </context-param>

see http://static.springsource.org/spring/docs/2.5.x/reference/resources.html#resources-app-ctx-wildcards-in-resource-paths for further reference

the junit config should follow the same convention with classpath*:

but beware spring might load .xml context files you don't want it to do

这篇关于在Spring 3应用程序中Eclipse,Tomcat和JUnit之间的类路径问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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