三 AOP的实现方式---Proxy
Spring默认使用J2SE动态代理来作为AOP的代理,故对于代理接口而言用Proxy就可以执行。
JDKProxyFactory.java
package aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import service.impl.PersonServiceBean;
public class JDKProxyFactory implements InvocationHandler {
private Object targetObject;
public Object creatProxyObject(Object targetObject) {
this.targetObject = targetObject;
//创建代理对象 使用Proxy
return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),
this.targetObject.getClass().getInterfaces(),
this);
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
PersonServiceBean personServiceBean = (PersonServiceBean)this.targetObject;
Object result = null;
if(personServiceBean.getUser()!=null){
result = method.invoke(targetObject, args);
}
else {
System.out.println("你无权进行此操作");
}
return result;
}
}
四 AOP的实现方式---CGLib
如果是一个业务对象没有实现接口,在这种情况下,我们实现的解决方案------使用CGLib来代理。
CGlibProxyFactory.java
package aop;
import java.lang.reflect.Method;
import service.impl.PersonServiceBean;
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 targetObject; //代理的目标对象
public Object createProxyObjext(Object targeObject) {
this.targetObject = targeObject;
Enhancer enhancer = new Enhancer(); //创建代理对象
enhancer.setSuperclass(this.targetObject.getClass());//设置此类的父类 非final修饰符的所有方法
enhancer.setCallback(this);//设置回调函数,用对象本身。必须实现接口MehtodInterceptor
return enhancer.create(); //返回创建的代理对象
}
/**
* 执行了上面的回调,接着执行回调函数interceptor.
* proxy:代理对象本身。method:被拦截的方法。
* args: 方法的输入参数。methodProxy:方法的代理对对象。
*/
public Object intercept(Object proxy, Method method, Object[] args,
MethodProxy methodProxy) throws Throwable {
// TODO Auto-generated method stub
PersonServiceBean personServiceBean = (PersonServiceBean) this.targetObject;
Object result = null;
if (personServiceBean.getUser() != null) {
result = methodProxy.invoke(targetObject, args);//委派给代理对象targetObject
}
else{
System.out.println(" 你无权进行此操作!!!");
}
return result;
}
}
单元测试类
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import service.impl.PersonServiceBean;
import aop.CGlibProxyFactory;
public class JunitProxyText {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
// @Test
// public void jdkproxyTest(){
// JDKProxyFactory factory = new JDKProxyFactory();
// PersonService personService = (PersonService)factory.creatProxyObject(new PersonServiceBean());
// personService.save("111");
// }
@Test
public void cglibproxyTest(){
CGlibProxyFactory factory = new CGlibProxyFactory();
PersonServiceBean personServiceBean = (PersonServiceBean)factory.createProxyObjext(new PersonServiceBean("xxx"));
personServiceBean.save("222");
}
}
---------------------------------------------------------------------------------------------------------------------------
转载http://zywang.iteye.com/blog/974226
说明:上述Java文件创建好后,需要将其在Spring的容器中进行声明,可以在配置文件中定义<bean/>节点,也可以使用@Component组件进行修饰
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; 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.springframework.stereotype.Component; /** * 基于注解的AOP日志示例 * @author ZYWANG 2011-3-24 */ @Component @Aspect public class AopLog { //方法执行前调用 @Before("execution (* com.zywang.services.impl.*.*(..))") public void before() { System.out.println("before"); } //方法执行后调用 @After("execution (* com.zywang.services.impl.*.*(..))") public void after() { System.out.println("after"); } //方法执行的前后调用 @Around("execution (* com.zywang.services.impl.*.*(..))") public Object around(ProceedingJoinPoint point) throws Throwable{ System.out.println("begin around"); Object object = point.proceed(); System.out.println("end around"); return object; } //方法运行出现异常时调用 @AfterThrowing(pointcut = "execution (* com.zywang.services.impl.*.*(..))",throwing = "ex") public void afterThrowing(Exception ex){ System.out.println("afterThrowing"); System.out.println(ex); } }
上面这段代码中多次使用了重复的切入点,这种情况下,可以使用@Pointcut标注,来修改一个切入点方法(这个方法不需要参数和方法体),然后就可以在@Before等标注中引用该方法作为切入点,示例如下:
import org.aspectj.lang.ProceedingJoinPoint; 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.stereotype.Component; /** * 基于注解的AOP日志示例 * @author ZYWANG 2011-3-24 */ @Component @Aspect public class AopLog { @Pointcut("execution (* com.iflysse.school.services.impl.*.*(..))") public void pointcut(){} //方法执行前调用 @Before("pointcut()") public void before() { System.out.println("before"); } //方法执行的前后调用 @Around("pointcut()") public Object around(ProceedingJoinPoint point) throws Throwable{ System.out.println("begin around"); Object object = point.proceed(); System.out.println("end around"); return object; } }
Java文件
import org.aspectj.lang.ProceedingJoinPoint; /** * 基于配置文件的AOP日志示例 * @author ZYWANG 2011-3-24 */ public class AopLog { //方法执行的前后调用 public Object runOnAround(ProceedingJoinPoint point) throws Throwable{ System.out.println("begin around"); Object object = point.proceed(); System.out.println("end around"); return object; } }
Spring配置文件
<bean id="aopLog" class="com.iflysse.school.aop.AopLog"></bean> <aop:config> <aop:aspect ref="aopLog"> <aop:around method="runOnAround" pointcut="execution (* com.zywang.services.impl.*.*(..))"/> </aop:aspect> </aop:config>
注意:上面这个示例使用的是around方式的拦截,该方法要求Java类中的方法有一个ProceedingJoinPoint类型的参数
使用第二种方式的AOP配置,在Eclipse(有SpringIDE插件)中被拦截到的方法中有标识显示
以上配置基于Spring 3.0.5 进行设置,参考其《Reference Documentation》