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