欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring集成aspectj 博客分类: 框架 springaopaspectj 

程序员文章站 2024-02-14 09:26:46
...
	<!--支持spring注解-->
	<context:annotation-config />  
	<context:component-scan base-package="xx" />
	<!--支持aspectj注解,注意设置proxy-target-class为true,强制使用cglib-->
	<aop:aspectj-autoproxy proxy-target-class="true"/>


@Aspect
@Component
public class TickLogInterceptorWithAspectJ
{
    public void pointCut()
    {
    }
    
    @Around("execution(* xx..*.CarMaker.make*(..))")
    public Object tick(ProceedingJoinPoint joinPoint)
    {
        System.out.println(MessageFormat.format("enter method: {0}.{1}, params: {2}",
            joinPoint.getSignature().getDeclaringTypeName(),
            joinPoint.getSignature().getName(),
            getParamsString(joinPoint.getArgs())));
            
        try
        {
            Object o = joinPoint.proceed(joinPoint.getArgs());
            
            System.out.println(MessageFormat.format("exit method: {0}.{1}, result: {2}",
                joinPoint.getSignature().getDeclaringTypeName(),
                joinPoint.getSignature().getName(),
                o));
                
            return o;
        }
        catch (Throwable e)
        {
            return null;
        }
    }
    
    private String getParamsString(Object[] params)
    {
        return Joiner.on(",").join(Arrays.asList(params));
    }
}