版本:WebLogic 10.3.5.0
以Spring为例,在此版本的WebLogic中自带了Spring2.5版本,如下图
而我在程序中使用了Spring 3.0的包,当使用以下代码时
ServletContext servletContext = this.getServletContext();;
//获取Spring上下文
WebApplicationContext wac =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
wac.getBean(XXXX.class);
报错
java.lang.NoSuchMethodError: org/springframework/web/context/WebApplicationContext.getBean(Ljava/lang/Class;)
于是我们知道是版本问题,在低版本的BeanFactory中似乎还没有getBean(java.lang.Class clazz)这个方法。
解决方法如下:
在Web工程的WEB-INF目录下加入weblogic.xml文件,文件内容
<?xml version = '1.0' encoding = 'UTF-8'?>
<weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd"
xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
<context-root>test</context-root>
<container-descriptor>
<!-- 优先加载web工程中的包 -->
<prefer-web-inf-classes>true</prefer-web-inf-classes>
</container-descriptor>
</weblogic-web-app>
OK,问题解决!仅以Spring为例,其它类似情况可以参照。