Weblogic Server 12c: Differences from Tomcat when deploying a web application

Let’s suppose that we have a spring web application, which is already deployed in Tomcat and we want to migrate to Weblogic Server.

We must take care of the following:

First create an xml file with the name weblogic.xml and write the following:

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app">
    <context-root>MyApplicationName</context-root>
    <container-descriptor>
      <prefer-web-inf-classes>true</prefer-web-inf-classes>
      </container-descriptor>
</weblogic-web-app>

and place it at the same place with web.xml.

1) In weblogic, the context-root of the application is not given by the .war name, like Tomcat, but from the tag <context-root> of weblogic.xml

2) If the second part of the xml shown above, (container-descriptor/prefer-web-inf-classes) is omitted or it has value “false”, 

then it is possible that we will get the following error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sessionFactory’ defined in class path resource [resources/hibernate/Hibernate.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError

This happens because weblogic tries to use its own hibernate classes, which are likely to be different from those required.

By setting prefer-web-inf-classes=true, weblogic will always use the classes included in WEB-INF folder of the .war file.

3) In weblogic the double dot, indicating one-level-up in directory structure, does not work in the case of loading

the application context of spring, using e.g. ClassPathXmlApplicationContext(“../servlets/root-context.xml”).

Weblogic assumes we are always in the classpath, when Tomcat can understand this escape to the parent directory, even if the current is classpath.

So, in this case weblogic server, would raise the following “unexplained” error:

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [../servlets/root-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [../servlets/root-context.xml] cannot be opened because it does not exist

when Tomcat would run without problem.

The solution is to change the location of the root-context.xml to some place under the classpath (preferably exactly under the classpath).

This way both Tomcat and Weblogic will be able to locate it.

Thus, we need 3 changes:

a) relocate the file root-context.xml

b) change the following part of the web.xml:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/classes/root-context.xml</param-value>
    </context-param>

c) Change the code of application context initiatialization, without any directories:

ApplicationContext appContext = new ClassPathXmlApplicationContext("root-context.xml");

Leave a Reply