First, in the foreword, I emphasize the grievances of EJB, Hibernate, and Spring, but also to explain, I have always been ink you how to invade, you say how I rely on EJB VS Hibernate, EJB VS Spring discussion, because this behavior It is meaningless, wrong. I advocate benefit from the correct technical comparison and rational technology. Comparison, we need to find a comparison point; criticize, we need to analyze, criticize from Source, Spec, Application Context.
Second, start from EJB
2.1 EJB Several Bean Types of Introduction
EJB1.0, there are two kinds of bean types: SessionBean, EntityBean.
EJB2.0, introducing CMP EntityBean, introducing Message-Driven Bean, introducing a Local interface.
2.2 Entity Bean and O / R Mapping Subtle Relationship
I want to make a brief description of O / R mapping, o / r mapping engine.
O / R mapping, look at DB Record in Object View, and object operations can be mapped into DB Record operations.
O / R mapping engine is to make O / R mapping a possible specific implementation technique.
From our definition, use BMP EntityBean means yourself in implementing a very simple O / R mapping, yourself in order to make efforts to interact with object views and DB. In order to support the CMP EntityBean, the EJB Server Providers will provide you with O / R mapping capabilities. Moreover, the fact is true that any EJB Server that supports CMP EntityBean needs to provide a Persistence (O / R mapping) Engine, such as JBoss Jaws (Just Another Web Store). As for Hibernate, Ibatis, etc., although it is also called O / R Mapping Tool, but their significance has far exceeded the TOOL such as CMP EntityBean O / R Mapping Engine. There will be a detailed analysis below.
2.3 EJB-1.0 EJB1.0 is a distributed component architecture, including SessionBean and EntityBean. It introduces a lot of very avant-garde technology, concept. It mainly includes a distributed component, a container, and a DB operation.
EJB1.0, the application scenario of EJB envisaged in Expert Group is a large-scale distributed system. So, sessionbean, EntityBean has not introduced a Local interface. Client-> sessionBean, Client-> EntityBean, sessionbean-> sessionbean, sessionbean-> EntityBean, etc. is Remote.
EJB1.0, introduce this concept of containers into EJB, which is significant. Here I want to clarify a question by the way: what is the container? My point of view: The container is just a concept, an architecture. Take EJB Server, Server tries to provide BEAN to provide distributed, transaction, security, etc., then there must be a Layer or WARP that is over Bean, so that it can be intercepted from the high-level BEAN call, and some additional operating. Such architecture is called a container architecture, which is of course not from EJB. As for how it is achieved, the method is different.
In fact, based on individual perspectives, the core of the container architecture is not in the "master" Beans (Objects) from the top, but what technology is used to implement the interception of Bean (Object). Undoubtedly, EJB Servers and Spring implementation techniques are different. This issue will be discussed in detail below. EJB1.0 provides an object attempt to DB operation. How does EXPERT GROUP positioning EntityBean? Undoubtedly, the EntityBean in 1.0 is 2.0 BMP EntityBean, the positioning is domain object (I don't know if there is this concept at that time, just their thoughts are very consistent). Its Fields direct mapping DB Table Schema, Member Functions is the operation of Table Record. Client-> EntityBean, sessionBean-> EntityBean, etc. can be interacted directly with the database.
Some people say that EJB1.0 is based on CatalySIS methodology, SessionBean corresponds to the Domain Object. So far, I have a reservation attitude, because I have no traces of this statement in EJB SPEC.
2.4 EJB-2.X
Undoubtedly, EJB1.x has a major defect. 2.0 Increase features include Local interface, CMP EntityBean, which is a major correction of 1.x defects.
First, in fact, there is no large-scale distributed application in Expert Group imagination. From two aspects: (1) Use EntityBean through the Remote method to cause serious performance issues, it is necessary to provide a Local interface. (2) Visiting sessionbean WebApplication and SessionBean deployment on the same server, so providing sessionBean's Local interface is also an inevitable thing. After 2.x, the most common Pattern is to access the EntityBean in the form of the sessionbean fa. Ade. Moreover, even if the application size is large enough to connect to different Server, it is not a relationship because EJB2.x also supports the Remote interface.
Second, EJB2.0 introduced into CMP EntityBean. CMP EntityBean solves the issue of EntityBean persistence and JDBC separation, and greatly simplifies the development of EntityBean and improves performance. However, I have to say that CMP EntityBean is completely rushed out of the Domain Model concept of EJB1.x because CMP EntityBean cannot contain any Domain Logic. BMP EntityBean seems to be DomainObject that Matrin said, and CMP EntityBean seems to be in the Transaction Script of Martin, which is Martin, the application scenacinoBject.
By the way, I personally disagree with Martin's RichdomainObject said. Because, in the data application system, Martin mentioned RichdomaInModel, which is mentioned in Transacton Script, often does not reflect the real world. A Bank Account is reflected in the real world, which is a record in the account, which has no spontaneous action, such as withdraw. It is different from Person, and Person can have SAY (STRING WORDS) self-motivation. Accent's Withdraw should be placed in AccountManager and manipulate AccountManger. It is not that Object in OO needs to have actions. In the real world, there is still a static, there is no self-motivation, such as a book, an account, a database. Although it is undeniable, Rich Domain Model can bring a lot of benefits (what kind of benefits, you will see the Martin "Domain Logic and SQL", you know). Third, I understand Hibernate
Originally, the topic of this article is called "EJB, Spring: Analysis, Critique and Outlook", because I feel Hibernate and EJB, Spring is not a hierarchy, although this truth is very shallow, but why so many people also take hibernate to attack EJB to attack EntityBean? EntityBean is worth attacking, but you use a mistake.
I mentioned above, EJB IMPLEMENTS supporting CMP EntityBean has a persistence engine, which is O / R mapping engine. What is the CMP O / R mapping engine to do? It generates SQL, then interacts with DB by analyzing CMP Abastract Schema, analyzing EJBQL, analyzing Bean status.
In my eyes, Hibernate is not "O / R Mapping Tool". These words can be summarized. I said that Hibernate is a unique lightweight translation middleware, which is Layer, and CMP EntityBean O / R mapping engine is not a hierarchy.
Application -------> CMP EntityBean Operation --------> DB & Line; (O / R mapping engine) & line; --- HQL, Criteria Query
Application ------> Hibernate ------> & line; --- Pojo / Po Operation ---------> DB & line; --- and so on
Do you see the difference between the two pictures?
EntityBean applications, do not know the existence of O / R mapping engine, just use EntityBean to complete your interaction.
In Hibernate applications, Application is directly using Hibernate. That is, it uses O / R mapping engine directly.
Here, I suggest you stop, thinking that EntityBean should correspond to PO / POJO in Hibernate? For example, after you modify PO, if you need sessionobj.update (po) to update, this sessionobj.Update (po) does indicate you directly using Hibernate's persitation engine? Yes it is. And in EntityBean, after you modify EntityBean, do you need other behaviors to make EntityBean change to DB? No need. Because EJB Container intercepts your call, before you change the bean's field before, Container will call the Load / Store method (of course, in BMP / CMP EntityBean, the situation is different, BMP EntityBean calls Programmer to write with JDBC Load / store, etc., and CMP EntityBean uses CMP Peristence Engine to do this.). In this way, implicit persistence data is. No, you call session.Update as a statement like session.Update like Hibernate. EntityBean This synchronization method is one of the important reasons for its performance. It is worth noting that EJB IMPLEMENTS is not entirely the same as described above, the synchronous frequency and transactions are not entirely, and specific imports are closely related. In general, CMP EntityBean O / R Mapping Engine is developed for static, functionally fixed EntityBean's O / R mapping support. Hibernate is a Layer role.
Fourth, Spring is not myth
Where is RD Johnson smart? Smart, he insists on his own practice, not with great flow. RD Johnson realizes that 90% of the application does not require distributed, no need to technologies in J2EE, such as JNDi, he will do it for EJB to remove Remote this layer, will isolate unnecessary technologies in most applications, transformation . From the scope of application, Spring has complements 90% of EJB.
Personal view: Spring philosophy is that Framework is designed for the most common, simplest application scenarios, etc., when you need special technology, you will find a solution to the problem. In this way, Spring shows advantage in applications where there is no special requirements. Below, we will make a detailed explanation.
4.1 Spring "No Vanity" is a lie, but there is a qualified smile "EJB outside the step"
"No invasive" is the characteristics of the Spring advertisement. However, I want to say that Spring's "no invasion" is a lie, with the deepening of the application, "no invasive" is a deception of what Framework.
What is called "no invasiveness"? Object deployed into spring does not need to force any implementation interface to say Spring is "no invasive"? I feel, this is a big mistake. If you don't have to say, Spring doesn't need to force some interfaces like EJB, then I can only tell you:
(1) The application scenario of the Object of Spring is from the simplest. So, it doesn't have some specific interfaces to implement Object for some features to use in some expectations. However, in fact, in Spring, if your application scenario is slightly inserted, then you are bound to Spring.
(2) Spring management Object, from a sense that it is not status. For the first point, I will give two examples. (1) The call of the EJB internal method can cause infrastructure to do not work. However, the bean interface (sessionbean, messagendrivenbean) can make BEAN's support, such as the SetSessionContext (SessionContext CTX) of SessionBean, etc., the container will set a context to each bean when the container is deployed. In ejbcontext, there is a function like EjBobject getEjbobject, which allows Bean to get its own EJBOBJECT, so that BEBOBJECT calls Bean's own function, the infrastructure will work. And Spring, if, an Object's function needs to call your other functions, and hope as security check, transaction, etc. Aspect works? So how do Spring? You need to set the bean's ExposeProxy property.
ExposeProxy: whether the current proxy should be exposed in a ThreadLocal so that it can be accessed by the target (It's available via the MethodInvocation without the need for a ThreadLocal.) If a target needs to obtain the proxy and exposeProxy is true, the. Target can use the aopcontext.currentproxy () Method.
So, when you need the internal calls that need the internal calls that require infrastructure work, no matter whether it is in EJB or Spring, it is definitely needed to be bound to a specific framework. Why, Spring fifty steps laugh? Because I mentioned above, Spring is easy to deploy in the Object, it is possible to be deployed, multiplexed. Ejb does no matter what you need, you will make you need. Similarly, the BeanFactoryAware, BeanNameaWare, etc. in Spring also explains a point: Spring peels the characteristics from Object, so as to minimize its dependencies. Framework will invade your object when your Object is complicated.
For the second point, I want to talk about it. Why say, from a sense that the object departure in Spring is not status? We know, Spring supports two Object: Singleton and Prototype. Spring Spec, think that Singleton can be called STATELESS, Prototype can be called StateFule. In the world of EJB, StateFulesessionBean and EntityBean are also known as Stateful. So what does their STATEFUL mean? Why is it so big in terms of dependence? Why does Object in Spring does not need to implement a specific interface, while EJB needs? Let's take a look at the EJB SessionBean interface:
Void
ejbActivate () The activate method is called when the instance is activated from its "passive" state. void ejbPassivate () The passivate method is called before the instance enters the "passive" state. void ejbRemove () A container invokes this method before it Ends the life of the session object. VoidsetSessionContext (sessioncontext ctx) set the associated session context.
I said it is said. See ejbactivate (), ejbpassive (), why have these two functions? Spring does not need to implement the same function interface? This is caused by the different object management mechanisms of EJB and Spring. EJB IMPLEMENTS Generally, in order to multiplex beans, the first-class cache will use the first-level instancepool (StatelessSessionBean does not need Cache), so that the StatefulSessionBean persists to disk, support EntityBean Bean Instance (Note this bean instance and Client's EntityBean is a different, its unused multiplexing, which results in an introduction of EJBACRIVATE, EJBPassivate et al. However, Spring did not use this management mechanism, which only Singleton / Prototype. Although Prototype can also be STATEFULE, it will not use Object Instance in different clients, but every client, even 10,000 client, then generate 10,000 instance, and in EJB In, 100 instance can be used to serve, and the Not Active's bean persisted to disk and multiplexed bean instance. Please also note that here I don't mean the StateFulesessionBean in EJB. In fact, I found that when it is very large, it is a problem that the BEAN to disk is used to save memory, I / O bottleneck caused by I / O bottleneck More serious.
Look again, EJBREMOVE, this is not much more, spring you can choose to implement the InitializingBean, DISPOSABEBean interface, but Spring is recommended to do this, write the ordinary init member function, then specify Init-method, Destroy-Method in the configuration file Attributes, this avoids the binding of the Spring framework. In general, Spring starts with the most basic reference scenarios of the object. When complex feature is required, special mechanisms will be used to solve the problem, that is, at this time, the application will be bind to Spring. So, its intrusive is relatively low, but it is not "no invasive", not what you think so, of course, there is no "absolutely non-invasive" Framework.
4.2 I don't have a perfect design idea of Spring IoC
Spring's IOC is considered a magical thing by some people. Does EJB have the ability of the IOC in Spring? The answer is yes. EJB references, resource references, environmental properties in EJB can be said to be IOC, isn't it? However, where is the IOC of Spring and EJB?
Features of Spring Injection: Mainly considering the Local Object search, this time does not require any protocol (such as JNDI), when you need to inject Remote Object, use the RMI protocol or use a third party Tool (such as HESSIAN).
EJB Features: Whether your bean-bean is deployed on the same machine, whether the client-> bean is on the same machine, it is definitely to query bean through JNDI, just, if they are on the same machine, if they are on the same machine, if they are on the same machine You use the local interface so that the call is changed to local calls, thereby promoting performance. EJB It is positioned as a distributed component architecture from the time of birth, and it is not easy to play "distributed". This may be called "the end is big".
All of this can only say that their positioning is different. One more attention to LOCAL, one more attention to Remote. Spring still adheres to its philosophy. From the most basic, most scenes, when they go to special needs, they will find ways to solve the problem. It tried to find a balanced point of J2EE development and system capabilities.
It can be said that Spring is more reasonable. However, I also believe that Spring is a bit overkill over "only support for Remote injection". I feel, it can do better, such as encapsulating Local, Remote lookup as J2EE standard JNDI.
Currently, Spring doesn't care about Remote Object injection. For information on Remote injection, only provide simple support, but also need to write configuration information separately for Expert. Here, the practice of EJB3.0, I feel that it is the most convenient and most reasonable, and the most promising. EJB3.0 You can make EJB Server different deployments through At Remote, @ local.
Spring exports the remote object. Properties PROPS = New Properties (); Pros.Put ("SearchServiceFactory", "ORG.NET.SPRING.Bean.beanfactory"); Context CTX = New Context (ENV); CTX.lookup ("serviceName"); Properties PROPS = New Properties (); Pros.Put ("SearchServiceFactory", "com.sun.j2ee.jndi"); Context CTX = New Context (ENV); CTX.lookup ("serviceName"); From below, you can see that I personally think Spring If you still have OS, Free, there may be many people choose it, but if it tries to commercialize, then it is certain that it must be close to EJB3.0. Close to the standard. 4.3 Independent, complete, faculty-oriented AOP framework is the true highlight of Spring Does ejb have AOP capabilities in Spring? The answer is yes. EJB's infrastructure is implemented using AOP technology. However, I want to say that EJB's AOP does not face programmers. Why do you say that? EJB IMPLEMENTS provides you with a set of infrastructure (for example, those configible interceptors in JBoss). You can configure it according to the system. However, you can't write your own interceptor and configure it into the system. Because EJB Interceptors tends to be tight, you want to write your own interceptor, which you must read EJB IMPLEMENTS Source to understand its implementation. In other words, the AOP technology in EJB IMPLEMENTS is for EJB Server to provide infrastructure, which does not think of providing more AOP capabilities for Programmer. The Spring's AOP begins to develop as a framework, which is independent, complete. This is the cause of history. The EJB IMPLEMENTS author is not God, they are impossible, N years ago, I thought of sufficient independence of the AOP frame design to Programmes. Personal view: EJB3.0 Introduction to infrastructure, basically followed EJB2.x. Just, you can mention EJB3.0 support to use infrastructure through Annotations, not to say, EJB3.0 requires a perfect AOP framework, but I think EJB3.0 Implments should provide a separate, complete, facing Programmer AOP frame. In fact, JBoss didn't have JBO. Of course, using AOP in Spring is not so easy. For example, let you write TransactionProxy, you still need to understand the Spring AOP internal operation mechanism. 4.4 Spring integration to other frameworks This problem doesn't talk. 5. What is EJB going to? 5.1 EJB-3.0 We don't have to say how much the ejb2.x itself is, after all, it is the product of the previous J2EE era, can only say that EJB2.x can no longer reflect the actual needs of most J2EE applications. out-of-date. So where is EJB3.0 plan to take us? EJB3.0 SPEC has made a lot of modifications in addition to simplified development, convenient testing, convenient deployment, etc., more importantly, EJB3.0 has a comprehensive intensive surgery for SessionBean, especially the ENTITYBEAN model. This modification is revolutionary. In my "If I want to improve the EJB2.x model", I talk about, if I make me modify the EJB2.x's EntityBean model, then first need to set up a new model. Take EntityBean. First Road: Continue EntityBean Design First Try: Remote Domain Model (including the ANAEmicDomainObject represented by the Domain Model and CMP EntityBean represented by BMP EntityBean, and retains the local interface, trying to change the design of persistence model, improve performance (even CMP EntityBean) The performance is also difficult to accept, this situation, I personally think that it is mainly because the EntityBean model is not good, in my other "If I want to improve the EJB2.x model", I have an in-depth analysis), Enhancements (EJBQL is too weak), let those connected to SessionBean, EntityBean need to deploy applications on different Server to EJB2.x's EntityBean. However, obviously, the EJB Server provider is impossible to be willing to be willing to be willing, because the application is too small. The facts have proven that if the transitybean's Remote is not required, the RemoteEntityBean performance is not feasible, it can only work in the back end of the sessionBean, however, even if the EntityBean works in the back end of the sessionBean, there is too much limitations. The granularity is either too thin, performance, function is too weak, and so on, the development data is very flat, then if the Remote EntityBean is not a must, I don't completely give up EntityBean, use other in the back end of the sessionBean O / R Mapping Tool to develop data applications, such as Hibernate. This is, EntityBean can take the second road. Of course, in a sense, it is also the way it must go. The second road: completely abandon ENTITYBEAN, using O / R mapping engine such as Hibernate as a backend data persistence tool for session beans, Message-Driven Beans. From EJB3.0, EJB3.0 does completely abandon the traditional EntityBean model. Personal: You can say this, EntityBean has no longer existed, and Expert Group gives you a very sharp's persistence engine in SessionBean. You hold the engine, what you want to do (above, EntityBean, PersitencyEngine is clear to the client, which is determined by the essential effect of these two engines. Some people say that Dynamic Query can be used in EntityBean Application, which can only declare EJBQL in the configuration file, these are the nature of both Persistence Engine. Determined). The EntityBean of the mandatory model does not exist! In addition, the EntityBean Remote feature is not mentioned in EJB3.0, perhaps only as an optional feature (I haven't thought that in EJB3.0, how to support Remote Po, this problem is very strange). It seems that Expert Group has completely denies the design of EntityBean, or EntityBean does not meet the actual needs, REMOTE EntityBean, Remote Domain Object is unrealistic in most cases. Words: Hibernate and JDO relationship, very subtle. EJB3.0 and JDO merger, Gavin enters EJB3.0 ExpertGroup is confusing. The persistence model of EJB3.0 uses JDO, which should be taken. However, at present, the Persitance Engine part of EJB3.0 seems to be around Hibernate, then where should JDO's location? Sixth, how will Spring going? Undoubtedly, "sounds very wonderful" IOC, strength, practical Spring AOP, integrated large number of Framework Spring is the most reasonable choice for systems that are not strong in distributed and advanced J2EE characteristics. However, you can see that Spring can do, in addition to integrated a large number of Framework features (of course this will never be written into EJB SPEC, but if EJB Server supplies do this, it is also very simple), EJB3 .0 can also do, and many places do a lot than Spring, the most important, EJB is standard, so, if Spring Os, Free, maintain the current gesture development, will still become a developer good The choice, however, if Spring tries to commercialize, I am RD Johnson, I will close to EJB3.0 and become an EJB3.0 Server provider. Seven, EJB3.0 is the future of J2EE commercial Framework The new EJB3.0 is the future of J2EE commercial Framework.