Spring AOP续

三 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

第一种配置方法:使用@AspectJ标签

  1. 在配置文件中添加<aop:aspectj-autoproxy/>注解
  2. 创建一个Java文件,使用@Aspect注解修饰该类
  3. 创建一个方法,使用@Before、@After、@Around等进行修饰,在注解中写上切入点的表达式

说明:上述Java文件创建好后,需要将其在Spring的容器中进行声明,可以在配置文件中定义<bean/>节点,也可以使用@Component组件进行修饰

示例:

Java代码 复制代码  收藏代码
  1. import org.aspectj.lang.ProceedingJoinPoint;   
  2. import org.aspectj.lang.annotation.After;   
  3. import org.aspectj.lang.annotation.AfterThrowing;   
  4. import org.aspectj.lang.annotation.Around;   
  5. import org.aspectj.lang.annotation.Aspect;   
  6. import org.aspectj.lang.annotation.Before;   
  7. import org.springframework.stereotype.Component;   
  8.   
  9. /**  
  10.  * 基于注解的AOP日志示例  
  11.  * @author ZYWANG 2011-3-24  
  12.  */  
  13. @Component  
  14. @Aspect  
  15. public class AopLog {   
  16.        
  17.     //方法执行前调用   
  18.     @Before("execution (* com.zywang.services.impl.*.*(..))")   
  19.     public void before() {   
  20.         System.out.println("before");   
  21.     }   
  22.        
  23.     //方法执行后调用   
  24.     @After("execution (* com.zywang.services.impl.*.*(..))")   
  25.     public void after() {   
  26.         System.out.println("after");   
  27.     }   
  28.        
  29.     //方法执行的前后调用   
  30.     @Around("execution (* com.zywang.services.impl.*.*(..))")   
  31.     public Object around(ProceedingJoinPoint point) throws Throwable{   
  32.         System.out.println("begin around");   
  33.         Object object = point.proceed();   
  34.         System.out.println("end around");   
  35.         return object;   
  36.     }   
  37.        
  38.     //方法运行出现异常时调用   
  39.     @AfterThrowing(pointcut = "execution (* com.zywang.services.impl.*.*(..))",throwing = "ex")   
  40.     public void afterThrowing(Exception ex){   
  41.         System.out.println("afterThrowing");   
  42.         System.out.println(ex);   
  43.     }   
  44. }  
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等标注中引用该方法作为切入点,示例如下:

Java代码 复制代码  收藏代码
  1. import org.aspectj.lang.ProceedingJoinPoint;   
  2. import org.aspectj.lang.annotation.Around;   
  3. import org.aspectj.lang.annotation.Aspect;   
  4. import org.aspectj.lang.annotation.Before;   
  5. import org.aspectj.lang.annotation.Pointcut;   
  6. import org.springframework.stereotype.Component;   
  7.   
  8. /**  
  9.  * 基于注解的AOP日志示例  
  10.  * @author ZYWANG 2011-3-24  
  11.  */  
  12. @Component  
  13. @Aspect  
  14. public class AopLog {   
  15.        
  16.     @Pointcut("execution (* com.iflysse.school.services.impl.*.*(..))")   
  17.     public void pointcut(){}   
  18.        
  19.     //方法执行前调用   
  20.     @Before("pointcut()")   
  21.     public void before() {   
  22.         System.out.println("before");   
  23.     }   
  24.        
  25.     //方法执行的前后调用   
  26.     @Around("pointcut()")   
  27.     public Object around(ProceedingJoinPoint point) throws Throwable{   
  28.         System.out.println("begin around");   
  29.         Object object = point.proceed();   
  30.         System.out.println("end around");   
  31.         return object;   
  32.     }   
  33. }  
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;
	}
}

 

 

 

 

 

第二种配置方法:基于配置文件的配置

  1. 创建一个Java文件,并指定一个用于执行拦截的方法,该方法可以有0个或多个参数
  2. 在Spring配置文件中注册该Java类为一个Bean
  3. 使用<aop:config/>、<aop:aspect/>等标签进行配置

示例:

Java文件

Java代码 复制代码  收藏代码
  1. import org.aspectj.lang.ProceedingJoinPoint;   
  2.   
  3. /**  
  4.  * 基于配置文件的AOP日志示例  
  5.  * @author ZYWANG 2011-3-24  
  6.  */  
  7. public class AopLog {   
  8.        
  9.     //方法执行的前后调用   
  10.     public Object runOnAround(ProceedingJoinPoint point) throws Throwable{   
  11.         System.out.println("begin around");   
  12.         Object object = point.proceed();   
  13.         System.out.println("end around");   
  14.         return object;   
  15.     }   
  16.        
  17. }  
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配置文件

Xml代码 复制代码  收藏代码
  1. <bean id="aopLog" class="com.iflysse.school.aop.AopLog"></bean>  
  2.   
  3. <aop:config>  
  4.     <aop:aspect ref="aopLog">  
  5.         <aop:around method="runOnAround" pointcut="execution (* com.zywang.services.impl.*.*(..))"/>  
  6.     </aop:aspect>  
  7. </aop:config>  
	<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》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值