首页 > 代码库 > 解决EJB本地调用“java.lang.ClassCastException: $Proxy96 cannot be cast to com.tgb.ejb.UserManager”异常

解决EJB本地调用“java.lang.ClassCastException: $Proxy96 cannot be cast to com.tgb.ejb.UserManager”异常

   EJB本地调用方式:把Webclient和EJB服务端部署到同一个JBoss,client和server通过一个JVM进行通信。

   Web客户端本地调用时。需引用EJB服务端打包的jar,不需引用JBoss/client下的jar和src下的jndi.properties文件。调用方式例如以下:

<%@ page language="java"
	import="java.util.*,javax.naming.*,com.tgb.ejb.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<html>
	<head>
		<base href="http://www.mamicode.com/">
		<title>My JSP ‘index.jsp‘ starting page</title>
	</head>

	<body>
		<%
			InitialContext ctx = new InitialContext();
			UserManager usermanager = (UserManager) ctx
					.lookup("UserManagerBean/local");
			User user = new User();
			user.setUsername("quwenzhe");
			user.setPassword("quwenzhe123");

			usermanager.addUser(user);
			out.println("userId=" + user.getId());
		%>
	</body>
</html>
     在浏览器输入client公布的URL地址,JBoss会报例如以下异常:

13:02:30,924 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: $Proxy96 cannot be cast to com.tgb.ejb.UserManager
	at org.apache.jsp.index_jsp._jspService(index_jsp.java:79)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
	at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
	at java.lang.Thread.run(Thread.java:619)
    解决方式为:在JBoss服务器的jboss-4.2.3.GA\server\default\deploy\jboss-web.deployer\META-INF路径下找到jboss-service.xml文件,把<attribute name="UseJBossWebLoader">false</attribute>的false改为true,重新启动JBoss,问题解决。

   UseJBossWebLoader属性设置为true:表示Tomcat使用JBoss的统一类载入器作为Web应用的类载入器,这意味着WAR包中WEB-INF/classes和WEB-INF/lib文件夹里的类将被载入到缺省的共享类载入器仓库中。通过它。能够实现类和资源在Web应用之间共享。可是,假设这不是你想要的情况,能够通过将该属性设置为false来避免这样的行为。


   
通常情况下应该避免使用这样的情况。由于当不同的应用中拥有相同的类和资源的时候,JBoss不同应用之间会产生冲突;举比例如以下:两个war应用 WarA,WarB以下均有com.tgb.User.class文件;如今期望每一个War包以下分别用各自的User.class文件,当改动一个WarA的应用时候,tomcat会又一次部署以下应用。这个时候。就会出现WarA应用使用WarB以下的User.class,造成混乱。

   希望我的解说可以帮助大家进步。

解决EJB本地调用“java.lang.ClassCastException: $Proxy96 cannot be cast to com.tgb.ejb.UserManager”异常