EJB3 in action 学习笔记之一

来源:互联网 发布:数据交换技术有哪几种 编辑:程序博客网 时间:2024/04/28 08:49
 
 
1.Three primary techniques in EJB 3 eliminate these sources of complexity: metadata annotations, minimal deployment descriptors, and dependency injection.
 
2.The primary goal of dependency injection (DI) is to make component
interdependencies as loosely coupled as possible.
3. Unlike stateless session beans, stateful session beans guarantee that a client can expect to set the internal state of a bean and count on the state being maintained between any number of method calls.
4. The @Remove annotation marks the end of the workflow modeled by a stateful bean. In our case, we are telling the container that there is no
longer a need to maintain the bean’s session with the client after the
confirmOrder method is invoked.
5. If your application needs robust support for accessing remote components or the ability to seamlessly expose your business logic as web services, EJB 3 is the clear choice.
 
The anatomy of a session bean
6. Each session bean implementation has two distinct parts—one or more bean interfaces and a bean implementation class.
 
7. The following summarizes the rules that apply to all types of session beans:
■ As we discussed earlier in section 3.1.2, a session bean must have at least one business interface.(session bean至少有一个业务接口。)
■ The session bean class must be concrete. You cannot define a session bean class as either final or abstract since the container needs to manipulate it. (session bean 类必须是具体的类,不能将其定义为finalabstract类型,因为容器需要处理该类。)
■ You must have a no-argument constructor in the bean class. As we saw, this is because the container invokes this constructor to create a bean instance when a client invokes an EJB. Note that the compiler inserts a default no argument constructor if there is no constructor in a Java class.(session bean类中必须定义一个无参数的构造函数。)
■ A session bean class can subclass another session bean or any other POJO.(session bean能继承其它session beanPOJO)
■ The business methods and lifecycle callback methods may be defined
either in the bean class or in a superclass. It’s worth mentioning here that
annotation inheritance is supported with several limitations with EJB 3 session beans. (业务方法和生命周期回调方法既可被定义在本类中,又可被定义在其超类中。)
■ Business method names must not start with “ejb.” For example, avoid a
method name like ejbCreate or ejbAddBid because it may interfere with
EJB infrastructure processing. You must define all business methods as
public, but not final or static. If you are exposing a method in a remote
business interface of the EJB, then make sure that the arguments and the
return type of the method implement the java.io.Serializable interface.
(session bean 类中的业务方法名不能以“ejb”字符开头。)
 

8. As noted earlier, stateless session beans model tasks don’t maintain conversational state. This means that session beans model tasks can be completed in a single method call, such as placing a bid. However, this does not mean that all stateless session beans contain a single method.

9. Three types of business interfaces correspond to the different access types:

Local interface

A local interface is designed for clients of stateless session beans collocated in the same container (JVM) instance.

Remote interface

Clients residing outside the EJB container’s JVM instance must use some kind of remote interface. Remote business interfaces do have one special requirement: all parameters and return types of interface methods must be Serializable. This is because only Serializable objects can be sent across the network using RMI.

 

Web service endpoint interface

The third type of interface is specific to stateless session beans that you haven’t seen yet: the web service endpoint interface (also known as SEI). The ability to expose a stateless session bean as a SOAP-based web service is one of the most powerful features of EJB 3.

10. Stateful session beans are guaranteed to maintain conversational state.

11. Since stateful session beans cannot be pooled and reused like stateless

beans, there is a real danger of accumulating too many of them if we don’t have a way to destroy them. Therefore, we have to define a business method for removing the bean instance by the client using the @Remove annotation.

(由于有状态session bean不能像无状态session bean放入对象池而重复使用,所以如果我们不能及时销毁,就会累积许多的风险。我们必须使用@Remove标注来定义一个业务方法去移去bean实例。)

12.A stateful session bean cannot have a web service endpoint interface.

This is because SOAP-based web services are inherently stateless in nature.(有状态session bean不能实现 web 服务endpoint接口,这是由于基于SOAPweb 服务本质上是无状态的。)

13

 

14. To outline some of the best practices for session beans:   

n       Choose your bean type carefully. Stateless session beans will be suitable most of the time.(认真选择session的类型,在大多时候无状态session bean是最适合的。)

n       Carefully examine interface types for session beans. Remote interfaces involve network access and may slow down your applications.(仔细检查session bean的接口类型。如果是远程接口必须访问网络,这样会减慢系统的访问速度。)

n       If you are using DI, make sure you don’t inject a stateful session bean into a stateless session bean or servlet.(如果你使用依赖注入,确保不要将有状态session bean注入到无状态session beanservlet中。)

n       Separate crosscutting concerns such as logging and auditing using business interceptors (which we discuss in chapter 5) instead of spreading them all over the business logic.(使用拦截器代替在整个业务逻辑中传递,来分离像日志和审核等之类的横关切点)

n       Closely examine what kind of data you are storing in the conversation state. Try to use small, primitive instance variables in a stateful bean whenever possible as opposed to large nested composite objects.

n       Don’t forget to define remove methods in a stateful session bean.

n       Tune passivation and timeout configurations to find the optimal values for your application.(调整纯化和超时配置,以录求系统最佳性能。)

原创粉丝点击