1. 程式人生 > >AspectJ 在spring專案中的maven配置

AspectJ 在spring專案中的maven配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>me.mritd</groupId>
  <artifactId>Test1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>


  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjtools -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjtools</artifactId>
      <version>1.8.9</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.7.4</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-clean-plugin</artifactId>
      <version>2.4.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-clean-plugin</artifactId>
      <version>2.4.1</version>
    </dependency>


  </dependencies>
  <build>
    <plugins>
      <!-- Maven 編譯外掛 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <compilerVersion>1.8</compilerVersion>
          <fork>true</fork>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <!-- AspectJ 編譯外掛 -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.8</version>
        <configuration>
          <verbose>true</verbose>
          <privateScope>true</privateScope>
          <showWeaveInfo>true</showWeaveInfo>
          <source>1.8</source>
          <target>1.8</target>
          <complianceLevel>1.8</complianceLevel>
          <!-- <encoding>UTF-8</encoding> -->
          <verbose>false</verbose>
          <outxml>true</outxml>
          <aspectLibraries>
            <!-- 此處定義外部的aspect包,例如spring的事務aspect包 。這裡引用的包必須在依賴中宣告 -->
            <!--
              <aspectLibrary>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-aspects</artifactId>
             </aspectLibrary>
            -->
          </aspectLibraries>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
              <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <ajdtVersion>1.6.11</ajdtVersion>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

這個是可以把程式碼打包成jar 或者war的

注意 我這邊用的是idea  idea需要把

File -> settings -> Java Compiler 


這樣  

ok

這個大家可以參考下 http://opoo.org/aspectj-compile-time-weaving/

感謝前輩大牛 的分享