Another implementation of AOP ---- cglib

Source: Internet
Author: User

As we all know, dynamic proxy can implement AOP, but it has a disadvantage that all objects to be proxies must implement an interface. Otherwise, an exception is reported. If no interface is implemented by the proxy object, how can we implement AOP? Of course, it can be used.

1. What is

 

Is a good choice.

                   System.out.println("create() is running..."                System.out.println("delete() is running..."                System.out.println("update() is running..."                System.out.println("query() is running..."  }

 

AuthProxy. userName = MethodProxy arg3 )(! "Michael" System. out. println ("You have no permission! "}

               TableDao tDao =                                Enhancer en =  Enhancer();                    en.setSuperclass(TableDao.                               }

TableDao tDao = TableDAOFactory. getAuthInstance (AuthProxy ("Zhang San" TableDao tDao = TableDAOFactory. getAuthInstance (AuthProxy ("Li Si "}

AuthProxyFilter. java:

          AuthProxyFilter                                     (!"query"              0;                                 1   }

As to why 0 or 1 is returned, the comments are very detailed.

Add the following method in:

           Enhancer en =          en.setSuperclass(TableDao.         en.setCallbacks( Callback[]{authProxy,NoOp.INSTANCE});           en.setCallbackFilter(              }  

Note that en. the array parameter sequence in the setCallbacks () method is the method interceptor represented by the return value of the preceding method. If return 0 is used, authProxy interceptor is used, and return 1 is used NoOp. INSTANCE interceptor, NoOp. the INSTANCE is the default method interceptor and does not process it.

The following method is added to the test class:

TableDao tDao = TableDAOFactory. getAuthInstanceByFilter (AuthProxy ("Zhang San" tDao = TableDAOFactory. getAuthInstanceByFilter (AuthProxy ("Li Si "}

Call this method in the main method. The program running result is as follows:

Create () is running...
Query () is running...
Update () is running...
Delete () is running...
You do not have permission!
Query () is running...
You do not have permission!
You do not have permission!

In this way, all users have access to the query method, while other methods only allow access by Michael Jacob.

References: http://llying.iteye.com/blog/220452

 

  

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.