生成一个包含@Pointcut,@Advice,@Aspect的AOP简单框架
时间: 2024-05-08 10:16:11 浏览: 92
简易的AOP框架
这里提供一个简单的AOP框架实现,包含@Pointcut,@Advice,@Aspect三个注解。
1. 定义@Pointcut注解
```java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Pointcut {
String value() default "";
}
```
2. 定义@Advice注解
```java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Advice {
String value() default "";
}
```
3. 定义@Aspect注解
```java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Aspect {
}
```
4. 定义切面类
```java
@Aspect
public class MyAspect {
@Pointcut("execution(* com.example.demo.service.*.*(..))")
public void pointcut() {}
@Advice("before")
public void beforeAdvice() {
System.out.println("before advice");
}
@Advice("after")
public void afterAdvice() {
System.out.println("after advice");
}
}
```
5. 定义目标类
```java
@Service
public class MyService {
public void doSomething() {
System.out.println("do something");
}
}
```
6. 实现AOP框架
```java
public class AopFramework {
public static void run(Class<?> targetClass) {
// 获取所有切面类
List<Class<?>> aspectClasses = getAspectClasses(targetClass);
// 创建所有切面类的实例
List<Object> aspectInstances = createAspectInstances(aspectClasses);
// 获取所有目标类的方法
Method[] targetMethods = targetClass.getDeclaredMethods();
// 遍历所有目标类的方法
for (Method targetMethod : targetMethods) {
// 遍历所有切面类
for (Object aspectInstance : aspectInstances) {
// 获取切点方法
Method pointcutMethod = getPointcutMethod(aspectInstance);
if (pointcutMethod != null) {
try {
// 调用切点方法
pointcutMethod.invoke(aspectInstance);
// 获取前置通知方法
Method beforeAdviceMethod = getBeforeAdviceMethod(aspectInstance);
if (beforeAdviceMethod != null) {
// 调用前置通知方法
beforeAdviceMethod.invoke(aspectInstance);
}
// 调用目标方法
targetMethod.invoke(targetClass.newInstance());
// 获取后置通知方法
Method afterAdviceMethod = getAfterAdviceMethod(aspectInstance);
if (afterAdviceMethod != null) {
// 调用后置通知方法
afterAdviceMethod.invoke(aspectInstance);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
private static List<Class<?>> getAspectClasses(Class<?> targetClass) {
List<Class<?>> aspectClasses = new ArrayList<>();
Package targetPackage = targetClass.getPackage();
String targetPackageName = targetPackage.getName();
String aspectPackageName = targetPackageName + ".aspect";
try {
Reflections reflections = new Reflections(aspectPackageName);
Set<Class<?>> annotatedClasses = reflections.getTypesAnnotatedWith(Aspect.class);
aspectClasses.addAll(annotatedClasses);
} catch (Exception e) {
e.printStackTrace();
}
return aspectClasses;
}
private static List<Object> createAspectInstances(List<Class<?>> aspectClasses) {
List<Object> aspectInstances = new ArrayList<>();
for (Class<?> aspectClass : aspectClasses) {
try {
Object aspectInstance = aspectClass.newInstance();
aspectInstances.add(aspectInstance);
} catch (Exception e) {
e.printStackTrace();
}
}
return aspectInstances;
}
private static Method getPointcutMethod(Object aspectInstance) {
Method[] methods = aspectInstance.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(Pointcut.class)) {
return method;
}
}
return null;
}
private static Method getBeforeAdviceMethod(Object aspectInstance) {
return getAdviceMethod(aspectInstance, "before");
}
private static Method getAfterAdviceMethod(Object aspectInstance) {
return getAdviceMethod(aspectInstance, "after");
}
private static Method getAdviceMethod(Object aspectInstance, String adviceType) {
Method[] methods = aspectInstance.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(Advice.class)) {
Advice advice = method.getAnnotation(Advice.class);
if (advice.value().equals(adviceType)) {
return method;
}
}
}
return null;
}
}
```
7. 测试AOP框架
```java
public class AopTest {
public static void main(String[] args) {
AopFramework.run(MyService.class);
}
}
```
输出结果:
```
before advice
do something
after advice
```
阅读全文