简体   繁体   中英

Unable to load BeanIO mapping from JAR file in WAR application

I have an application loading its Spring context with XML configuration.

Context files are stored in a jar's classpath (in the META-INF/context directory).

Introducing a new bean, I want to load a beanio mapping file from within the classpath of the same archive. With Tomcat running within Eclipse it all works as a charm, but when I deploy a WAR file to our QA environment I get the application not starting.

Let me walk the code:

public class BeanIoParser implements Parser, Iterator<Message>, InitializingBean
{
    private StreamFactory streamFactory;

    private Resource resourceMapping; //Injected via Context

    @Override
    public void afterPropertiesSet() throws Exception
    {
        streamFactory = StreamFactory.newInstance();
        streamFactory.load(resourceMapping.getFile()); //boom here
    }
}

And then the context

<bean id="messagesParser" class="it.acme.BeanIoParser" scope="prototype">
    <property name="resourceMapping" value="classpath:META-INF/mappings/messages-beanio.xml" />
</bean>

And then what? Uhm... I have double checked the built jar file by uncompressing the war file first. It all matches.

I can see my messages-beanio.xml file correctly.

But when I start the application I get the following root cause :

java.io.FileNotFoundException: class path resource [META-INF/mappings/messages-beanio.xml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%208.0/temp/11-app/WEB-INF/lib/app-3.0.0.20151105.jar!/META-INF/mappings/messages-beanio.xml

I have also tried another way, by using classpath* (because we already faced this problem when loading Spring context from multiple jar files)

So if my bean declaration becomes

<property name="resourceMapping" value="classpath*:META-INF/mappings/messages-beanio.xml" />    

My error becomes

java.io.FileNotFoundException: ServletContext resource [/classpath*:META-INF/mappings/messages-beanio.xml] cannot be resolved to absolute file path - web application archive not expanded?

How can I fix?

Solved differently. I programmatically instantiate the ClasspathResource with path starting with META-INF and not classpath:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM