1、jdk动态代理 实现
核心:Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.target.getClass().getInterfaces(), this);
package org.sh.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import org.sh.HellowImpl; import org.sh.HellowInf; public class JDKProxyFactory implements InvocationHandler { private Object target = null; public Object getProxyBean(Object target){ this.target = target; return Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.target.getClass().getInterfaces(), this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("i am proxy"); Object result = null; if(((HellowInf)this.target).getName() != null){ System.out.println("i am not null"); try{ //前置 result = method.invoke(this.target, args); //注意 this.target 而不是proxy //后置 }catch(Exception e){ //例外 }finally{ //最终 } } return null; } public static void main(String[] args) { Object obj = new JDKProxyFactory().getProxyBean(new HellowImpl("wch")); HellowInf hellow = (HellowInf) obj; hellow.sayHellow(); } }
2、cglib 代理(导入相应的包,spring会包含 cglib-nodep.jar)
核心:Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(target.getClass()); //父类
enhancer.setCallback(this); //回调
return enhancer.create();
package org.sh.proxy; import java.lang.reflect.Method; import org.sh.HellowImpl; import org.sh.HellowInf; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; public class CGLIBProxyFactory implements MethodInterceptor{ private Object target; public Object createProxyBean(Object target){ this.target = target; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(target.getClass()); //父类 enhancer.setCallback(this); //回调 return enhancer.create(); } @Override public Object intercept(Object target, Method method, Object[] arg2, MethodProxy methodProxy) throws Throwable { HellowInf hellow = (HellowInf) this.target; if (hellow.getName()!= null) { System.out.println("cglib"); return methodProxy.invoke(this.target, arg2); } return null; } public static void main(String[] args) { Object obj = new CGLIBProxyFactory().createProxyBean(new HellowImpl("wch")); HellowInf hellow = (HellowInf) obj; hellow.sayHellow(); } }
3、aspect 注解方式
package org.sh.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @Aspect public class Myinterceptor { @Pointcut("execution(* org.sh.aop.PersonBean.*(..))") public void anyMethod(){ } @Before("anyMethod() && args(param)") public void before(String param){ //只有一个String类型的参数的方法才会被拦截 System.out.println("before"); } @AfterReturning(pointcut="anyMethod()",returning="result") public void afterReturn(String result){ //返回stirng类型的result才会被拦截 } @AfterThrowing(pointcut="anyMethod()",throwing="e") public void afterThrow(Exception e){ //e就是方法抛出的异常 } @After("anyMethod()") public void after(){ //最终通知 } @Around("anyMethod()") public Object around(ProceedingJoinPoint pjp){ try { //之前 return pjp.proceed(); //执行方法 //之后 } catch (Throwable e) { e.printStackTrace(); } return null; } public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); ((PersonBean)ac.getBean("personbean")).save(); } }
4、配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/> <!-- 打开注解方式 --> <bean id="myinter" class="org.sh.aop.Myinterceptor"></bean> <bean id="personbean" class="org.sh.aop.PersonBean"></bean> <aop:config> <aop:aspect id="myaspect" ref="myinter"> <aop:pointcut expression="execution(* *(..))" id="pointcut"/> <aop:before method="before" arg-names="p" pointcut-ref="pointcut"/> <aop:after method=""/> <aop:after-returning method=""/> </aop:aspect> </aop:config> </beans>