AspectJ compile-time weaving - CTW

         AspectJ is a concrete realization framework of AOP. AOP (Aspect Oriented Programming) is aspect-oriented programming, a technology that can dynamically and uniformly add functions to programs without modifying the source code through pre-compilation and runtime dynamic agents. AspectJ can weave aspects not only through precompilation (CTW) and runtime dynamic proxy, but also during loading (Load Time Weaving, LTW). AspectJ extends Java and defines some specialized AOP syntax.

      1 pom configuration maven plugin:

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


      2 Add pom dependency

        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.13</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>

      3 Write the injected sevice class

public interface SampleService {
    public String getName(String name);
}

public class SampleServiceImpl implements SampleService {
    @Override
    public String getName(String name) {
        return name;
    }
}

      4 Write aspect classes

@Aspect
public class SampleAspect {
    /**
     * 切入点:SampleService继承树中所有方法。
     */
    @Pointcut("execution(* com.li.service.impl..*(..))")
    public void methodePointCut(){

    }

    @Before("methodePointCut()")
    public void monitor(JoinPoint joinPoint) throws Throwable{
        System.out.println(joinPoint.getStaticPart());
    }

    @After("methodePointCut()")
    public void monitor2(JoinPoint joinPoint) throws Throwable{
        System.out.println(joinPoint.getStaticPart());
    }
}

      5 Write test cases

public class SampleTest {
    @Test
    public void testAspect(){
        SampleService sampleService = new SampleServiceImpl();
        System.out.println(sampleService.getName("aaa"));
    }
}


      6 Run the test

execution(String com.li.service.impl.SampleServiceImpl.getName(String))
execution(String com.li.service.impl.SampleServiceImpl.getName(String))
aaa

     

      7 facets provided by individual projects

       The pom configuration where the aspect is located:

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

     The cut-in project references the aspect project

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.10</version>
                <configuration>
                    <complianceLevel>1.8</complianceLevel>
                    <source>1.8</source>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>com.li</groupId>
                            <artifactId>aspectJ2</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
</build>

Guess you like

Origin blog.csdn.net/lsb2002/article/details/78973933