Spring Enterprise JavaBeans (EJB) integration

Source: Internet
Author: User

The original address: need to turn over the wall

https://translate.google.com/translate?hl=zh-CN&sl=zh-CN&tl=zh-CN&u=http%3A%2F%2Fdocs.spring.io% 2fspring-framework%2fdocs%2fcurrent%2fspring-framework-reference%2fhtml%2fejb.html&sandbox=1


22.1 Introduction

As a lightweight container, spring is considered an alternative to EJB. We do believe that, without too much application and usage, spring as a container, combined with its rich support capabilities in things, ORM and data access, is a better choice than using EJB and EJB containers to do the same thing.


However, it is important to use spring instead of using EJBS. In fact, spring makes it easier to access EJBS and implement EJBS and internal functionality. In addition, using spring to access the business provided by EJBS allows the implementation classes of those services to be easily converted between local ejbs, remote EJBS, or Pojo variables without changing the client's code.


In this chapter, you'll see how spring can help you access and implement EJBS. Spring provides dedicated value to access stateless session beans (SLSBS).


22.2 Visit EJBs


22.2.1 concept


To invoke a local or remote stateless session bean method, the client code must typically perform a jndi lookup to get the main project of a local or remote EJB, and later invoke the Create method on that object to get a true remote or local EJB object. Here the EJB invokes one or more methods.


To avoid duplication of low-level code, many EJB applications employ service Locator (server locator) and business delegate mode. These are much better than executing JNDI queries through client-side code, but there are obvious flaws in their general implementation. For example:

    • The general use of EJBS relies on service positioning or business delegate Singleton, making it difficult to test.
    • In the case where service positioning is used instead of the business delegate, the application code still ends with invoking the EJB's Create () method and handles the resulting exception. It still retains a complex EJB programming pattern that is coupled to the EJB API and retains
    • Implementing the business delegate model generally leads to obvious code duplication, where we have to write a number of methods that simply invoke the same method as the EJB.

Spring's approach is to allow proxy objects to be created and used, typically within the spring container's internal configuration, and can be a code-free business delegate. You don't need to write another service locator, another Jndi lookup, or a repeating method of a hand-coding business delegate, unless you really need to add the actual value in such code.


22.2.2 Accessing local Slsbs

Suppose we have a Web controller that needs to use a local EJB. We will follow best practices and use the EJB business method interface pattern, so that the EJB's local interface inherits a non-ejb-specified business method interface. Call this business method interface as MyComponent.


<span style= "FONT-SIZE:14PX;" >public interface MyComponent {...  } </span>

One of the main reasons for using the business method interface pattern is to ensure that the asynchronous between the local interface and the Bean implementation class is atomic. Another reason is that later we convert a pojo into a business to implement more containers if the application is concerned about this. Of course we also need to implement the local home interface and provide an implementation class to implement the Sessionbean and MyComponent business method interfaces. Now we need to do the Java coding work (connecting the Web layer controller to the EJB implementation Class) is a setter method that exposes the controller mycomponent type. This saves a reference, which is within the controller as an instance variable.



<span style= "FONT-SIZE:14PX;" >    private mycomponent mycomponent;            public void Setmycomponent (MyComponent mycomponent) {          this.mycomponent = mycomponent;      }  </span>

We can then use this instance variable in any of the business methods of this controller. Now suppose we get our controller object from the outside of a spring container, in the same context we can configure an Localstatelesssessionproxyfactorybean instance, which is an EJB proxy object. The configuration of this agent, and the setting of the MyComponent property of this controller, is handled in a configuration entry.
<span style= "FONT-SIZE:14PX;" >    <bean id= "mycomponent"              class= " Org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean ">          <property name=" Jndiname "value = "Ejb/mybean"/>          <property name= "Businessinterface" value= "com.mycom.MyComponent"/>      </bean >            <bean id= "Mycontroller" class= "Com.mycom.myController" >          <property name= "MyComponent" ref= " MyComponent "/>      </bean>  </span>

With the spring AOP framework, there are a lot of things going on behind this scenario, although you can not force the use of AOP concepts to enjoy the results you want. The MyComponent bean definition creates the proxy for the EJB, which implements the business method interface. The EJB local home is cached at startup, so there is only a single jndi query. Each time the EJB is invoked, the agent invokes the ClassName method of the local EJB and invokes the corresponding business method of the EJB.


The Mycontroller bean defines the Controller class MyComponent attribute set for the EJB proxy.


In addition, if there are many such proxy definitions, consider using in spring's "Jee" namespace<jee:local-slsb>配置元素。

<span style= "FONT-SIZE:14PX;" >    <jee:local-slsb id= "MyComponent" jndi-name= "Ejb/mybean"              business-interface= " Com.mycom.MyComponent "/>            <bean id=" Mycontroller "class=" Com.mycom.myController ">          <property Name= "MyComponent" ref= "MyComponent"/>      </bean>  </span>

The EJB access mechanism simplifies application code: the Web Layer code (other EJB clients) does not rely on the use of EJBS. If we want to use a pojo, or a dummy object or some other test stub instead of the EJB reference, we simply need to change the mycomponent bean definition without changing a single line of Java code. In addition, we do not have to write a single-line Jndi query or other EJB vertical code as part of our application.



Benchmarking programs and experiences in real-world applications indicate that the execution overhead of this approach (involving reflection calls from the target EJB) is minimal and imperceptible in general usage. Remember, we don't want to make any fine-grained calls to EJBS, because overhead is added to the Application server's association with the EJB's underlying.


There is a warning in the Jndi query that in a bean container, this class is generally used as a singleton (there is no reason to set it to prototype). However, if the bean is pre-instantiated as a singleton (as if it were a different XMLapplicationcontext variable), you might encounter a problem if the bean container before the EJB container loads the target EJB. This is because the Jndi query will be executed in the Init () method of this class and cached later, but the EJB is not bound to the target location. Although this solution does not instantiate the factory object in advance, it is allowed to be created at the first use. In this XML container, it is controlled by the Lazy-init property.


While this is not the most interesting place for spring users, those who use EJBS programmatic AOP programming may want to look at Localslsbinvokerinterceptor.


Spring Enterprise JavaBeans (EJB) integration

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: info-contact@alibabacloud.com 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.