Implement AOP with Java dynamic proxy

xiaoxiao2021-03-06  87

At present, the entire development community is highly supported by AOP (Aspect Oriented Program), and a large number of excellent Framework, - Spring, Jac, JBoss AOP, and more, which support AOP. 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, you can go to its 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");} } The code for the client calls the business method is as follows:

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:

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

New Post(0)