承接上篇文章,本篇讲述Aop的agent增强方式

@Aspect
public class MyAspect {
 
    @Before("execution(* com.shuttle.spring5.service.MyService.*())")
    public void before() {
        System.out.println("before service...");
    }
 
}
 
@Service
public class MyService {
 
    public void service1() {
        System.out.println("service1...");
        hello();
    }
 
    public void hello() {
        System.out.println("hello...");
    }
 
}
 
@SpringBootApplication
public class A10Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(A10Application.class);
        MyService service = context.getBean(MyService.class);
        System.out.println("service class: " + service.getClass());
        service.service1();
    }
}

运行时虚拟机参数设置

-javaagent:C:\Users\123\.m2\repository/org/aspectj/aspectjweaver/1.9.4/aspectjweaver-1.9.4.jar

程序运行结果如下:

service class: class com.shuttle.spring5.service.MyService

before service…
service1…

before service…
hello…

类加载增强class文件【截取自课程视频】:

Logo

Agent 垂直技术社区,欢迎活跃、内容共建,欢迎商务合作。wx: diudiu5555

更多推荐