1. Create a Maven project and import the pom. XML dependencies


      
<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>org.example</groupId>
    <artifactId>spring-aop</artifactId>
    <version>1.0 the SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.3.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5. RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.5. RELEASE</version>
        </dependency>
    </dependencies>

</project>
Copy the code

2. Create interfaces, implementation classes, and enhanced methods for dynamic proxy

package com.abchina.aop;

public interface TargetInterface {
    public void method(a);
}

Copy the code
package com.abchina.aop;

public class Target implements TargetInterface {
    public void method(a) {
        System.out.println("Target Running...."); }}Copy the code

2.1 Creating a Test Class Use the dynamic proxy delivered with the JDK

package com.abchina.test;

import com.abchina.aop.TargetInterface;
import com.abchina.aop.Target;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;


public class AopTest {
    // Dynamic proxy based on JDK
    @Test
    public void testAop(a) {

        final Target target = new Target();
        TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), new InvocationHandler() {
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("Pre-enhanced code...");
                        Object invoke = method.invoke(target, args);
                        System.out.println("Post-enhanced code...");
                        returninvoke; }}); proxy.method(); }}Copy the code

2.2 Using cglib Dynamic Proxy

package com.abchina.test;

import com.abchina.aop.TargetInterface;
import com.abchina.aop.Target;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

public class AopTest {

    // Cglib based dynamic proxy
    @Test
    public void testCglibAop(a) {
        final Target target = new Target();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Target.class);
        enhancer.setCallback(new MethodInterceptor() {
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("Pre-enhanced code...");
                Object invoke = method.invoke(target, objects);
                System.out.println("Post-enhanced code...");
                returninvoke; }}); Target proxy = (Target) enhancer.create(); proxy.method(); }@Resource
    private TargetInterface target;
    @Test
    public void xmlAopTest(a) { target.method(); }}Copy the code

3. Configure AOP using XML

3.1 Creating a Section class

package com.abchina.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;


public class MyAspect {
    public void before(a) {
        System.out.println("Pre-code enhancement...."); }}Copy the code

3.2 Writing AN XML configuration File

3.2.1 Configure the Target class and Aspect Class

<! -- Configure target class -->
    <bean id="target" class="com.abchina.aop.Target"/>
<! -- Configure the section class -->
    <bean id="myAspect" class="com.abchina.aop.MyAspect"/>
Copy the code

3.2.2 Weaving into the section

    <aop:config>
        <aop:aspect ref="myAspect">
            <aop:before method="before" pointcut="execution(public void com.abchina.aop.Target.method())"/>
        </aop:aspect>
    </aop:config>
Copy the code

3.2.3 Creating a test class to test AOP

package com.abchina.test;

import com.abchina.aop.TargetInterface;
import com.abchina.aop.Target;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {

    @Resource
    private TargetInterface target;
    @Test
    public void xmlAopTest(a) { target.method(); }}Copy the code

Configure AOP using annotations

4.1 Enable annotation component scanning and AOP’s automatic proxy

<! Annotation development needs to scan the package in which the component resides -->
    <context:component-scan base-package="com.abchina.aop"/>
<! Aop automatic proxy -->
    <aop:aspectj-autoproxy/>
Copy the code

4.2 Registering the target class in the Spring container

@Component("target")
public class Target implements TargetInterface 
Copy the code

4.3 Register the custom aspect with the Spring container and configure the aspect

Notice: Sections can be registered on classes and methods. Section expressions can be abbreviated

package com.abchina.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component("myAspect")
@Aspect
public class MyAspect {
    @Before("execution(* com.abchina.aop.*.*(..) )"
    public void before(a) {
        System.out.println("Pre-code enhancement...."); }}Copy the code

5. Source code link