struts2 异常Unable to load bean 文件struts-default.xml
摘要: 项目中需要升级struts到struts2.5.14.1,这个版本有较大的改动,于是出现了较多的jar包冲突。很郁闷的是,很多问题在jetty服务器下,才会发生。
日志的处理
struts2.5.14.1的日志使用了log4j2的配置,如果项目中没有用到log4j,可能需要在引入struts的pom配置中exclude相关的日志处理包。并且在项目的合适位置引入log4j2.xml
的配置文件。
关于jar包xwork-core
struts的低版本中用到了xwork-core,但是目前该包的最高版本是2.3.34,已经不能和struts版本保持一致,是因为struts放弃了对xwork-core的维护,而将struts中需要的xwork-core的内容全部移入到了struts-core中。所以,请不要在高版本的struts项目中引入以下依赖:
<dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.x.y</version>
</dependency>
freemarker.jar的冲突
新版的struts中用到了新版本的freemarker可能会导致冲突,需要exclude
asm.jar的冲突
新版的struts中用到了新版本的asm可能会导致冲突,需要exclude
Unable to load bean
这个异常有好多个变种,它们的原因基本都是jar包冲突。
Unable to load configuration. - bean - xxx/struts2-core-2.x.y.jar!/struts-default.xml:29:72
这个异常特别常见,出现这个异常,首先应该去确认项目中是否引入了两个版本不同的struts2-core-xxx.jar
定位struts-default.xml文件到第29行,总之是错误发生的那一行。比如可能是下面这行:
<bean type="org.apache.struts2.components.template.TemplateEngine" name="jsp"
class="org.apache.struts2.components.template.JspTemplateEngine" />- 结合错误中的
Caused by:
后面的内容,找到问题的发生原因,下面这个是ClassNotFoundException
,说明肯定缺少jar包
Caused by: java.lang.ClassNotFoundException: javax.servlet.jsp.JspWriter
- 手动导入相关的jar包,比如在pom.xml中加入下面依赖:
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
Unable to load bean: type:com.opensymphony.xwork2.XXX /struts2-core-2.0.11.2.jar!/struts-default.xml:135:154
- 首先应该去确认项目中是否引入了两个版本不同的struts2-core-xxx.jar
- 还好定位struts-default.xml文件到错误发生的那一行第135行
- 结合错误中的
Caused by:
后面的内容
Caused by : java.lang.IncompatibleClassChangeError: Implementing class
- 这就是说,有一个jar包的版本太低,不能满足struts的基本需求,需要升级相应jar包到最新版。
Unable to load bean:type:com.opensymphony.xwork2.TextProviderFactory
如果有上面这个问题,直接移除xwork-core的依赖就可以了。
Caused by: Unable to load bean: type:com.opensymphony.xwork2.ObjectFactory with the name spring has already been loaded by bean
如果有上面这个问题,一般是因为在struts整合spring的时候,struts的版本与struts2-spring-plugin-xxx.jar的版本不对应。