springAOP (注解方式)

package com.zf.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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;

import com.zf.dao.PersonDao;

@Component
@Aspect
public class DaoAspect {
	
	@Pointcut("execution(* com.zf.dao.PersonDao.*(..))")
	public void daoPointCut(){};
	
	@Before("daoPointCut()")
	public void beforeDao(JoinPoint joinPoint){
		PersonDao pd = (PersonDao)joinPoint.getTarget();
		pd.setMessage("helloWorld");
		System.out.println();
		System.out.println("beforeDao...");
	}
	
	@AfterReturning("daoPointCut()")
	public void afterDao(JoinPoint joinPoint){
		System.out.println(joinPoint.getTarget() instanceof PersonDao);
		System.out.println("afaterDao...");
	}
	
	@Around("daoPointCut()")
	public Object daoAround(ProceedingJoinPoint joinPoint) {  
        Object[] args = joinPoint.getArgs();  
        System.out.println("argsLength: " + args.length);
        for (Object object : args) {		//得到目标对象
			System.out.println(object + "  -------------------------");
		}
        Object obj = null;  
        try {  		
            obj = joinPoint.proceed(args);
        } catch (Throwable e) {  
            e.printStackTrace();  
        }  
        return obj;  
    }  

}
  java进阶   http://www.javady.com/index.php/category/hign_xingneng

你可能感兴趣的:(spring)