spring随笔(二) AOP(三)

2014-11-24 02:22:06 · 作者: · 浏览: 9
y2() throws Exception { Customer customer = new Customer(); customer.setName("张珊"); ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml"); ICustomeService service = ctx.getBean("customerService",ICustomeService.class); service.save(customer); }

打印结果:

your Transcation...logic progream...Before....
CustomerServiceImpl......save...张珊
your Transcation...logic progream...After....

3,spring使用@AspectJ注解添加AOP功能。

AspectJ是一门专门用于Java AOP编程的Java扩展语言。在Spring中,可以通过使用@AspectJ注释,来添加AOP功能。 要使用@AspectJ注释编程,首先要在Spring的配置文件中引入aop命名空间:并加入 标签 aspectj-autoproxy/>
   

   
    xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
        
    

   

使用@AspectJ注解来功能来获取代理首先需要定义一个切面:(定义一个类,使用@Aspect声明) 如下切面类TransCationService:
package com.xiaohui.aop;

import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class TransCationService {

	@Pointcut("execution(* com.xiaohui.aop.*ServiceImpl.*(..))")
	public void pointcut() {
	}

	@Before("pointcut()")
	public void beginTranscation(JoinPoint jp) {
		System.out.println("目标类:" + jp.getTarget().getClass().getName());
		System.out.println("参数: " + Arrays.toString(jp.getArgs()));
		System.out.println("方法: " + jp.getSignature().getName());
		System.out.println("session.brgintranscation()........");
	}

	@AfterReturning("pointcut()")
	public void commit() {
		System.out.println("session.getTranscation().commit()");
	}

	@AfterThrowing(pointcut = ("execution(* com.xiaohui.aop.*ServiceImpl.*(..))"), throwing = "e")
	public void rollback(Throwable e) {
		System.out.println("出现异常......" + e.getMessage());
		System.out.println("session.rollback....");
	}

	@After("pointcut()")
	public void closeSession() {
		System.out.println("session.close()");
	}
}
上面的
@Pointcut("execution(* com.xiaohui.aop.*ServiceImpl.*(..))")
	public void pointcut(){}
其定义了一个切入点表达式:其中第一个‘*’ 表示返回值为任何类型,第二个‘*’表示在包com.xiaohui.aop下面的以ServiceImpl结尾的类,第三个‘*’表示所有方法()里的.. 表示任意参数类表。,最终的意思为,在com.xiaohui.aop包下面返回值为任何类型,且类名以ServiceImpl结尾的参数类表不限所有方法。 @Before("pointcut()"):前置通知,在目标代理对象执行方法之前调用。直接引用了上面已经定义好的切入点,使用方法名。 @AfterReturning("pointcut()"):后置通知,在目标代理对象成功执行方法之后调用。 @After("pointcut()"):最终通知,在目标代理对象执行方法之后,无论目标对象方法成功调用与否,都会执行。 @AfterThrowing(pointcut=("execution(* com.xiaohui.aop.*ServiceImpl.*(..))"),throwing="e"):异常通知:在一个方法抛出异常后执行。throwing,表示抛出的异常。其切入点为自己重新定义的切入点表达式。 applicationContext.xml配置如下:
   

   
	
	
    
	
    
	
    

   

ICustomerService接口和测试类和2.3中的代码一致,为了测试有异常抛出的情况,我们对CustomerServiceImpl类的save方法进行一点点修改。save方法如下:
public void save(Customer customer) {
		System.out.println("CustomerS