学习SpringAOP

This commit is contained in:
2026-10-14 10:24:41 +08:00
parent 26df9a0d87
commit b7350ee43d
29 changed files with 790 additions and 0 deletions
@@ -0,0 +1,5 @@
package com.ycjy.ch01.a;
public interface Imp {
void doSth();
}
@@ -0,0 +1,25 @@
package com.ycjy.ch01.a;
import org.springframework.aop.framework.AopContext;
import org.springframework.aop.framework.AopProxy;
import org.springframework.stereotype.Service;
@Service
public class Impl implements Imp {
@Override
public void doSth(){
System.out.println("doSth--");
doSth2();// 为什么调用方法没有切面?
// 因为底层super.doSth2()就是调用上层的doSth方法
// 执行到doSth2();的时候就是this.doSth2()了, 所以没有aop
// 拿到当前代理对象
Impl proxy = (Impl) AopContext.currentProxy();
proxy.doSth2();
// 切面通过切面表达式在连接点处对目标对象进行通知并由AOP代理将织入逻辑应用到目标对象
}
void doSth2(){
System.out.println("doSth2--");
}
}
@@ -0,0 +1,10 @@
package com.ycjy.ch01.a;
import org.springframework.stereotype.Service;
@Service
public class Impl2 {
void doSth2(){
System.out.println("doSthImpl2--");
}
}
@@ -0,0 +1,19 @@
package com.ycjy.ch01.a;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Log {
@Before("execution(void com.ycjy.ch01.a.Impl.*())")
void log(){
System.out.println("log---bef");
}
// @Before("execution(void com.ycjy.ch01.a.Impl.doSth())")
// void log2(){
// System.out.println("log---bef2");
// }
}
@@ -0,0 +1,15 @@
package com.ycjy.ch01.a;
import com.ycjy.ch01.config.ApplicationConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
Imp impl = context.getBean(Imp.class);
impl.doSth();
// System.out.println(impl.getClass());
// impl.doSth2();
// impl.doSth3();
}
}
@@ -0,0 +1,12 @@
package com.ycjy.ch01.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)// proxyTargetClass= true强制CGLIB
@ComponentScan("com.ycjy.ch01.a")
public class ApplicationConfig {
}
@@ -0,0 +1,19 @@
package com.ycjy.ch02.a;
import org.springframework.stereotype.Service;
@Service
public class Impl {
// public int doSth(){
// System.out.println("做了点事情###");
//
// throw new RuntimeException("抛出了一些信息");
//
//// return 100;
// }
public int add(int x, int y){
System.out.println(x + "+" + y + "========");
return x+y;
}
}
@@ -0,0 +1,87 @@
package com.ycjy.ch02.a;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Log {
// // 在之前
// @Before("execution(* com.ycjy.ch02.a.Impl.doSth())")
// public void before() {
// System.out.println("before###");
// }
//
// // 做完之后
// @AfterReturning("execution(* com.ycjy.ch02.a.Impl.doSth())")
// public void afterReturning(){
// System.out.println("after-returning###");
// }
//
// // 之后异常
// @AfterThrowing("execution(* com.ycjy.ch02.a.Impl.doSth())")
// public void afterThrowing(){
// System.out.println("after-throwing###");
// }
// // 之后异常(携带信息)
// @AfterThrowing(value = "execution(* com.ycjy.ch02.a.Impl.doSth())",
// throwing = "ex")
// public void afterThrowing(Exception ex){
// System.out.println("after-throwing###" + ex.getMessage());
// }
//
// // 在之后
// @After("execution(* com.ycjy.ch02.a.Impl.doSth())")
// public void after(){
// System.out.println("after###");
// }
// 环绕(测试这个的时候注释掉上面的, 这一个几乎全能, 能满足上述)
// @Around("execution(* com.ycjy.ch02.a.Impl.*())")
// public void around(ProceedingJoinPoint pjp) {
// System.out.println("之前执行###");
// try {
// pjp.proceed();
// System.out.println("做完之后执行###");
// } catch (Throwable e) {
// System.out.println("捕获到异常执行###");
// } finally {
// System.out.println("最终###");
// }
// }
// // 环绕(测试这个的时候注释掉上面的, 这一个几乎全能, 能满足上述)
// @Around("execution(* com.ycjy.ch02.a.Impl.*())")
// public Object around(ProceedingJoinPoint pjp) throws Throwable {
// System.out.println("之前执行###");
// try {
// Object proceed = pjp.proceed();
// System.out.println("做完之后执行###");
// return proceed;
// } catch (Throwable e) {
// System.out.println("捕获到异常执行###");
// throw e;// 继续抛出异常
// } finally {
// System.out.println("最终###");
// }
// }
// 多参数条件下, 两个点, 任意参数
// 环绕(测试这个的时候注释掉上面的, 这一个几乎全能, 能满足上述)
@Around("execution(* com.ycjy.ch02.a.Impl.add(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("之前执行###");
try {
Object[] args = pjp.getArgs();
System.out.println("第一个值: " + args[0]);
System.out.println("第二个值: " + args[1]);
return pjp.proceed(args);
} catch (Throwable e) {
System.out.println("捕获到异常执行###");
throw e;// 继续抛出异常
} finally {
System.out.println("最终###");
}
}
}
@@ -0,0 +1,18 @@
package com.ycjy.ch02.a;
import com.ycjy.ch02.config.Application;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
Impl bean = context.getBean(Impl.class);
// int i = bean.doSth();
// System.out.println("值"+i);
// System.out.println("结果: " + bean.add(2, 5));
int add = bean.add(5, 5);
System.out.println("x'x'x: " + add);
}
}
@@ -0,0 +1,11 @@
package com.ycjy.ch02.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com.ycjy.ch02.a")
public class Application {
}
@@ -0,0 +1,45 @@
package com.ycjy.chAnLiZongJie;
import com.ycjy.chAnLiZongJie.config.Application;
import com.ycjy.chAnLiZongJie.omponentScan.service.cglib.C;
import com.ycjy.chAnLiZongJie.omponentScan.service.jdk.AImpl;
import com.ycjy.chAnLiZongJie.omponentScan.service.jdk.AInterface;
import com.ycjy.chAnLiZongJie.omponentScan.service.jdk.MyParam;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
// a beanA = context.getBean(a.class);
// beanA.aaa();
// beanA.bbb();
// b beanB = context.getBean(b.class);
// beanB.ccc();
// beanB.ddd();
// AInterface beanA = context.getBean("AImpl",AInterface.class);
//// beanA.doSth();
// beanA.doSth2("abc","abcd",1);
// AInterface beanB = context.getBean("BImpl",AInterface.class);
//// beanB.doSth();
// beanB.doSth2("abc2","abcd",1);
// C bean = context.getBean(C.class);
// bean.c(new MyParam());
// AInterface a = context.getBean("AImpl", AInterface.class);
// a.doSth();
// AInterface b = context.getBean("BImpl", AInterface.class);
// b.doSth();
// AInterface bean = context.getBean("AImpl",AInterface.class);
// bean.doSth();
// bean.doSth2("x","y",1);
AInterface a = context.getBean("AImpl", AInterface.class);
a.doSth();
AInterface b = context.getBean("BImpl", AInterface.class);
b.doSth();
}
}
@@ -0,0 +1,11 @@
package com.ycjy.chAnLiZongJie.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.ycjy.chAnLiZongJie.omponentScan")
@EnableAspectJAutoProxy
public class Application {
}
@@ -0,0 +1,29 @@
package com.ycjy.chAnLiZongJie.omponentScan.aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
// 切面内共享
@Component
@Aspect
public class LogAspect1 {
// 切点
@Pointcut("bean(AImpl)")
public void p(){}
// 通知
@Before("p()")
public void before() {
System.out.println("before###");
}
// 通知
@After("p()")
public void after() {
System.out.println("after###");
}
}
@@ -0,0 +1,85 @@
package com.ycjy.chAnLiZongJie.omponentScan.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import java.lang.annotation.Target;
@Component
@Aspect
public class PcdAspect {
// 基础语法 ? 代表可有可无
// // 修饰符? 返回类型 包?.类?.方法名(参数) 异常?
// @Before("execution(* com.ycjy.chAnLiZongJie.omponentScan.service.cglib.a.doSth())")
// public void beforeExecution(){
// System.out.println("--beforeExecution###做了一些事情");
// }
// // within(包.类)
// @Before("within(com.ycjy.chAnLiZongJie.omponentScan.service.cglib.*)")
// public void beforeWithin(){
// System.out.println("--beforeWithin###做了一些事情");
// }
// // this(类型)cglib
// @Before("this(com.ycjy.chAnLiZongJie.omponentScan.service.cglib.b)")
// public void beforeThis(){
// System.out.println("--beforeThis###做了一些事情");
// }
// // this(类型)jdk
// @Before("this(com.ycjy.chAnLiZongJie.omponentScan.service.jdk.AInterface)")
// public void beforeThis(){
// System.out.println("--beforeThis###做了一些事情");
// }
// // target(类型)jdk
// @Before("target(com.ycjy.chAnLiZongJie.omponentScan.service.jdk.AInterface)")
// public void beforeTarget(){
// System.out.println("--beforeTarget###做了一些事情");
// }
// // args(类型)jdk
// @Before("args(String,String,int)")
// public void beforeArgs(){
// System.out.println("--beforeArgs###做了一些事情");
// }
// // @target
// @Before("@target(com.ycjy.chAnLiZongJie.omponentScan.service.jdk.MyAnno)")
// public void beforeAtTarget(){
// System.out.println("--beforeAtTarget###做了一些事情");
// }
// // @args
// @Before("@args(com.ycjy.chAnLiZongJie.omponentScan.service.jdk.ParamAnno)")
// public void beforeAtArgs(){
// System.out.println("--beforeAtArgs###做了一些事情");
// }
// // @within
// @Before("@within(com.ycjy.chAnLiZongJie.omponentScan.service.jdk.MyAnno)")
// public void beforeWithin(){
// System.out.println("--beforeWithin###做了一些事情");
// }
// // @annotation
// @Before("@annotation(com.ycjy.chAnLiZongJie.omponentScan.service.jdk.MethodAnno)")
// public void beforeAnnotation(){
// System.out.println("--beforeAnnotation###做了一些事情");
// }
// // bean
// @Before("bean(AImpl)")
// public void beforeBean(){
// System.out.println("--beforeBean###做了一些事情");
// }
// bean
@Before("bean(aimpl) || bean(bimpl)")
public void beforeBean(){
System.out.println("--beforeBean###做了一些事情");
}
}
@@ -0,0 +1,15 @@
package com.ycjy.chAnLiZongJie.omponentScan.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
// 切面内共享
@Component
@Aspect
public class TxAspect1 {
@Before("com.ycjy.chAnLiZongJie.omponentScan.aspect.LogAspect1.p()")
public void before() {
System.out.println("xxx");
}
}
@@ -0,0 +1,11 @@
package com.ycjy.chAnLiZongJie.omponentScan.service.cglib;
import com.ycjy.chAnLiZongJie.omponentScan.service.jdk.MyParam;
import org.springframework.stereotype.Component;
@Component
public class C {
public void c(MyParam param) {
System.out.println("c" + param);
}
}
@@ -0,0 +1,14 @@
package com.ycjy.chAnLiZongJie.omponentScan.service.cglib;
import org.springframework.stereotype.Service;
@Service
public class a {
public void aaa(){
System.out.println("我是aaa");
}
public void bbb(){
System.out.println("我是bbb");
}
}
@@ -0,0 +1,14 @@
package com.ycjy.chAnLiZongJie.omponentScan.service.cglib;
import org.springframework.stereotype.Service;
@Service
public class b {
public void ccc(){
System.out.println("我是ccc");
}
public void ddd(){
System.out.println("我是ddd");
}
}
@@ -0,0 +1,23 @@
package com.ycjy.chAnLiZongJie.omponentScan.service.jdk;
import org.springframework.stereotype.Service;
@Service
//@MyAnno
public class AImpl implements AInterface{
@MethodAnno
@Override
public void doSth() {
System.out.println("我是jdk-AImpl实现类");
}
// @MethodAnno
@Override
public void doSth2(String s, String s2, int i) {
System.out.println("AImpl =" + s);
}
public void use(MyParam p) {
System.out.println("AImpl.use 收到参数");
}
}
@@ -0,0 +1,9 @@
package com.ycjy.chAnLiZongJie.omponentScan.service.jdk;
public interface AInterface {
void doSth();
void doSth2(String s, String s2, int i);
void use(MyParam p);
}
@@ -0,0 +1,21 @@
package com.ycjy.chAnLiZongJie.omponentScan.service.jdk;
import org.springframework.stereotype.Service;
@Service
//@MyAnno
public class BImpl implements AInterface {
@Override
public void doSth() {
System.out.println("我是jdk-BImpl");
}
@Override
public void doSth2(String s, String s2, int i) {
System.out.println("BImpl =" + s);
}
public void use(MyParam p) {
// System.out.println("AImpl.use 收到参数");
}
}
@@ -0,0 +1,11 @@
package com.ycjy.chAnLiZongJie.omponentScan.service.jdk;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodAnno {
}
@@ -0,0 +1,11 @@
package com.ycjy.chAnLiZongJie.omponentScan.service.jdk;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnno {
}
@@ -0,0 +1,5 @@
package com.ycjy.chAnLiZongJie.omponentScan.service.jdk;
@ParamAnno
public class MyParam {
}
@@ -0,0 +1,11 @@
package com.ycjy.chAnLiZongJie.omponentScan.service.jdk;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ParamAnno {
}