Ejb+annotation realize AOP_EJB

Source: Internet
Author: User
Tags aop stub system log jboss

We introduce JBoss AOP because our projects are used, the project is EJB, we need to use AOP to insert a layer to record the system log, and later may have to insert a layer of cache, and security control aspects.
Project-driven learning because our application server chooses JBoss and naturally uses JBoss's own more mature framework, JBoss AOP
Jboss AOP is a aspected-core framework that can be seamlessly integrated into any programming environment or with our application servers. Aspect-oriented programming (AOP) makes it easier for us to modularize our code base, and many times it is easy to resolve object-oriented (OO) problems that are not easy to solve. Jboss AOP uses JDK 1.5 annotations instead of generating Java code with separate annotation code.

Language is always weak, let the demo to state the truth.

List the main classes, other code click here to download.
@Stateless public class Usermanageimpl implements Usermanage {@AroundInvoke//class Internal Interceptor 1 public Object MYINTERC Eptor1 (Invocationcontext ic) throws Exception {System.out.println ("Myinterceptor-----------1-------------  
        : "+ Ic.getmethod (). GetName ());      
    return Ic.proceed ();  
        @AroundInvoke//Class Internal Interceptor 2 public Object MyInterceptor2 (Invocationcontext ic) throws Exception {  
        System.out.println ("Myinterceptor-----------2-------------:" + Ic.getmethod (). GetName ()); 
    return Ic.proceed ();  @Override public string SayHello (string userName) {//TODO auto-generated method stub return
    "hello!@" +username; @Override public String greet (user user) {//TODO auto-generated Method stub return "greet!@" +
    User.getname ();
        @Override public User GetUser () {//TODO auto-generated method stub user User=new user (); User.setname ("Marjorie");
    return user; }
    
}


The stateless session Bean defines two interceptor methods and three common bean methods, namely MyInterceptor1 and MyInterceptor2. When the client invokes the Sayhello,greet,getuser method, the EJB container invokes the MyInterceptor1, and then the MyInterceptor2 method is invoked, and the Bean method is finally invoked. There are several points to note when using the Interceptor method:

1. The Interceptor method must have a return value, and the return value type is object.

2. The Interceptor method can have only one parameter, and the parameter type must be Javax.interceptor.InvocationContext.

3. The EJB container will call the next interceptor method or the blocked Bean method only if the proceed method of the Invocationcontext interface is invoked.
Class internal interceptor, not recommended, Plug and pull inconvenient, and put into the business logic class, always feel so nondescript.
The following is an external interceptor class that can intercept methods in different beans, which puts AOP at the acme of the idea. In this case, the Interceptor method needs to be placed in a separate class. This class is called the Interceptor class. The following is the code for an interceptor class:

Interceptor 1 public class UserInterceptor1 {@AroundInvoke public Object interceptormethod (Invocationcontext ic) throws
        Exception {System.out.println ("Userinterceptor-----------1-------------:" + Ic.getmethod (). GetName ());
    return Ic.proceed (); }//Interceptor 2 public class UserInterceptor2 {@AroundInvoke public Object interceptormethod (invocationcontext ic) th
        Rows Exception {System.out.println ("Userinterceptor-----------2-------------:" + Ic.getmethod (). GetName ());
    return Ic.proceed (); }} @Stateless @Interceptors ({userinterceptor1.class, userinterceptor2.class})//Multiple external Interceptor classes//@Interceptors (
    Userinterceptor1.class)//An external interceptor public class Usermanageimpl implements Usermanage {@AroundInvoke//class internal Interceptor 1 Public Object MyInterceptor1 (Invocationcontext ic) throws Exception {System.out.println ("Myintercepto  
        R-----------1-------------: "+ Ic.getmethod (). GetName ());      
    return Ic.proceed ();  

  }  @AroundInvoke//Class Internal Interceptor 2 public Object MyInterceptor2 (Invocationcontext ic) throws Exception {System  
        . OUT.PRINTLN ("Myinterceptor-----------2-------------:" + Ic.getmethod (). GetName ()); 
    return Ic.proceed (); 
        @ExcludeClassInterceptors//Block interceptor methods in interceptor classes blocking the Bean method @Override public string SayHello (string userName) {
    TODO auto-generated method stubs return "hello!@" +username; @Override public String greet (user user) {//TODO auto-generated a stub return "greet!@" +u
    Ser.getname ();
        @Override public User GetUser () {//TODO auto-generated method stub user User=new user ();
        User.setname ("Marjorie");
    return user;
 }
    
}

From the above class, you can see that @Interceptors ({userinterceptor1.class, userinterceptor2.class})//Multiple external interceptor classes, this annotation completes the interceptor insertion. @ExcludeClassInterceptors//No blocking method, that is, the annotation blocks the interceptor method from intercepting the Bean method in the Interceptor class
If more than one interceptor class and Interceptor method is specified, the problem of a call order is involved. The EJB container invokes the Interceptor method in the Interceptor class first, and calls in the specified order if multiple interceptor classes are specified. Press Usermanageimpl's annotation, the Interceptor method in the UserInterceptor1 class, and then the UserInterceptor2. Finally, the interceptor methods defined in the bean are invoked MyInterceptor1 and MyInterceptor2.
The code is very simple, add a few annotations on the OK. Other code click here to download. If need more detailed content, go to JBoss official Internet café, http://www.jboss.org/jbossaop/
It has to be said that the people who can write the AOP framework are cattle, and those who can extract AOP ideas are more likely to be. From the code, it is clear that AOP separates the application logic from the system architecture code cleanly and is easily plugged in. We can easily insert caching, asynchronous communications, transactions, security, and many many functional modules.
This also provides a great convenience for us to cooperate in development, there are two types of developers, one is responsible for the development of the application business logic, and the other is responsible for the development of the system environment, such as logs, permissions, caching and so on.



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.