SpringAOP 注解方式代码示例

 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;  
    }  
 
}
package xxx.design.pattern.proxy.balance;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.nico.noson.Noson;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Aspect
@Component
public class XXXServiceInterceptor {

    /**
     * 日志打印器
     */
    private Logger logger = Logger.getLogger(XXXServiceInterceptor.class);

    /**
     * StringRedis工具类
     */
    @Resource
    private RedisStrUtil redisStrUtil;

    /**
     * 方法拦截
     * @param point
     * @return
     */
    @Around(value = "execution(public * com.xxx.services.impl.XXXServiceImpl.listAll(..))" , argNames="point")
    public Object Method(ProceedingJoinPoint point) {

        //被拦截方法对应的返回值
        Object result = null;
        //Redis缓存对应的key值
        StringBuffer redisKey = new StringBuffer("redis_key:xxx@");

        //被拦截方法对应的参数
        Object[] args = point.getArgs();

        //根据参数,计算出key值
        for(Object obj : args){
            redisKey.append(obj.toString());
            redisKey.append("#");
        }

        //尝试从缓存中获取待返回值
        try{
            String value = redisStrUtil.get(redisKey.toString());
            result = Noson.parseArray(value);
            logger.info("使用Redis查询缓存成功,listKey:" + redisKey.toString() + " size:" + (result != null ? ((List)result).size() : 0));
        }catch (Exception e){
            logger.error("从Redis中获取缓存信息失败:" + e.getMessage());
        }

        //没有从缓存中获取到待返回的值,从数据库中取出来,并把值放入Redis中
        try {
            if(result == null || ((List)result).size() == 0){
                result = point.proceed();
                redisStrUtil.setEx(redisKey.toString() , Noson.reversal(result) , 31 , TimeUnit.DAYS);
                logger.info("使用数据库查询数据成功,将数据保存到redis中,listKey:" + redisKey.toString() + " size:" + (result != null ? ((List)result).size() : 0));
            }
        } catch (Throwable e) {
            logger.error("从数据库中获取数据信息失败:" + e.getMessage());
        }

        //打印日志,现在Redis缓存的key值
        logger.info("redis key : " + redisKey.toString());

        //返回结果
        return result;
    }

}

 

你可能感兴趣的:(Unix&Unix-Like)