摘要:Spring AOP ,从代理机制看AOP,动态代理的范例
		
		
				
						 
				
		
		
				在JDK1.3之后加入了可协助开发动态代理功能的API,你不必为特定对象与方法编写特定的代理对象,使用动态代理,可以使用一个处理者(Handler)服务于各个对象。
		
		
				
						 
				
		
		
				
						●     
				
				
						LogHandler.java
				
		
		
				
						 
				
		
		
				package com.kela.spring.aop;
		
		
				
						 
				
		
		
				import java.lang.reflect.InvocationHandler;
		
		
				import java.lang.reflect.Method;
		
		
				import java.lang.reflect.Proxy;
		
		
				
						 
				
		
		
				import org.apache.log4j.Logger;
		
		
				
						 
				
		
		
				public class LogHandler implements InvocationHandler {
		
		
				
						 
				
		
		
				
						    private Logger log = Logger.getLogger(this.getClass().getName());
		
		
				
						    
						
						
				
		
		
				
						    private Object delegate;
		
		
				
						    
						
						
				
		
		
				
						    public Object bind(Object delegate) {
		
		
				
						        this.delegate = delegate;
		
		
				
						        return Proxy.newProxyInstance(delegate.getClass().getClassLoader(), delegate.getClass().getInterfaces(), this);
		
		
				
						    }
		
		
				
						    public Object invoke(Object arg0, Method method, Object[] args)
		
		
				
						            throws Throwable {
		
		
				
						        Object result = null;
		
		
				
						        
						
						
				
		
		
				
						        try {
		
		
				
						            log.info("hello 
				方法开始执行... ...");
		
		
				
						            
						
						
				
		
		
				
						            result = method.invoke(delegate, args);
		
		
				
						            
						
						
				
		
		
				
						            log.info("hello 
				方法执行完毕");
		
		
				
						        } catch (Exception e) {
		
		
				
						            System.out.println("[ERROR]" + e.getMessage());
		
		
				
						        }
		
		
				
						        return result;
		
		
				
						    }
		
		
				
						 
				
		
		
				}
		
		
				
						●     
				
				
						IHell.java
				
		
		
				
						 
				
		
		
				package com.kela.spring.aop;
		
		
				
						 
				
		
		
				public interface IHello {
		
		
				
						    public void hello(String name);
		
		
				}
		
		
				
						●     
				
				
						HelloSpeaker.java
				
		
		
				
						 
				
		
		
				package com.kela.spring.aop;
		
		
				
						 
				
		
		
				public class HelloSpeaker implements IHello {
		
		
				
						 
				
		
		
				
						    public void hello(String name) {
		
		
				
						        System.out.println("
				你好," + name);
		
		
				
						    }
		
		
				}
		
		
				
						●     
				
				
						ProxyDemo.java
				
		
		
				
						 
				
		
		
				package com.kela.spring.aop;
		
		
				
						 
				
		
		
				public class ProxyDemo {
		
		
				
						    
						
						
				
		
		
				
						    public void method_2() {
		
		
				
						        LogHandler logHandler = new LogHandler();
		
		
				
						        
						
						
				
		
		
				
						        IHello helloProxy = (IHello)logHandler.bind(new HelloSpeaker());
		
		
				
						        
						
						
				
		
		
				
						        helloProxy.hello("kela");
		
		
				
						    }
		
		
				
						 
				
		
		
				
						    public static void main(String[] args) {
		
		
				
						        ProxyDemo proxyDemo = new ProxyDemo();
		
		
				
						        
						
						
				
		
		
				
						        proxyDemo.method_2();
		
		
				
						    }
		
		
				}
		
		
				
						●     
				
				
						学习小结
				
		
		
				
						 
				
		
		
				使用代理对象将记录等于业务逻辑无关的动作或任务提取出来,设计为一个服务对象,如LogHandler和上一小节中的HelloProxy,这样的对象称之为切面(Aspect)。