javascript
java获取方法上的注解_Spring:使用Spring 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">
測試結果:
如果使用的是接口的模式,而注解在實現類上,則不能使用如下方式獲取目標方法的對象,因為該方式獲取的是該類的接口或者頂級父類的方法的對象
MethodSignature methodSignature =(MethodSignature)point.getSignature();
Method method= methodSignature.getMethod();
總結
以上是生活随笔為你收集整理的java获取方法上的注解_Spring:使用Spring AOP时,如何获取目标方法上的注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java webstart 自动升级_w
- 下一篇: c改java_Android NDK开发