学习SpringAOP
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
@Configuration 声明配置类, 什么配置类?给谁声明?
|
||||
|
||||
@EnableAspectJAutoProxy 声明SpringAOP自动代理, 为什么要声明自动代理, 不声明会怎么样?声明了之后又做了什么?
|
||||
|
||||
@ComponentScan扫描指定包下Spring组件, 扫描到了组件(组件是类吗?)才能拿到组件才能读取是否有注解?
|
||||
|
||||
@Service声明服务层Bean本质是@Component, 所以相当于Service调用Component?只是说用Service写在业务类上更好区分是普通类还是业务类?
|
||||
|
||||
@Component声明普通SpringBean就是让被声明的类被Spring管理起来吗?相当于xml里的<bean name="" class="">?
|
||||
|
||||
@Aspect声明切面类, 声明了之后Spring就知道这个类是切面的?
|
||||
|
||||
Impl在方法末尾调用了Impl2的doSth2, 为什么被调用的方法没有切?
|
||||
|
||||
CGLIB 生成子类代理, CGLIB是什么? 子类代理是动态代理?Spring自动生成在内存中的?
|
||||
CGLIB是子类代理, 和动态代理是一个东西吗?动态代理是JDK动态代理?为什么没有接口用不了JDK动态代理?
|
||||
|
||||
想要使用切面就得需要pom.xml:
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
</dependency>
|
||||
那这个依赖是Spring官方的依赖还是别的?不使用这个依赖能否实现切面?
|
||||
|
||||
|
||||
|
||||
2. 不声明就相当于没开启S'p'ri'n'gAOP?
|
||||
3. 调用代理对象的方法时,先执行通知,再调用目标方法。是什么意思
|
||||
4. 就是说一个类, 首先要得是一个Bean, Spring扫描到了之后, SpringAOP解析阶段就识别Aspect就能识别到这个 Bean 是一个切面?
|
||||
5.
|
||||
@@ -0,0 +1,170 @@
|
||||
SpringAOP切点指示符
|
||||
|
||||
1. execution -- 匹配连接点方法
|
||||
这是最核心最精确的匹配方式, 语法:
|
||||
execution(修饰符 返回类型 包.类.方法名(参数) throws 异常)
|
||||
* 任意返回类型/任意字符串
|
||||
.. 在包路径里表示任意层级, 在参数里表示任意参数和任意类型
|
||||
|
||||
2. within -- 在什么之内
|
||||
匹配某个类型或包下所有类里的方法
|
||||
within(包.类) 类里所有方法
|
||||
within(包..*) 包下所有类的所有方法
|
||||
|
||||
3. this -- 代理对象类型匹配
|
||||
TODO: 后面再说, 切接口不切实现
|
||||
4. target -- 目标对象类型匹配
|
||||
TODO: 后面再说, 切接口切实现
|
||||
5. args -- 方法参数类型匹配
|
||||
6. @target —— 目标类上有注解
|
||||
目标类上写上注解, @target(包.注解)
|
||||
7. @args -- 运行时实参类型上有注解
|
||||
说人话就是看传进去的参数上方有没有加注解, 加了就切, 具体看案例
|
||||
8. @within —— 方法所在类上有注解
|
||||
和@target差不多
|
||||
9. annotation —— 方法上有注解
|
||||
被调用的那个方法, 头上有没有贴某个注解
|
||||
10. bean -- 某个SpringBean的所有方法
|
||||
|
||||
|
||||
切点逻辑表达式
|
||||
&& 并且两个条件成立切
|
||||
|| 任意一个成立切
|
||||
! 反转
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ps:
|
||||
二、逐条解释老师的意思
|
||||
1. execution —— 匹配连接点方法
|
||||
最核心、最精确的匹配方式。语法:
|
||||
|
||||
text
|
||||
execution(修饰符 返回类型 包.类.方法名(参数) throws 异常)
|
||||
* 任意返回类型/任意字符串
|
||||
|
||||
.. 在包路径里表示「任意层级」,在参数里表示「任意个任意类型参数」
|
||||
|
||||
你的 execution(void com.ycjy.ch01.a.Impl.*()) 就是「Impl 类下所有无参返回 void 的方法」
|
||||
|
||||
2. within —— 「在……之内」
|
||||
匹配某个类型或包下所有类里的方法。它只看到「类型」这一层,不看方法签名。
|
||||
|
||||
java
|
||||
within(com.a.UserService) // UserService 类里所有方法
|
||||
within(com.a.service..*) // service 包及其子包下所有类
|
||||
对比:execution 精确到方法,within 只到类/包。
|
||||
|
||||
3. this —— 代理对象类型匹配
|
||||
this(Type) 判断的是 Spring 生成的代理对象是不是 Type 的实例。
|
||||
|
||||
用 JDK 动态代理时,代理对象实现了接口,所以 this(接口) 能匹配。
|
||||
|
||||
用 CGLIB 时,代理对象是目标类的子类。
|
||||
|
||||
⚠️ 注意:this 判断的是「代理对象」,不是原始对象。
|
||||
|
||||
4. target —— 目标对象类型匹配
|
||||
target(Type) 判断的是被代理的原始目标对象是不是 Type 的实例。
|
||||
|
||||
this 和 target 容易混:
|
||||
|
||||
this → 看代理
|
||||
|
||||
target → 看本体
|
||||
|
||||
大多数情况下两者结果一样;但在「引入(Introduction)」场景下会有区别(代理对象可能实现了目标对象没实现的接口)。
|
||||
|
||||
5. args —— 方法参数类型匹配
|
||||
args(Type1, Type2) 匹配「参数类型依次是 Type1、Type2」的方法。支持通配。
|
||||
|
||||
java
|
||||
args(java.lang.String) // 一个 String 参数
|
||||
args(java.lang.String, ..) // 第一个是 String,后面任意
|
||||
args(..) // 任意参数(也常用于表示不限)
|
||||
⚠️ args 匹配的是运行时参数类型,不是声明类型。
|
||||
|
||||
6. @target —— 目标类上有注解
|
||||
@target(Anno) 匹配「目标对象所属的类带有 Anno 注解」的所有方法。
|
||||
|
||||
java
|
||||
@target(org.springframework.stereotype.Service)
|
||||
→ 所有被 @Service 标注的类里的方法都被匹配。
|
||||
|
||||
7. @args —— 运行时实参类型上有注解
|
||||
@args(Anno) 匹配「方法实际传入的参数的类型上带有 Anno 注解」。
|
||||
|
||||
这个比较少用。比如你有个参数类型被 @MyAnno 标注,方法调用时传了这种参数,才会匹配。
|
||||
|
||||
8. @within —— 方法所在类上有注解
|
||||
@within(Anno) 匹配「声明该方法的类上带有 Anno 注解」。
|
||||
|
||||
和 @target 的区别:
|
||||
|
||||
@target 看的是运行时目标对象的类
|
||||
|
||||
@within 看的是方法声明时所在的类(更偏静态,且不含从父类继承的方法)
|
||||
|
||||
实际用起来,@within 和 @target 常常效果相近,但语义和边界(继承、接口)不同。
|
||||
|
||||
9. @annotation —— 方法上有注解
|
||||
@annotation(Anno) 匹配「连接点方法自身带有 Anno 注解」。
|
||||
|
||||
这个很常用!比如自定义一个 @Loggable 注解,哪个方法想被记录日志就标上,切点写 @annotation(com.a.Loggable)。这样比 execution 写死包名类名灵活得多。
|
||||
|
||||
10. bean —— 某个 Spring Bean 的所有方法
|
||||
bean(userService) 匹配「Spring 容器里名字叫 userService 的 bean」的所有方法。支持通配:
|
||||
|
||||
java
|
||||
bean(*Service) // 名字以 Service 结尾的 bean
|
||||
⚠️ 这是 Spring AOP 特有的,不是 AspectJ 标准的一部分。
|
||||
|
||||
三、几个关键辨析(老师可能想考的)
|
||||
execution vs within
|
||||
|
||||
execution 细到方法签名
|
||||
|
||||
within 只到类型/包这一层,粒度粗
|
||||
|
||||
this vs target
|
||||
|
||||
this = 代理对象
|
||||
|
||||
target = 目标对象
|
||||
|
||||
@target vs @within
|
||||
|
||||
@target = 目标对象运行时类上的注解
|
||||
|
||||
@within = 声明方法的类上的注解
|
||||
|
||||
@args vs args
|
||||
|
||||
args 看参数类型
|
||||
|
||||
@args 看参数类型上带的注解
|
||||
|
||||
@annotation 是最实用的自定义切点方式——配合自定义注解,可以做到「想切哪个方法就在方法上标一个注解」。
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<parent>
|
||||
<groupId>com.linlangjz</groupId>
|
||||
<artifactId>SpringStudy</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>SpringFramework-Aop</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
</dependency>
|
||||
<!-- MySql -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
<!-- MyBatis -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
</dependency>
|
||||
<!-- servlet -->
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -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 {
|
||||
}
|
||||
+29
@@ -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###");
|
||||
}
|
||||
}
|
||||
+85
@@ -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###做了一些事情");
|
||||
}
|
||||
}
|
||||
+15
@@ -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");
|
||||
}
|
||||
}
|
||||
+11
@@ -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);
|
||||
}
|
||||
}
|
||||
+14
@@ -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");
|
||||
}
|
||||
}
|
||||
+14
@@ -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");
|
||||
}
|
||||
}
|
||||
+23
@@ -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 收到参数");
|
||||
}
|
||||
}
|
||||
+9
@@ -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);
|
||||
}
|
||||
+21
@@ -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 收到参数");
|
||||
}
|
||||
}
|
||||
+11
@@ -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 {
|
||||
}
|
||||
+11
@@ -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 {
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.ycjy.chAnLiZongJie.omponentScan.service.jdk;
|
||||
|
||||
@ParamAnno
|
||||
public class MyParam {
|
||||
}
|
||||
+11
@@ -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 {
|
||||
}
|
||||
@@ -13,6 +13,7 @@
|
||||
<module>SpringFramework-ioc</module>
|
||||
<module>SpringFramework-Lifecycle</module>
|
||||
<module>SpringFramework-ZY-V1</module>
|
||||
<module>SpringFramework-Aop</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
@@ -26,6 +27,7 @@
|
||||
<servlet.version>6.1.0</servlet.version>
|
||||
<jackson.version>2.17.2</jackson.version>
|
||||
<druid.version>1.2.24</druid.version>
|
||||
<aspectj.version>1.9.25.1</aspectj.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
@@ -78,6 +80,11 @@
|
||||
<artifactId>druid</artifactId>
|
||||
<version>${druid.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>${aspectj.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
</project>
|
||||
Reference in New Issue
Block a user