2016-11-04 78 views
0

我試圖在沒有運氣的情況下使用Spring/AspectJ集成。春天的版本是3.2.17(是的,有點舊,我知道)。Spring AspectJ集成不起作用

這裏是我的相關配置:

的pom.xml:

<!-- Spring dependencies, including spring-aspects --> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjrt</artifactId> 
     <version>1.7.4</version> 
    </dependency> 

    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjweaver</artifactId> 
     <version>1.7.4</version> 
    </dependency> 

的applicationContext.xml:

<context:annotation-config/> 
<aop:aspectj-autoproxy /> 
<bean id="loggingAspect" class="com.jason.app.web.util.logging.LoggingAspect" />  

LoggingAspect.java(相關類):

@Aspect 
public class LoggingAspect { 

    private Logger log = LoggerFactory.getLogger(LoggingAspect.class); 


    /** 
    * Advice for before logging 
    * @param joinPoint 
    */ 
    @Before("execution(* com.jason.app.web.process..*(..))") 
    private void beforeAdvice(JoinPoint joinPoint) { 

     final String outputFormat = "intercept: executing method %s(%s)"; 
     final String method =joinPoint.getSignature().getName(); 

     List<?> argumentList = Collections.unmodifiableList(Arrays.asList(joinPoint.getArgs())); 
     final String formattedArguments = argumentList.stream().map(s -> s.toString()).collect(Collectors.joining(", ")); 

     log.debug(String.format(outputFormat, method, formattedArguments)); 

    } 
} 

我倒了o nline教程,沒有運氣。任何人都可以指出我做錯了什麼?

傑森

+0

由於Spring-AOP的限制,當我試圖討論與AspectJ的Spring集成時,由於主持人確信自己我想使用Spring-AOP,因此將其關閉。 謝謝你沒有人。 – Jason

回答

1

,你可以添加更多的依賴

<dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aop</artifactId> 
      <version>${spring.version}</version> 
</dependency> 

嘗試,改變你的切入點,以

@Before("execution(* com.jason.app.web.process..*.*(..))") 

(指通知將被應用到服務中定義的所有公共方法包或子包:com.jason.app.web.process)

+0

什麼都沒有。我是否必須啓用編譯時編織或使用aspectj maven插件? 我的問題是,我有一個要求記錄ALL方法調用的條目和結果,而不管訪問說明符如何。所以,Spring AOP不會工作,因爲它是基於代理的,只能執行公共方法。 – Jason

+1

@Jason他們你必須去純粹的方面j。正如你所說春天只能攔截公共方法 檢查http://stackoverflow.com/q/23327336/410677 – kuhajeyan

+1

這就是爲什麼我說AspectJ而不是Spring AOP。 – Jason

0

更改e上的表達,以
@Before("execution(public * your.package.YourClass.yourMethod(..))")

2

Spring配置標籤<aop:aspectj-autoproxy />將使Spring基於代理的AOP的基礎設施,它僅適用於Spring的bean,它並因此使用代理相比於純粹的AspectJ一個該解決方案的所有限制。

現在,如果您想要使用AspectJ而不是Spring AOP,則需要在編譯時編織或加載時編織之間進行選擇。如果使用編譯時編織,則需要將aspectj-maven-plugin添加到您的版本中。如果選擇加載時織入,則需要運行帶有-javaagent:path/to/aspectjweaver.jar vm參數的JVM,如AspectJ Documentation中所述。

如果你需要使你的方面由Spring(自動裝配等)進行後處理,你需要在你的Spring配置中列出它。 Aspects是在Spring之外創建的單例實例,因此您需要指定靜態工廠方法aspectOf()來訪問由AspectJ運行時創建的aspectj的單個實例。

<bean id="loggingAspect" 
    class="com.jason.app.web.util.logging.LoggingAspect" 
    factory-method="aspectOf" 
/> 

或註釋的方式:

@Configuration 
public class AspectConfig { 
    @Bean 
    public LoggingAspect loggingAspect() { 
     return LoggingAspect.aspectOf(); 
    } 
} 

不要忘記刪除<aop:aspectj-autoproxy />,如果你不打算使用Spring AOP的除了AspectJ的。你爲什麼選擇這樣做,當AspectJ更強大?

+0

謝謝。看起來Spring/AspectJ的大部分教程都遺漏了這一點。 Spring AOP不能滿足我的需求,因爲我需要記錄所有方法的輸入和輸出,而不僅僅是公共的。 – Jason