1 Aspectj切面切本地方法

1.1 创建springboot项目

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.9.7</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.7</version>
</dependency>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <!-- AspectJ Maven 插件 -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.14.0</version>
            <configuration>
                <source>11</source>
                <target>11</target>
                <complianceLevel>11</complianceLevel>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
@Aspect
@Component
public class HelloAspect {

    @Before("execution(public * org.example.service.impl.HelloServiceImpl.*(..))")
    public void beforeHello() {
        System.out.println("[Aspect] 进入");
    }
}
@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public void hello() {
        System.out.println("打招呼一次");
        // 做本地方法调用
        hello2();
    }

    public void hello2() {
        System.out.println("打招呼两次");
    }
}

maven工具中clean再complie,即可切入到本地方法

2 SpringAop切面切本地方法

去除AspectJ Maven 插件

添加切面类

@Aspect
@Component
// 注解也可以放在启动类上
@EnableAspectJAutoProxy(exposeProxy = true)
public class HelloAspect {

    @Before("execution(public * org.example.service.impl.HelloServiceImpl.*(..))")
    public void beforeHello() {
        System.out.println("[Aspect] 进入");
    }
}
/**
 * @author 陈cc
 * @version 1.0
 * @date 2025/4/10 19:26
 */
@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public void hello() {
        System.out.println("打招呼一次");
        // 获取当前服务类的代理(需要开启@EnableAspectJAutoProxy(exposeProxy = true))
        HelloService service = (HelloService) AopContext.currentProxy();
        service.hello2();
    }

    public void hello2() {
        System.out.println("打招呼两次");
    }
}