JavaEE5实战笔记01EJB环境搭建及异常解决

1.       说明

此实战笔记是为了之前的学习笔记做一个实践,因为任何零散的知识点只有整合起来才更能发现问题、解决问题、从问题中提高。这次项目实战是打算做一个微型的在线网络银行,系统软件开发框架使用JSFRI+tomahawk12-1.1.10.jar组件)+JBoss5+EJB3.0规范+JPAJBoss内部的Hibernate实现)+Oracle10g实现。还是那句老话,有什么心得先写出来,事后恐怕就没那么多感受了。有什么错误之后再更正呗。

2.       环境搭建

采用MyEclipse8.6进行软件开发,首先建立JavaEE Web项目,之后陆续加入JPA以及JSF规范支持。JPA选择Hibernate实现即可、JSF使用默认配置。建立完成后项目特性应该有如下几个
JavaEE5实战笔记01EJB环境搭建及异常解决_第1张图片
 添加完特性之后会有很多重复的jar包在项目中引用,将其从项目的build path中删除掉即可,之后再引入本项目用到的其他jar包。如下
JavaEE5实战笔记01EJB环境搭建及异常解决_第2张图片
 log4j:这个不用解释了吧,在EJB开发中最麻烦的就是部署和调试,很头疼,有一个好的日志输出给程序的调试和排错解决了不少问题。

ojdbcOracleJDBC驱动

jsf-faceletsJSF的一个页面框架

jsf-el-riJSF相关包

jsf-oroJSF相关包

jsf-portletJSF关于portlet框架的相关包

tomahawkJSF的开源组件

因为这个系统底层采用的是Hibernate作为JPA标准的框架,其实JBoss自己本身已经包含了Hibernate的相关核心包,只是笔者在持久层加入了Hibernate的特殊性配置,所以这里不得不引入HibernateAnnotations特性包。

至于JavaEE的标准嘛~采用的是JavaEE5的标准。

目前先加入这么多jar包,之后可能随着需求的变化不断地往里面加入相关jar文件。

3.       代码包结构

笔者的实践项目叫做WebBank。项目包结构如下

ejb
ejb.dto
ejb.eao
ejb.eao.base
ejb.eao.impl
ejb.service
ejb.service.base
ejb.service.impl
util
web
web.action
web.managedBean
/WebBank/src/META-INF
/WebBank/src/log4j.properties
/WebBank/resource/WebBank/srcTest

 如图
JavaEE5实战笔记01EJB环境搭建及异常解决_第3张图片
 

说明:

srcsrcTestresource都是build path包,src是项目源代码包,srcTest是单元测试源码包、resource是资源配置文件、国际化文件配置包。

src下面的ejb包是放实体Bean、会话bean、消息驱动bean的包,不仅如此,EJB组件通常都对外面提供一种faces的调用模式,因此还需要service层进行包装。util包专门放着一些辅助类,web包实际上就是放着JSF web层的控制器。因为JSF的托管bean可以服用ejb的实体bean,也就是dto中的实体类。那么其实这里manageBean并没什么用。不过先放着,给特殊的托管Bean预留。

4.       系统运行流程分析

我们来分析一下一般JavaEE软件的运行流程,流程图如下
JavaEE5实战笔记01EJB环境搭建及异常解决_第4张图片
 用户从浏览器访问系统,JSF引擎开始工作,经过JSF组件的生命周期接收来自客户端的请求,将组件属性值注入到JSF控制器中,JSF控制器的托管Bean经过setter后已经被注入了相关request的值,之后调用业务Bean处理业务逻辑,业务beanEJB容器为其注入的组件,不必setget,直接使用即可。业务处理Bean实际就是service层,service包装了EJB容器本地的会话EAOBean,本地EAOBean也是EJB容器注入到service层的。通过EAOBean操作实体Bean完成业务逻辑。在JSF控制器层返回的结果映射,将视图恢复后、组件渲染也完毕后,呈献给客户端。

5.       遇到的异常问题以及解决方式

有句话说得好,事与愿违。虽然之前做了那么多铺垫,但是EJB就是EJB,和轻量级框架系统就是不一样。没办法,谁让咱用到了这种分布式集群解决方案呢。可能用一般的SSH框架很简单就将系统搭建完成,但是遇到EJB容器很多人还是遇到了问题,下面我们来看看仅仅是一个简单程序所经历的各种异常吧。

1):错误如下

Caused by: org.jboss.xb.binding.JBossXBRuntimeException: {http://java.sun.com/xml/ns/javaee}param-name not found as a child of {http://java.sun.com/xml/ns/javaee}description in unordered_sequence: {http://java.sun.com/xml/ns/javaee}key?
	at org.jboss.xb.binding.sunday.unmarshalling.SundayContentHandler.startElement(SundayContentHandler.java:383)
	at org.jboss.xb.binding.parser.sax.SaxJBossXBParser$DelegatingContentHandler.startElement(SaxJBossXBParser.java:401)
	at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
	at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
	at org.apache.xerces.xinclude.XIncludeHandler.startElement(Unknown Source)
	at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
	at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
	at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
	at org.jboss.xb.binding.parser.sax.SaxJBossXBParser.parse(SaxJBossXBParser.java:199)
	... 41 more

 在部署web工程的时候在纯tomcat下没问题,一到了JBoss下就会出现这个异常。

解决办法

web.xml中的<context-param>不能加<description> </description>标签,可能是因为JBoss5的tomcat版本比较老了,因此不支持web工程加以上标签

 2):异常

org.hibernate.AnnotationException: Unknown Id.generator: system-uuid
	at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:428)
	at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1901)
	at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1279)
	at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
	at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
	at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)

 这是因为笔者在实体bean中的主键生成策略是UUID,而且使用了Hibernate的特殊标记,所以此时应该引入相关包

解决方法:

hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate-validator.jar

 3):异常

java.lang.ClassCastException: org.apache.xerces.jaxp.SAXParserFactoryImpl cannot be cast to javax.xml.parsers.SAXParserFactory
	at javax.xml.parsers.SAXParserFactory.newInstance(Unknown Source)
	at com.sun.facelets.compiler.SAXCompiler.createSAXParser(SAXCompiler.java:271)
	at com.sun.facelets.compiler.SAXCompiler.doCompile(SAXCompiler.java:231)
	at com.sun.facelets.compiler.Compiler.compile(Compiler.java:105)
	at com.sun.facelets.impl.DefaultFaceletFactory.createFacelet(DefaultFaceletFactory.java:218)
	at com.sun.facelets.impl.DefaultFaceletFactory.getFacelet(DefaultFaceletFactory.java:149)
	at com.sun.facelets.impl.DefaultFaceletFactory.getFacelet(DefaultFaceletFactory.java:100)
	at com.sun.facelets.FaceletViewHandler.buildView(FaceletViewHandler.java:519)
	at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:569)
	at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:110)
	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
	at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
	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:235)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
	at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
	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:158)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:662)

 解决办法

如果项目中有xml-apis.jar,将xml-apis.jar包去掉

4):异常

下面可能是很多人都会遇到的异常,异常信息虽然比较多,但是我还是想把完整的异常信息贴出来

java.lang.ClassCastException: org.hibernate.ejb.HibernatePersistence cannot be cast to javax.persistence.spi.PersistenceProvider
	at org.jboss.jpa.deployment.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:300)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.jboss.reflect.plugins.introspection.ReflectionUtils.invoke(ReflectionUtils.java:59)
	at org.jboss.reflect.plugins.introspection.ReflectMethodInfoImpl.invoke(ReflectMethodInfoImpl.java:150)
	at org.jboss.joinpoint.plugins.BasicMethodJoinPoint.dispatch(BasicMethodJoinPoint.java:66)
	at org.jboss.kernel.plugins.dependency.KernelControllerContextAction$JoinpointDispatchWrapper.execute(KernelControllerContextAction.java:241)
	at org.jboss.kernel.plugins.dependency.ExecutionWrapper.execute(ExecutionWrapper.java:47)
	at org.jboss.kernel.plugins.dependency.KernelControllerContextAction.dispatchExecutionWrapper(KernelControllerContextAction.java:109)
	at org.jboss.kernel.plugins.dependency.KernelControllerContextAction.dispatchJoinPoint(KernelControllerContextAction.java:70)
	at org.jboss.kernel.plugins.dependency.LifecycleAction.installActionInternal(LifecycleAction.java:221)
	at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:54)
	at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:42)
	at org.jboss.dependency.plugins.action.SimpleControllerContextAction.simpleInstallAction(SimpleControllerContextAction.java:62)
	at org.jboss.dependency.plugins.action.AccessControllerContextAction.install(AccessControllerContextAction.java:71)
	at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextActions.java:51)
	at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:348)
	at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:1631)
	at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:934)
	at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1082)
	at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:984)
	at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:774)
	at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:540)
	at org.jboss.deployers.vfs.deployer.kernel.BeanMetaDataDeployer.deploy(BeanMetaDataDeployer.java:121)
	at org.jboss.deployers.vfs.deployer.kernel.BeanMetaDataDeployer.deploy(BeanMetaDataDeployer.java:51)
	at org.jboss.deployers.spi.deployer.helpers.AbstractSimpleRealDeployer.internalDeploy(AbstractSimpleRealDeployer.java:62)
	at org.jboss.deployers.spi.deployer.helpers.AbstractRealDeployer.deploy(AbstractRealDeployer.java:50)
	at org.jboss.deployers.plugins.deployers.DeployerWrapper.deploy(DeployerWrapper.java:171)
	at org.jboss.deployers.plugins.deployers.DeployersImpl.doDeploy(DeployersImpl.java:1439)
	at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1157)
	at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1178)
	at org.jboss.deployers.plugins.deployers.DeployersImpl.install(DeployersImpl.java:1098)
	at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:348)
	at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:1631)
	at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:934)
	at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1082)
	at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:984)
	at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:822)
	at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:553)
	at org.jboss.deployers.plugins.deployers.DeployersImpl.process(DeployersImpl.java:781)
	at org.jboss.deployers.plugins.main.MainDeployerImpl.process(MainDeployerImpl.java:702)
	at org.jboss.system.server.profileservice.repository.MainDeployerAdapter.process(MainDeployerAdapter.java:117)
	at org.jboss.system.server.profileservice.repository.ProfileDeployAction.install(ProfileDeployAction.java:70)
	at org.jboss.system.server.profileservice.repository.AbstractProfileAction.install(AbstractProfileAction.java:53)
	at org.jboss.system.server.profileservice.repository.AbstractProfileService.install(AbstractProfileService.java:361)
	at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:348)
	at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:1631)
	at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:934)
	at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1082)
	at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:984)
	at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:822)
	at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:553)
	at org.jboss.system.server.profileservice.repository.AbstractProfileService.activateProfile(AbstractProfileService.java:306)
	at org.jboss.system.server.profileservice.ProfileServiceBootstrap.start(ProfileServiceBootstrap.java:271)
	at org.jboss.bootstrap.AbstractServerImpl.start(AbstractServerImpl.java:461)
	at org.jboss.Main.boot(Main.java:221)
	at org.jboss.Main$1.run(Main.java:556)
	at java.lang.Thread.run(Thread.java:662)

 这是因为笔者想偷懒,因为EJB项目打包、部署、重启JBoss十分费时间,所以就想将整个项目作为一个完整的工程发布到JBoss中,结果就造成了以上的异常结果。所以要将EJB组件、web组件分别打包,具体打包过程,请参考博客:

http://suhuanzheng7784877.iteye.com/blog/919899

下面就是笔者的ear文件内容:
JavaEE5实战笔记01EJB环境搭建及异常解决_第5张图片META-INF下的application.xml内容如下

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="5"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd">
	<display-name>WebBankApp</display-name>
	<module id="myeclipse.1303695851606">
		<web>
			<web-uri>WebBank.war</web-uri>
			<context-root>/WebBank</context-root>
		</web>
	</module>
	<module id="myeclipse.1303695851607">
		<ejb>WebBankEJB.jar</ejb>
	</module>
</application>

你可能感兴趣的:(java,Hibernate,jboss,JSF,ejb)