Spring AOP的

问题描述:

package com.vanilla.daoService; 

    @Repository("daoService") 
    public class DaoServiceImpl implements DaoService { 

     @Override 
     public String addStudent(Student student) { 
      //saving new user 
      } 

     @Override 
     public String updateStudent(Student student) { 
      //update new user 
      } 

     @Override 
     public String getStudent(String id) { 
      //update new user 
      } 
    } 

我的业务逻辑类:Spring AOP的

package com.vanilla.blService; 

@Service("blService") 
public class BlServiceImpl implements BlService { 

    @Autowired 
    DaoService daoService; 

@Override 
public void updateStudent(String id){ 
    Student s = daoService.getStudent(id); 
    set.setAddress(address); 
    daoService.updateStudent(s); 
} 

} 

现在我想衡量每一个业务逻辑函数中执行的所有方法的执行

我创建(daoservice。*)我的外观类

@Aspect 
public class BlServiceProfiler { 

    @Pointcut("within(com.vanilla.blService.BlService.*)") 
    public void businessLogicMethods(){} 

     @Around("businessLogicMethods()") 
     public Object profile(ProceedingJoinPoint pjp) throws Throwable { 
      long start = System.currentTimeMillis(); 
      System.out.println("Going to call the method " + pjp.toShortString()); 
      Object output = pjp.proceed(); 
      System.out.println("Method execution completed."); 
      long elapsedTime = System.currentTimeMillis() - start; 
      System.out.println(pjp.toShortString()+" execution time: " + elapsedTime + " milliseconds."); 
      return output; 
     } 

} 

不幸的是,什么也没有发生。我认为我的@PointCut定义不正确,我怎么才能正确地做到这一点?

+0

你已经注册使用Spring的方面? (只是检查...) –

+0

@肖恩,是的,我做了:) –

你可能想这样的:

@Pointcut("within(com.vanilla.blService.BlService+)") 
public void businessLogicMethods(){} 

BlService+意味着BlService和所有子类/实现类。

+0

我测试了它,它看起来像应用于BlService.updateStudent而不是daoService方法,因为它的打印去。如果你想让它适用于DAO的,而不是再改变调用方法执行(BlService.updateStudent(..)) –

+0

它在'(com.vanilla.daoService.DaoService +)'内。你的问题说你想要切入DAO,但你的代码示例意味着你正在尝试切入点服务,这有点令人困惑。 –

+0

@Niall,@Pointcut(“within(com.vanilla.blService.BlService +)”),@Pointcut(“within(com.vanilla.blService.BlService。*)”)和@Pointcut(“执行(* com.vanilla.blService.BlService。*(..))“)他们都有相同的结果? –

尝试使用:

within(com.vanilla.blService.BlService) && execution(public * com.vanilla.daoService..*.*(..)) 
+0

我得到︰嵌套的异常是org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException:切入点表达式'businessLogicMethods()'包含不受支持的切入点原语'调用' –

+0

打扰一下。 Spring中@AspectJ不支持'call'。尝试像使用固定代码一样使用'execution'。 – Constantiner

+0

不幸的是,我没有执行,看起来像缺少pointCut。 –

你应该使用下面的切入点

@Pointcut("execution(* com.vanilla.blService.BlService.*(..))")