EJB Session – EJB advanced concepts


EJB insider

Behind the EJB: container for each bean instance will automatically generate a proxy called EJB object, implemented by the container, transparent to the developer

EJB context: in the ideal circumstances, should not be used EJB context object in the programming; But in reality, inevitably there is a requirement, so there is the existence of EJBContext; Corresponding to the session bean specific subclass SessionContext, corresponding to the exclusive subclass of MDB MessageDrivenContext ; Can be injected through @Resource, when injected, the container will be based on the current type of bean automatically injected into the specific subclass


Dependency injection and JNDI access resources

@Resource:the most comprehensive annotation for EJB3's dependy managerment; parameter: name: the value of the container will be further resolved in the form of java: conp / env / {name} fully qualified JNDI name;

In the deployment of the container in the deployment process will EJB components, services, parsed as resources, and the resources bound to the ENC (environment naming context); use setter (setter) injection, to facilitate unit testing, easy to initialize

@Resource practical application:
Inject the JMS resource
B. Inject EJBContext
C. Access the environment entries in the deployment descriptor file
D. Inject JavaMail
E. Injection timer service

@Resouce Inheritance: If the superclass uses the @Resouce annotation to define any resource, they can be inherited by subclasses

Find resources and EJBs: Use search (that is, use APIs) rather than dependency injection Allows you to dynamically determine which resources to use at runtime


AOP in EJB: interceptor

AOP Overview: Facing facets

What is the interceptor: the interceptor is the EJB version of the AOP, there is no real AOP powerful, but the use of simple; interceptor only one form of "surround call notification." At the beginning of the method and method of return is triggered, can be used for business methods And lifecycle callback methods that can be used for session beans and message-driven beans

Specified interceptors:
The @Interceptions annotation allows a method or class to call one or more interceptor classes; when annotated using the interceptor at the class level, its lifecycle triggers the lifecycle callback interceptor method in the interceptor, how can the business method be invoked Triggering business method interceptors; in addition to methods and class-level interceptors, there are default interceptors, can only be set in the deployment descriptor file
B. Interceptor call order: from the large scope to the small scope of the implementation, the first is the default interceptor to the class interceptor to the method interceptor
C. Call order in multiple interceptors at the same level: Follow the order in the annotation
D. You can use @ExcludeDefaultInerceptors to disable the default level interceptor; you can use @ExcludeClassInerceptors to disable class-level interceptors

Interceptor implementation:
@AroundInvoke annotation is triggered by the surround call method, an interceptor class can only have one by the annotation annotation method
The method signature of the calling method must follow the Object <method name> (InvocationContext invocationContext) throws Exception
InvocationContext interface: you can dynamically check the status of the intercepted bean and its dynamic changes to parameters such as the operation


Leave a Reply