2011-12-09 27 views
0

試圖向org.springframework.samples.petclinic中的方面包添加新的方面類。向Spring添加新方面PetClinic

我的切面類如下:

package org.springframework.samples.petclinic.aspects; 

import org.apache.openjpa.jdbc.sql.Join; 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.After; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.Pointcut; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.samples.petclinic.context.SessionContext; 

import java.util.Date; 

@Aspect 
public class MethodLogAspect { 

    Logger logger = LoggerFactory.getLogger(MethodLogAspect.class); 

    @Pointcut("execution(* org.springframework.samples..*.*(..))") 
    public void methodLogging(){} 

    @Before("methodLogging()") 
    public void logMethodStart(JoinPoint joinPoint){ 

     String methodName = joinPoint.getSignature().getName(); 
     String className = joinPoint.getTarget().getClass().getName(); 
     logger.info("class name: "+className+"invoked method:"+methodName+" at "+ ((new Date()).getTime())); 
    } 

    @After("methodLogging()") 
    public void logMethodEnd(JoinPoint joinPoint){ 
       String methodName = joinPoint.getSignature().getName(); 
       String className = joinPoint.getTarget().getClass().getName(); 
       logger.info("class name: "+className+"finished invoking method:"+methodName+" at "+ ((new Date()).getTime())); 
    } 

} 

然後我繼續和方面在aop.xml中的中/資源/ META-INF如下:

<?xml version="1.0"?> 

<!-- Custom aspects for the PetClinic sample application --> 
<aspectj> 

    <weaver> 
     <include within="org.springframework.samples.petclinic..*"/> 
    </weaver> 

    <aspects> 
     <aspect name="org.springframework.samples.petclinic.aspects.UsageLogAspect"/> 
     <aspect name="org.org.springframework.samples.petclinic.aspects.MethodLogAspect"></aspect> 
     <concrete-aspect name="org.springframework.samples.petclinic.aspects.ApplicationTraceAspect" 
       extends="org.springframework.samples.petclinic.aspects.AbstractTraceAspect"> 
      <pointcut name="traced" expression="execution(* org.springframework.samples..*.*(..))"/> 
     </concrete-aspect> 
    </aspects> 

</aspectj> 

當我建立戰爭並部署它,在我的方面指定的輸出沒有在日誌中顯示。我不確定我在這裏錯過了哪一步。我也覺得我不明白所有東西如何結合在一起的機制。有人能指出我錯過了什麼,並給我一個正確的方向推動。

感謝

編輯:

我能夠在web應用/ WEB-INF/spring文件夾添加豆(方面)的applicationContext-jdbc.xml來解決這個問題。我不知道爲什麼這會起作用?有人可以給我解釋嗎? -Thanks

+0

您確定要進行日誌配置嗎?你可以使用sop來排除這一點。其他方面的工作 - 對於aspectj集成,您需要使用javaagent命令行參數運行它 – gkamal

+0

@gkamal - 謝謝。是啊。 log4j.properties中的日誌記錄配置設置爲INFO.It似乎其他方面正在工作。什麼是SOP? –

+0

SOP - > System.out.println – gkamal

回答