java获取方法上的注解_Spring:使用Spring AOP时,如何获取目标方法上的注解

本文介绍如何使用Spring AOP结合自定义注解实现方法级别的增强处理,包括日志记录等功能。通过具体示例展示了如何创建自定义注解,并在AOP切面中利用这些注解进行条件判断。

当使用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">

测试结果:

4f12c6a4e2d2acd7062a4cd907e5358f.png

如果使用的是接口的模式,而注解在实现类上,则不能使用如下方式获取目标方法的对象,因为该方式获取的是该类的接口或者顶级父类的方法的对象

MethodSignature methodSignature =(MethodSignature)point.getSignature();

Method method= methodSignature.getMethod();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值