当使用spring AOP时,判断目标方法上的注解进行相关操作,如缓存,认证权限等
自定义注解
packagecom.agent.annotation;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;importorg.springframework.stereotype.Component;
@Component
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)public @interfaceMyAnnotation {public boolean isEnable() default true;
}
Spring AOP的AspectJ
packagecom.agent.aop;importjava.lang.reflect.Method;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.springframework.stereotype.Component;importcom.agent.annotation.MyAnnotation;
@Component
@Aspectpublic classLogUtil {
@Around("@annotation(com.agent.annotation.MyAnnotation)")public Object logWrited(ProceedingJoinPoint point) throwsThrowable {
Object[] args=point.getArgs();
Class>[] argTypes = newClass[point.getArgs().length];for (int i = 0; i < args.length; i++) {
argTypes[i]=args[i].getClass();
}
Method method= null;try{
method=point.getTarget().getClass()
.getMethod(point.getSignature().getName(), argTypes);
}catch(NoSuchMethodException e) {
e.printStackTrace();
}catch(SecurityException e) {
e.printStackTrace();
}
MyAnnotation ma= method.getAnnotation(MyAnnotation.class);
System.out.println(ma.isEnable());returnpoint.proceed();
}
}
Service接口
packagecom.agent.service;public interfaceUserService {voidaddUser(String name, String password);
}
service接口的实现类,被自定义注解所注解
packagecom.agent.service.impl;importorg.springframework.stereotype.Service;importcom.agent.annotation.MyAnnotation;importcom.agent.service.UserService;
@Service(value="userService")public class UserServiceImpl implementsUserService {
@Override
@MyAnnotationpublic voidaddUser(String name, String password) {
System.out.println("UserServiceImpl.addUser()...... name: " + name + "; password: " +password);
}
}
测试类:
packagecom.agent.test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcom.agent.service.UserService;public classAOPTest {public static voidmain(String[] args) {
ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us= (UserService)ac.getBean("userService");
us.addUser("张三", "188");
}
}
Spring的配置文件:
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
测试结果:

如果使用的是接口的模式,而注解在实现类上,则不能使用如下方式获取目标方法的对象,因为该方式获取的是该类的接口或者顶级父类的方法的对象
MethodSignature methodSignature =(MethodSignature)point.getSignature();
Method method= methodSignature.getMethod();
本文介绍如何使用Spring AOP结合自定义注解实现方法级别的增强处理,包括日志记录等功能。通过具体示例展示了如何创建自定义注解,并在AOP切面中利用这些注解进行条件判断。

2万+

被折叠的 条评论
为什么被折叠?



