2017-03-16 179 views
0

我正在尝试记录用自定义界面注释的方法的执行时间。用于内部和私有方法的AOP Java

我正在使用Spring AOP。

但是这似乎不适用于内部方法。

我认为这是在Spring AOP限制

@Aspect 
public class BusinessProfiler { 

    private static Log log = LogFactory.getLog(BusinessProfiler.class); 


    @Around("execution(* *(..)) && @annotation(TimeLog)") 
    public Object profile(ProceedingJoinPoint point) throws Throwable { 
    long start = System.currentTimeMillis(); 
    Object result = point.proceed(); 
    String format = 
     String.format("%s#%s: took [%s msec]", point.getTarget().getClass().getSimpleName(), 
      MethodSignature.class.cast(point.getSignature()).getMethod().getName(), 
      System.currentTimeMillis() - start); 
    log.info(format); 
    return result; 
    } 

} 

是否有比Spring AOP

回答

2

如果你想想办法AOP注解由Spring处理,这将是明确的任何备选方案:

Spring将您的课程包装在一个代理中,并通过添加的AOP注释在运行中生成额外的代码。因此,只有通过代理调用的代码(即来自您课堂外的代码才会包含在内)。

@Service 
public class Foo { 

    public void doSomething() { 
     doSomethinInternal(); 
    } 

    private void doSomethingInternal() { 
    } 
} 

如果从其他的Spring bean我这样做:

@Service 
public class Bar { 

    @Autowired 
    private Foo foo; 

    public void execute() { 
     foo.doSomethinInternal(); 
    } 
} 

只有DoSomething的将通过其包装类,不doSomethingInternal代理,将由您被称为被称为类。

+0

谢谢。有没有其他办法可以达到我的要求。我认为它与CDI相关。将CDI拦截器与Spring集成的任何选项 – Patan

+2

此问题如何与CDI连接? CDI是关于依赖注入的,AOP是关于连接点截取的。如果你不愿意修改你的代码,并且需要捕获内部类,通过'this'调用方法(而不是通过外部代理)等等,你总是可以[在Spring中使用完整的AspectJ](https:// docs。 spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-using-aspectj),甚至完全没有Spring。 – kriegaex