开发者

FileNotFoundException for bean definition file happens only online, not on localhost

开发者 https://www.devze.com 2023-04-09 19:40 出处:网络
When I published a war file for an application that works locally with Eclipse WTP, I had a FileNotFoundException for the bean.xml file with my beans definitions.

When I published a war file for an application that works locally with Eclipse WTP, I had a FileNotFoundException for the bean.xml file with my beans definitions.

SEVERE: Exception sending context initialized event to listener instance of 
    class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: 
   IOException parsing XML document from class path resource 
   [META-INF/spring/beans.xml]; nested exception is java.
io.FileNotFoundException: class path resource [META-INF/spring/beans.xml] 
    cannot be opened because it does not exist
    at Caused by: java.io.FileNotFoundException: class path resource 
    [META-INF/spring/beans.xml] cannot be opened because it does not exist
    ...

I created the war file with mvn war:war and copied in the webapps folder of Tomcat 7.

beans.xml is located in src/main/resources/META-INF/spring/beans.xml and I've the following in my pom.xml:

<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <webResources>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

In the war file beans.xml gets packaged in META-INF/spring/beans.xml

In my web.xml I've:

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:META-INF/spring/beans.xml</param-value>
</context-param>

However the file is not found. How to solve the problem?

UPDATE: as Matthew Farwell suggested, is bean.xml is not packaged in the right location, so it's not in the class path, I think it's specified with maven-war-plugin parameters, now I try to look at its documentation. If someone knows it would be helpful.开发者_运维技巧

UPDATE 2: As explained in maven-war-plugin documentation, there is an optional parameter called targetPath. I tried and after changing maven-war-plugin configuration adding targetPath it gets packaged correctly.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
        <webResources>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>WEB-INF/classes</targetPath>
            </resource>
        </webResources>
    </configuration>
</plugin>

UPDATE 3: About Ryan Stewart's suggestion, I started my initial pom setup using roo, but after that I've done many changes and I'm not using roo any more. The directory src/main/resources is not mentioned in any other places in pom.xml (I've used grep), however the only setting that looks suspicious to me is:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.5</version>
    <configuration><encoding>UTF-8</encoding></configuration>
</plugin>

I've commented out that plugin, but nothing changed, then I commented out the configuration part of maven-war-plugin, but src/main/resources was not added to the war anymore, so for now I've added it back and I'm uploading it to test it online (it's still a staging server actually, not the final one anyway).

UPDATE 4 Ryan Stewart suggested that the problem was that I was running "mvn war:war" instead of "mvn package", and that was indeed the problem. With my targetPath, the resources appeared in WEB-INF/classes, but there weren't any classes there.

I was fighting an uphill battle, while instead the simpler solution was to remove the configuration part as in update 3, and use "mvn package" to build the war file. Thank you to both of you Ryan and Matthew, not only I solved my problem, but I've also learnt something more about Maven.


I have to assume you have another part of the POM that's excluding the file in question from being processed as a classpath resource, else it should be working. Either

  1. Stop doing that, and it'll work fine--the content of src/main/resources becomes classpath resources by default--or
  2. remove the classpath: from your path. Without that prefix, the path given in contextConfigLocation will be resolved against the root of the WAR file, and it will correctly find your file in META-INF/spring.

If you take path 1, then you should remove the webResources section, or you'll end up with the file in two places--not problematic, but potentially confusing.


In a war, / is not part of the classpath for a webapp. The classpath includes /WEB-INF/classes and all of the jars in /lib. See Apache Tomcat 6.0 - Class Loader HOW-TO

WebappX — A class loader is created for each web application that is deployed in a single Tomcat instance. All unpacked classes and resources in the /WEB-INF/classes directory of your web application, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application, are made visible to this web application, but not to other ones.

The other web servers will have similar rules. If you wish to reference something as part of the classpath, put it in WEB-INF/classes.

0

精彩评论

暂无评论...
验证码 换一张
取 消