2011-07-29 137 views
3
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

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

回答

2

你可能想這樣的:

@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。*(..))「)他們都有相同的結果? –

0

嘗試使用:

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。 –

1

你應該使用下面的切入點

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