Take AOP first step - use Java dynamic proxy to implement AOP [ZZ]

xiaoxiao2021-03-06  107

At present, the entire development community is advised to receive AOP (Aspect Oriented Program), and a large number of excellent Framework - Spring, JAC, JBoss AOP, etc. that support AOP is also emerged. AOP seems to become a trend. Java beginners can't help but make a feeling, OOP has not learned, come to AOP. This article is not to specifically explain what AOP is theory. Why do IP. To learn more about learning AOP can go to it http://aosd.net/. Here is just a simple example to show the beginners to make AOP. For the sake of simplicity, there is no use of any third-party AOP Framework, but use the dynamic agent function of the Java language itself to implement AOP. Let Let's go back to the AOP itself, AOP is mainly used in logging, performance statistics, security control, transaction processing, etc. Its main intentions will be clearly divided into logging, performance statistics, security control, etc., and we can put these behaviors a problem to be solved by the system, which is the so-called problematic. Programming (I don't know if AOP is translated as a problem-oriented programming). Through the separation of these behaviors, we hope to be independently configured to business methods, and to changing these behaviors do not need to affect business method code. Suppose the system is completed by a series of BusinessObject, and the system requires logging when performing a logic processing. Here we go to the specific business logic code. public interface BusinessInterface {public void processBusiness ();} public class BusinessObject implements BusinessInterface {private Logger logger = Logger.getLogger (this.getClass () getName ().); public void processBusiness () {try {logger.info ( "start TO processing ... "); // Business logic here. System.out.println (" here is business logic "); logger.info (" End Processing ... ");} catch (Exception E) {Logger. INFO ("Exception Happends ..."); // Exception Handling}} The code for processing business logic is mixed with logging code here, which gives a certain difficulties to future maintenance, and also cause a lot of code. repeat. The exact same LOG code will appear in every businessObject of the system. According to AOP thinking, we should separate the logging code. To separate these code, it involves a problem, we must know when the business logic code is called so that we will insert the log record code. Generally, intercepted a method, we can use a callback method or a dynamic agent. Dynamic agents generally have to be more flexible, and most of the AOP Framework is mostly implemented by dynamic agents. Here we also use dynamic agents as an example.

JDK1.2 provides a dynamic agent support, programmers provide an execution processor by implementing the java.lant.reflect.InvocationHandler interface, then get a proxy object through java.lang.reflect.proxy, through this proxy object to execute business Method, while the business method is called, the execution processor will be automatically called. With this support for JDK, we have to do just provide a log processor. public class LogHandler implements InvocationHandler {private Logger logger = Logger.getLogger (this.getClass () getName ().); private Object delegate; public LogHandler (Object delegate) {this.delegate = delegate;} public Object invoke (Object proxy, Method method, Object [] args) throws throwable {object o = null; try {logger.info ("Method Stats ..." method; o = method.invoke (delegate, args); logger.info ("Method Ends ... " method);} catch (Exception E) {logger.info (" Exception Happends ... "); // EXCETPION HANDLING.} Return O;} Now we can process all logs in BusinessObject All the code is removed.

public class BusinessObject implements BusinessInterface {private Logger logger = Logger.getLogger (this.getClass () getName ().); public void processBusiness () {// business processing System.out.println ( "here is business logic");} } client calls the business method of the following code, businessInterface businessImp = new BusinessObject (); InvocationHandler handler = new LogHandler (businessImp);. businessInterface proxy = (businessInterface) Proxy.newProxyInstance (businessImp.getClass () getClassLoader (), businessImp.getClass () .GetInterfaces (), handler); proxy.processbusiness (); program output is as follows: Inf method stats ... Here is Business Logicinf Method Ends ... The first small attempt is completed. It can be seen that after using AOP, logging and business logic code are completely separated. If you want to change the logging, you only need to modify the logging processor, and business object itself (BusinessObject) does not need to make any modifications. And this log record does not cause duplicate code, all commercial processing objects can reuse this log processor. Of course, in practical applications, this example is too rough. Since JDK's dynamic agent does not directly support multiple InvocationHandler, then we need to do some to do it if you have a logging method to perform statistics. Generally we can define a Handler interface, then maintain a queue to store all Handler, when the InvocationHandler is triggered, we call your own handler. Fortunately, almost all AOP Framework has provided very good support in this regard. ZZ: UNKNOWN Recommended Site:

http://aosd.net/

http://www.neward.net/ted/weblog/index.jsp?date=20030107

转载请注明原文地址:https://www.9cbs.com/read-104088.html

New Post(0)