问题
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.
回答1:
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 :)
回答2:
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());
}
回答3:
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.
回答4:
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
: addexcludes
fortomcat-embed-el
dependency fromspring-boot-starter-web
pom.xml
: add dependency forspring-boot-legacy
- add
web.xml
withcontextConfigLocation
,SpringBootContextLoaderListener
andappServlet
(in my previous configuration I had noweb.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.
来源:https://stackoverflow.com/questions/34408135/annotation-java-config-based-application-starts-to-look-for-xml-config-file-when