码迷,mamicode.com
首页 > 编程语言 > 详细

集成JPA+springmvc+spring+EJB中的Java EE应用

时间:2014-11-30 21:34:36      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

       EJB是sun的JavaEE服务器端组件模型,设计目标与核心应用是部署分布式应用程序。凭借java跨平台的优势,用EJB技术部署的分布式系统可以不限于特定的平台。EJB (Enterprise JavaBean)是J2EE(javaEE)的一部分,定义了一个用于开发基于组件的企业多重应用程序的标准。其特点包括网络服务支持和核心开发工具(SDK)。 在J2EE里,Enterprise Java Beans(EJB)称为Java 企业Bean,是Java的核心代码,分别是会话Bean(Session Bean),实体Bean(Entity Bean)和消息驱动Bean(MessageDriven Bean)。


具体选型为EJB+springMVC+spring+JPA稍微技术选型和大家分享


     Coding the Entity Beans: JPA



        实体是轻量级持久域对象,通常表示在关系数据库中的表。一个POJO成为一个实体bean的主要要求是该类必须与javax.persistence.Entity注解注释。表示一个主键属性或字段,使用javax.persistence.Id注解,告诉EJB容器这是EJB的实体bean


<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">package Jc.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;



@Entity
@Table(name="lisheheceshi_Student")
public class Student implements Serializable {
	private static final long serialVersionUID = -4969613879943565834L;
	@Id
	@GeneratedValue(generator="hibernateuuidgenerator")
	@GenericGenerator(name="hibernateuuidgenerator",strategy="uuid")
	@Column(name="studentId",length=32)
	private String studentId;
	@Column(name="studentCode",length=32)
	private String studentCode;
	public String getStudentCode() {
		return studentCode;
	}
	public void setStudentCode(String studentCode) {
		this.studentCode = studentCode;
	}
	public String getStudentId() {
		return studentId;
	}
	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
}
</span></span></span>

Coding the Business Logic: EJB

<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">package mgr.student.service.impl;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import jc.eao.IStudentEao;
import Base.Service.impl.BaseServiceImpl;
import Base.eao.IBaseEao;
import Jc.entity.Student;
import Jc.mgr.student.service.IStudentBean;
@Stateless
@Remote(IStudentBean.class)
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class StudentBeanimpl extends BaseServiceImpl<Student> implements IStudentBean {
	@EJB(beanName="BaseEao")
	private IBaseEao BaseEao;
	public boolean test(Student student) {
	
		System.out.print("李社河是好人调到ejb了");
		BaseEao.save(student);
		System.out.print("李社河是好人调到Eao了");
		return false;
	}
	
//	@Override
//	public IBaseEao<Student> getEao() {
//		return this.studentEao;
//	}
//	
//	public void save()
	
//	@EJB(beanName="studentEao")
//	private IStudentEao studentEao;
//	public boolean test(Student student) {
//	
//		System.out.print("李社河是好人调到ejb了");
//		studentEao.save(student);
//		System.out.print("李社河是好人调到Eao了");
//		return false;
//	}
//	public IStudentEao getStudentEao() {
//		return studentEao;
//	}
//	public void setStudentEao(IStudentEao studentEao) {
//		this.studentEao = studentEao;
//	}
////	@Override
////	public IBaseEao<Student> getEao() {
////		return this.studentEao;
////	}
////	
////	public void save()


	
}
</span></span></span>

Coding the Controller: Controller

<span style="font-size:18px;"><span style="font-size:18px;">package jc.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import Jc.entity.Student;
import Jc.mgr.student.service.IStudentBean;
@Controller
public class TestController {
	private IStudentBean studentbean;
	private Student student;
@RequestMapping("/add")
	public String add(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("ssss");
		Student student =new Student();
		student.setStudentId("001");
		student.setStudentCode("lishehe");
	
	studentbean.test(student);
		return "/list.jsp";
	}
	public Student getStudent() {
	return student;
}
public void setStudent(Student student) {
	this.student = student;
}
	public IStudentBean getStudentbean() {
		return studentbean;
	}
	public void setStudentbean(IStudentBean studentbean) {
		this.studentbean = studentbean;
	}
	
	
}
</span></span>

配置数据库链接xml

<span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    
	<persistence-unit name="Jc-entity" transaction-type="JTA">
<!-- 		<provider>net.bull.javamelody.JpaPersistence</provider> -->
		<provider>org.hibernate.ejb.HibernatePersistence</provider>

<!--   	 <jta-data-source>java:/MySqlDS</jta-data-source> -->
<!--    		 <jta-data-source>java:/JcOracleDS</jta-data-source>   -->
   		<jta-data-source>java:jboss/datasources/JcOracleDS</jta-data-source> 
   		<class>Jc.entity.Student</class>
  		<properties>
  		  	 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> 
<!--   		  	<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/> -->
			<property name="hibernate.hbm2ddl.auto" value="update" />
			<property name="hibernate.show_sql" value="true" />
			
			<!-- 指定二级缓存产品的提供商 --> 
<!-- 			<property name="hibernate.cache.provider_class"  -->
<!-- 					value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/> -->
<!-- 			<property name="hibernate.cache.region.factory_class"  -->
<!-- 				value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/> --> 
<!-- 			 开启二级缓存 --> 
<!-- 			<property name="hibernate.cache.use_second_level_cache" value="true"/> -->
<!-- 			 开启查询缓存 --> 
<!-- 			<property name="hibernate.cache.use_query_cache" value="true"/>   -->
<!-- 			 指定缓存配置文件位置   --> 
<!-- 			<property name="hibernate.cache.provider_configuration_file_resource_path"  -->
<!-- 				value="/ehcache.xml"/> -->
  		</properties>
	</persistence-unit>
  
</persistence>
</span></span>


Spring的applicationContext-common配置

<span style="font-size:18px;"><span style="font-size:18px;"><context:component-scan base-package="jc.action"/></span></span>

spring-mvc.xml

,通过jndi查找到EJB的Bean,之后通过spring容器把StudentBean注入到Controller.调用

<span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

<!-- 	 <bean id="test" class="Jc.mgr.student.service.impl.StudentBeanimpl"> -->
<!-- 	</bean> -->
	<jee:local-slsb id="StudentBean"
		jndi-name="global/lishehe_core/StudentBeanimpl!Jc.mgr.student.service.IStudentBean"
		business-interface="Jc.mgr.student.service.IStudentBean" />
	<bean name="/testController/**" class="jc.action.TestController">
		<property name="studentbean" ref="StudentBean"></property>
	
	</bean>




	<!-- 注解探测器 -->
	<!-- <context:component-scan base-package="com.sfpay"/> -->

	<!-- annotation默认的方法映射适配器 -->
	<!-- <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" 
		/> -->
<!---->
	<!-- <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" 
		/> -->
</beans>
</span></span>

web.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app></span>


运行后打出EJB的信息


<span style="font-size:18px;">0:36:42,255 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-8) JNDI bindings for session bean named StudentBeanimpl in deployment unit deployment "lishehe_core.jar" are as follows:

	java:global/lishehe_core/StudentBeanimpl!Jc.mgr.student.service.IStudentBean
	java:app/lishehe_core/StudentBeanimpl!Jc.mgr.student.service.IStudentBean
	java:module/StudentBeanimpl!Jc.mgr.student.service.IStudentBean
	java:jboss/exported/lishehe_core/StudentBeanimpl!Jc.mgr.student.service.IStudentBean
	java:global/lishehe_core/StudentBeanimpl
	java:app/lishehe_core/StudentBeanimpl
	java:module/StudentBeanimpl

20:36:42,257 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-8) JNDI bindings for session bean named BaseEao in deployment unit deployment "lishehe_core.jar" are as follows:

	java:global/lishehe_core/BaseEao!Base.eao.IBaseEao
	java:app/lishehe_core/BaseEao!Base.eao.IBaseEao
	java:module/BaseEao!Base.eao.IBaseEao
	java:jboss/exported/lishehe_core/BaseEao!Base.eao.IBaseEao
	java:global/lishehe_core/BaseEao
	java:app/lishehe_core/BaseEao
	java:module/BaseEao

20:36:42,257 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-8) JNDI bindings for session bean named studentEao in deployment unit deployment "lishehe_core.jar" are as follows:

	java:global/lishehe_core/studentEao!jc.eao.IStudentEao
	java:app/lishehe_core/studentEao!jc.eao.IStudentEao
	java:module/studentEao!jc.eao.IStudentEao
	java:jboss/exported/lishehe_core/studentEao!jc.eao.IStudentEao
	java:global/lishehe_core/studentEao
	java:app/lishehe_core/studentEao
	java:module/studentEao</span>

页面程序访问到的结果提示:


<span style="font-size:18px;">20:37:14,027 INFO  [stdout] (http-localhost/127.0.0.1:8080-1) 李社河是好人调到ejb了BaseEaoImpl中的save方法开始调用</span>

总结EJB的优点

         

1:EJB提供性能优化支持

         最主要的是性能问题,由于以前国内中文Java网站有些人弯曲EJB,认为EJB性能低,其实这是一种非常肤浅错误的认识,我们首先看看在一般Java环境中是如何提高性能。

  假定一个JavaBeans为A,那么一般使用这个JavaBeans命令如下:

  A a = new A();

  但是,在高访问量的环境中,new A()其实是很费时消耗系统性能的,因此,能不能在软件系统启动时候就预先建立一些对象,这样,系统运行时,从这些已经生成的对象池中借用一个,这样,就无需在使用时进行New,节约了开销,提高了性能,因此,真正成熟性能解决方案都是需要对象池等支持。

  在一个纯Web结构的系统(也就是只能运行在Tomat环境中),例如Struts + Hibernate等这样的系统,除非自己动手做,一般是没有对象池技术支持的,因此他们的性能只能算是Demo演示版本的性能,根本无法承受大容量并发访问,也无法称为一个成熟的系统,所以,我们研究成熟的开源Web系统,如Jive、OFBize,LifeRay等,他们都在Web层拥有自己的对象池和缓存池。

  对象池和缓存机制是J2EE必须的吗?当然,是所有成熟系统必须的,Windows系统如果去掉缓存将会变得怎样?

  自己动手开发对象池和缓存机制并不是一件简单的事情,需要对多线程以及同步锁等底层原理有深层次的把握,这其实也是一门非常深入的Java研究分支,所以,你可以抛开你的客户焦急的催促,精心研究开发自己的对象池和缓存池。

  但是,EJB容器(如JBoss)已经提供了对象池和缓存机制,所以,没有事务机制的无状态Session Bean的性能肯定要强于普通JavaBeans。EJB容器不但在单机中提供了对象池和缓存,而且可以跨服务器实现动态负载平衡,这些都无需开发者自己开发任何软件代码,结构如下:


bubuko.com,布布扣


2:EJB组件能提供真正的可重用框架

   每一个jar包代表一个EJB组件,一个系统可以由多个可重用的EJB组件构成,例如:树形结构EJB组件;自增序号EJB组件;用户资料EJB组件等,这样的EJB组件可以象积木一样搭配在大部分应用系统中,提高了系统的开发效率,保证了开发质量。


3:EJB提供了事务机制

        EJB提供的事务机制非常周全,但事务机制带来的缺点是性能的降低,因此,有些人认为EJB很重,因为在实际应用中,有的用户系统可能不需要事务机制,只是需要EJB提供的性能优化机制,这样,如果使用EJB,就象叫一个人来背东西,他除了背着我要的东西外,还背着我不要的东西。

  除非你是一个完美主义,在一般企业应用或数据库系统应用中,EJB不会对你构成很重的包袱。


集成JPA+springmvc+spring+EJB中的Java EE应用

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://blog.csdn.net/lishehe/article/details/41622175

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!