Java EE applications in integrated JPA+SPRINGMVC+SPRING+EJB

Source: Internet
Author: User
Tags jboss

EJB is the Sun's Java EE server-side component model, and the design goal and core application is to deploy distributed applications. With the advantages of Java cross-platform, distributed systems deployed with EJB technology can be limited to specific platforms. EJB (Enterprise JavaBean) is part of the Java EE, which defines a standard for developing component-based enterprise multi-application applications. Features include network service support and core development tools (SDKs). In Java EE, Enterprise Java Beans (EJB) is called Java Business Bean, which is the core code of the kernel, namely the session bean, the entity bean Bean) and the message-driven bean (Messagedriven bean).

Specific selection for EJB+SPRINGMVC+SPRING+JPA a little technology selection and we share

Coding the Entity beans:jpa

entities are lightweight persistent domain objects that are typically represented in tables in a relational database. The primary requirement for a pojo to become an entity bean is that the class must be annotated with the javax.persistence.Entity annotation. Represents a primary key property or field, using Javax.persistence.Id annotations, to tell the EJB container that this is an EJB entity bean

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >package jc.entity;import;import Javax.persistence.column;import javax.persistence.Entity; Import Javax.persistence.generatedvalue;import;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 ("Li Shi is a good person tuned to EJB"); (student); System.out.print ("Li Shi is a good person to 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 ("Li Shi is a good person tuned to EJB");// (Student),//system.out.print ("Li Shi is the good man transferred to 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; @Controllerpublic 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> 

metabase link XML

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;"    ><?xml version= "1.0" encoding= "UTF-8"? ><persistence xmlns= "Http://" Xmlns:xsi= "Http://" xsi:schemalocation= " Persistence "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 "/&Gt <!--<property name= "Hibernate.dialect" value= "Org.hibernate.dialect.Oracle10gDialect"/>-->< Property Name= "" value= "Update"/><property name= "Hibernate.show_sql" value= "true"/> <!--Specify a tier two cache offering--<!--<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 "/ > <!--turn on level two cache-<!--<property name= "Hibernate.cache.use_second_level_cache" value= "true"/> --><!--Open Query Cache-<!--<property name= "Hibernate.cache.use_query_cache" value= "true"/>--><! --Specify the cache profile location-<!--<property name= "Hibernate.cache.provider_configuration_file_resource_path"- <!--value= "/ehcache.xml"/>--</properties></persistence-unit> </persistencE></span></span> 

Spring's Applicationcontext-common configuration

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><context:component-scan base-package= "Jc.action"/></span></span>


, lookup the bean to the EJB through Jndi, and then inject the Studentbean into the controller through the spring container. Call

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://" xmlns:context= "Http://" xmlns:xsi= " Xmlschema-instance "xmlns:jee=" Http:// "xsi:schemalocation="/HTTP/  :// "><!-- <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 "/><beanName= "/testcontroller/**" class= "Jc.action.TestController" ><property name= "Studentbean" ref= "Studentbean" ></property></bean><!--Note Detector--><!--<context:component-scan base-package= "Com.sfpay"/ >--><!--Annotation Default method mapping adapter--><!--<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://     "Xmlns:xsi=" "xsi:schemalocation=" Http:// Http:// "> <context-param> <param-name>contextconfig Location</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </cont Ext-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</l        istener-class> </listener> <servlet> <servlet-name>action</servlet-name>            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> &LT;PARAM-VALUE&GT;/WEB-INF/SPRING-MVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servle t> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>/&lt ;/url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome- File> </welcome-file-list></web-app></span>

Post-run EJB information

<span style= "FONT-SIZE:18PX;" >0:36:42,255 INFO [] (MSC service Thread 1-8) JNDI bindings for session beans named Studentbeanimpl in Deployment Unit Deployment "Lishehe_core.jar" is as F ollows:java:global/lishehe_core/studentbeanimpl! jc.mgr.student.service.istudentbeanjava:app/lishehe_core/studentbeanimpl! jc.mgr.student.service.istudentbeanjava:module/studentbeanimpl! jc.mgr.student.service.istudentbeanjava:jboss/exported/lishehe_core/studentbeanimpl! Jc.mgr.student.service.istudentbeanjava:global/lishehe_core/studentbeanimpljava:app/lishehe_core/ studentbeanimpljava:module/studentbeanimpl20:36:42,257 INFO [] (MSC service thread 1-8) JNDI Bindings for session beans named Baseeao in Deployment Unit Deployment "Lishehe_core.jar" is as Follows:java:global/lisheh E_core/baseeao! Base.eao.ibaseeaojava:app/lishehe_core/baseeao! Base.eao.Ibaseeaojava:module/baseeao! Base.eao.ibaseeaojava:jboss/exported/lishehe_core/baseeao! base.eao.ibaseeaojava:global/lishehe_core/baseeaojava:app/lishehe_core/baseeaojava:module/baseeao20:36:42,257 INFO [] (MSC service thread 1-8) JNDI Bindings for session beans named Studenteao in Deployment Unit Deployment "Lishehe_core.jar" is as Follows:java:global/lis hehe_core/studenteao!jc.eao.istudenteaojava:app/lishehe_core/studenteao!jc.eao.istudenteaojava:module/ Studenteao!jc.eao.istudenteaojava:jboss/exported/lishehe_core/studenteao!jc.eao.istudenteaojava:global/lishehe _core/studenteaojava:app/lishehe_core/studenteaojava:module/studenteao</span>

Page Program Access to the results of the prompt:

<span style= "FONT-SIZE:18PX;" >20:37:14,027 INFO  [stdout] (http-localhost/ Li Shi is the good guy tuned into the EJB Baseeaoimpl the Save method starts calling </span >

Summarize the advantages of EJBS

1:EJB provides performance optimization supportThe main is the performance problem, because the previous Chinese Java Web site Some people bent EJB, think EJB performance is low, in fact, this is a very superficial error, we first look at how to improve performance in the general Java environment.

Assuming a javabeans is a, then the general use of this JavaBeans command is as follows:

A = new A ();

However, in a high-traffic environment, new A () is very time consuming system performance, so it is possible to pre-establish some objects when the software system starts, so that when the system runs, it borrows one from the pool of objects that have been generated, so that there is no need to do new at the time of use, saving overhead, Improves performance, so a truly mature performance solution requires support for object pooling.

In a purely web-structured system (that is, it can only run in a tomat environment), such as Struts + hibernate, and so on, unless you do it yourself, generally there is no object pool technical support, so their performance can only be considered demo version of the performance, There is no way to tolerate high-volume concurrent access or a mature system, so we study mature open source web systems such as Jive, Ofbize,liferay, and so on, all of which have their own object pools and cache pools at the Web layer.

is the object pooling and caching mechanism necessary for Java EE? Of course, it is necessary for all mature systems, what if the Windows system gets rid of the cache?

Self-developed object pool and caching mechanism is not a simple thing, need to multi-threaded and synchronous lock and other underlying principles have a deep grasp, which is actually a very deep Java Research Branch, so you can put aside your customers anxious urge, carefully research and development of their own object pool and cache pool.

However, the EJB container (such as JBoss) already provides the object pool and caching mechanism, so the stateless session bean without transaction mechanism must have better performance than ordinary JavaBeans. The EJB container not only provides object pooling and caching in a single machine, but also enables dynamic load balancing across servers, without the developer having to develop any software code, which is structured as follows:

2:EJB components provide a truly reusable framework

Each jar package represents an EJB component, and a system can consist of multiple reusable EJB components, such as a tree-structured EJB component, a self-increment EJB component, a user data EJB component, and so on, which can be used as a building block in most application systems, improving the system's development efficiency. Ensure the development quality.

3:EJB provides a transaction mechanism

The transaction mechanism provided by the EJB is very thorough, but the disadvantage of the transaction mechanism is that the performance is reduced, therefore, some people think that the EJB is very heavy, because in the actual application, some user system may not need the transaction mechanism, only needs the EJB to provide the performance optimization mechanism, so, if uses the EJB, is like calls a person to carry things Besides carrying what I wanted, he was carrying what I didn't want.

Unless you are a perfectionist, EJB does not pose a heavy burden to you in general enterprise applications or database system applications.

Java EE applications in integrated JPA+SPRINGMVC+SPRING+EJB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.