Annotation/java config based application starts to

2019-04-09 20:33发布

My spring java-config application packed as war runs wihout problem on weblogic 12.1.3 so I tried to deploy same war into weblogic 12.2.1 where it causes java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/DispatcherServlet-servlet.xml]. It seems like DispatcherServlet servlet is initialized with XmlWebApplicationContext (default one) instead of AnnotationConfigEmbeddedWebApplicationContext in 12.2.1 even when war is the same. Has someone any idea what was changed in weblogic implementation since previous version what is causing this problem?

Using same war:

  • in WLS 12.1.3 it is working without issue, application configures using annotations/java
  • in WLS 12.2.1 the same application looks for xml configuration at some point instead of configuring it using annotations/java as in 12.1.3.

4条回答
孤傲高冷的网名
2楼-- · 2019-04-09 21:18

Had the same problem. I guess there is something wrong with WebLogic 12.2.1.

Try to manually set context class for Spring servlet, like so:

public class ApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        // <some context configuration>

        ServletRegistration.Dynamic spring = container.addServlet("Spring", new DispatcherServlet(context));
        // <some servlet configuration>

        // Here, set desired context class using 'contextClass' parameter.
        spring.setInitParameter("contextClass", context.getClass().getName());

        container.addListener(new ContextLoaderListener(context));
    }

}

And everything will work fine again :)

查看更多
叼着烟拽天下
3楼-- · 2019-04-09 21:21

I managed to get spring-boot 1.3.6.RELEASE working on Weblogic 12.2.1.

I had to create an empty (no declared beans) dispatcherServlet-servlet.xml file on src/main/webapp/WEB-INF directory.

Here is the file content:

    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">

    </beans>

In addition, I made my main application class implement WebApplicationInitializer as stated on Spring Boot Ref Guide.

I am not sure if there are now two application contexts, but the one who is answering all my API calls seems to be working fine (dependency injection, aop, jdbc, jpa, logging, session context, actuator, etc).

PS: I have tried @amariq solution but it didn't work on Weblogic 12.2.1, only the one described above.

查看更多
Deceive 欺骗
4楼-- · 2019-04-09 21:28

Sorry , I can't add a comment on the response of amariq.

Just a comment: if your WebApplicationInitializer extends AbstractDispatcherServletInitializer, you can override the method customizeRegistration like:

@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
    registration.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
}
查看更多
可以哭但决不认输i
5楼-- · 2019-04-09 21:31

I had the exact same issue here and tried the solutions suggested above, but they didn't work for me. I finally managed to make it work by doing a few modifications using this GitHub repo as reference.

They main points I could outline are:

  • pom.xml: add excludes for tomcat-embed-el dependency from spring-boot-starter-web
  • pom.xml: add dependency for spring-boot-legacy
  • add web.xml with contextConfigLocation, SpringBootContextLoaderListener and appServlet (in my previous configuration I had no web.xml at all)

Take a look at https://github.com/DISID/disid-proofs/tree/master/spring-boot-weblogic for further details.

I hope it helps.

查看更多
登录 后发表回答