2016-12-16 313 views
0

我想将AspectJ合并到我的应用程序中以了解其工作原理。我不想使用Spring AOP,而是“纯”aspectj。开始使用aspectj而不使用Spring AOP

这是我有:

<dependency> 
    <groupId>org.aspectj</groupId> 
    <artifactId>aspectjrt</artifactId> 
    <version>1.6.11</version> 
</dependency> 

和:

package tmp; 

import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.Pointcut; 

@Aspect 
public class LoggingAspect { 

    @Pointcut("execution(* *.*(..))") 
    void anyMethodCall() { 
    } 

    @Before("anyMethodCall()") 
    public void beforeMethod() { 
     System.out.println("Aspect Before Method"); 
    } 
} 

当我执行我的应用程序,没有打印的信息。

我理解它的方式,beforeMethod应该在整个项目中的任何类的任何方法之前调用。

我猜我忘了一些东西,但我还没有找到一个很好的教程,但它对我来说很清楚它是如何工作的。

我在哪里可以到这里?

+0

如果您刚刚开始使用最新的aspectj版本(截至目前的1.8.9版本),而不是几年多的版本。 –

回答

1

在Eclipse项目类型必须更改为AspectJ项目(鼠标右键在项目 - > AspectJ的)

您需要的pom.xml

<dependencies> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjrt</artifactId> 
    </dependency> 
</dependencies> 
<build> 
    <pluginManagement> 
     <plugins> 
      <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>aspectj-maven-plugin</artifactId> 
      <configuration> 
       <complianceLevel>1.7</complianceLevel> 
       <source>1.7</source> 
       <target>1.7</target> 
      </configuration> 
      <executions> 
       <execution> 
        <goals> 
         <goal>compile</goal> 
         <goal>test-compile</goal> 
        </goals> 
       </execution> 
      </executions> 
      </plugin> 
      <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.--> 
      <plugin> 
       <groupId>org.eclipse.m2e</groupId> 
       <artifactId>lifecycle-mapping</artifactId> 
       <version>1.0.0</version> 
       <configuration> 
        <lifecycleMappingMetadata> 
         <pluginExecutions> 
          <pluginExecution> 
           <pluginExecutionFilter> 
            <groupId> 
             org.codehaus.mojo 
            </groupId> 
            <artifactId> 
             aspectj-maven-plugin 
            </artifactId> 
            <versionRange> 
             [1.7,) 
            </versionRange> 
            <goals> 
             <goal>compile</goal> 
             <goal>test-compile</goal> 
            </goals> 
           </pluginExecutionFilter> 
           <action> 
            <ignore></ignore> 
           </action> 
          </pluginExecution> 
         </pluginExecutions> 
        </lifecycleMappingMetadata> 
       </configuration> 
      </plugin> 
     </plugins> 
    </pluginManagement> 

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

改变你的切入点!”(TMP内。 LoggingAspect)& &执行(* (..))“为了避免抓住他自己。

+0

AspectJ Maven插件的当前版本是1.9。它也可以再次与Eclipse结合使用,而1.8版本存在一个问题,导致与Eclipse重新导入项目不兼容。所以没有必要再使用旧的1.7了。 – kriegaex

+0

有没有办法,我可以做这个常规的Maven项目?为什么我需要将它转换为aspectj项目? – user3629892

+0

@ user3629892 aspectj需要在编译后调用编织逻辑。你的项目可以成为一个maven项目。但编译类型必须更改。 –

1

在我的IDEA中,自从版本13以来,它已经内置了aspectj的插件。您应该做的下一步是更改Java编译器的设置。

Project Setting –> Compiler –> Java Compiler 

“使用编译”:更改为Ajc;

“Ajc编译器的路径”:aspjectjtools.jar;

如果你没有罐子,请打http://mvnrepository.com/artifact/org.aspectj得到你想要的。

现在,运行你的程序,它会起作用。祝你好运!