2015-06-28 50 views
2

後,自我調用AOP問題仍然存在,我希望使用AOP在我們的項目中進行日誌記錄。我面臨的問題是,如果一個類的方法正在調用其中的同一個類的另一個方法,那麼AOP就不能在這個調用中工作,因爲這是代理方式。爲了對付它,我正在嘗試使用aspectj maven插件編譯時間編織。我的頂級項目POM看起來是這樣的:即使在編譯時加入了aspectj maven插件編織

<dependencies> 
     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjrt</artifactId> 
      <version>1.8.1</version> 
     </dependency> 
</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>aspectj-maven-plugin</artifactId> 
      <version>1.7</version> 
      <configuration> 
       <complianceLevel>1.7</complianceLevel> 
       <source>1.7</source> 
       <target>1.7</target> 
      </configuration> 
      <executions> 
       <execution> 
        <goals> 
         <goal>compile</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

我有6個個子項目,但我希望做基於AOP記錄只在其中的一個。該項目的POM是這樣的:

<dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjrt</artifactId> 
     <version>1.8.1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aop</artifactId> 
     <version>4.1.6.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjweaver</artifactId> 
     <version>1.8.1</version> 
    </dependency> 

<build> 
    <plugins>   
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>aspectj-maven-plugin</artifactId> 
     </plugin>   
    </plugins> 
</build> 

我以爲這樣就解決了問題,但它仍然是在做基於代理的方法調用。還有什麼需要做的?

編輯:春季context.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:task="http://www.springframework.org/schema/task" 
xsi:schemaLocation=" 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> 

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

<!-- Allow proxys --> 
<!-- <aop:aspectj-autoproxy /> --> 

<context:component-scan base-package="com.relevant.package" ></context:component-scan> 
    </beans:beans> 

LoggingAspect.java

@Named 
@Aspect 
public class LoggingAspect { 

@Before("execution(public * com.relevant.package.*.process(..))") 
public void beforeProcessAdvice() { 
    System.out.println("AOP"); 
    System.out.println("Before"); 

    } 

@Before("execution(public * com.relevant.package.*.internalMethod(..))") 
public void beforeProcessAdvice() { 
    System.out.println("Internal"); 
    System.out.println("Method"); 

    } 
} 

目標文件處理方法,如:

process() { 
internalMethod(); 
} 

因爲我有評論自動代理部分,我是說不要使用代理。但是,現在不會出現任何日誌(既不調用過程方法,也不調用internalMethod)。如果autoproxy打開,則日誌顯示爲處理方法,但不顯示internalMethod,這是可以理解的。

+0

您能否提供一個簡潔,最小化,可重現的[SSCCE](http://sscce.org/)? Maven配置很有趣,但實際的代碼和Spring配置呢?它可能會幫助別人來幫助你。 :-) – kriegaex

+0

@kriegaex我編輯了一些相關的更改。我只想編織時間編織。 –

+0

爲了使AspectJ編譯器「看到」你的方面,它們或者需要與你想要編入的代碼位於相同的Maven模塊中,或者你需要配置AspectJ Maven Plugin以在編織的另一個模塊中找到它們依賴關係由於Codehaus已經關閉,目前正在遷移到Mojohaus,所以仍然沒有插件文檔頁面,但是我找到了一個[存檔版本](http://web.archive.org/web/20150507060929/http:/ /mojo.codehaus.org/aspectj-maven-plugin/compile-mojo。HTML#weaveDependencies)。 – kriegaex

回答

0

經過一天這個問題,解決方案很奇怪。我做了一步步的調試結果如下:

  1. 我檢查了showWeaveInfo和詳細爲true AspectJ的插件,和Maven構建過程中,我可以看到正確的方面是在正確的得到應用加入點。

  2. 然後我檢查了編譯後的.class文件,看看編織是否確實完成了,令我驚訝的是,.class被正確編織了。

  3. 然後我點擊Maven-> Update maven項目並再次檢查字節碼。現在編織的零件已從.class文件中消失。

因此,一旦我做了一個更新Maven項目.class文件會以某種方式恢復到未與縱橫碼編織的狀態。刷新項目而不是更新Maven項目解決了我的問題,但我無法理解這個原因。

相關問題