日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發(fā)布時間:2025/6/15 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java获取方法上的注解_Spring:使用Spring AOP时,如何获取目标方法上的注解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

當(dāng)使用spring AOP時,判斷目標(biāo)方法上的注解進(jìn)行相關(guān)操作,如緩存,認(rèn)證權(quán)限等

自定義注解

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接口的實現(xiàn)類,被自定義注解所注解

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">

測試結(jié)果:

如果使用的是接口的模式,而注解在實現(xiàn)類上,則不能使用如下方式獲取目標(biāo)方法的對象,因為該方式獲取的是該類的接口或者頂級父類的方法的對象

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

Method method= methodSignature.getMethod();

總結(jié)

以上是生活随笔為你收集整理的java获取方法上的注解_Spring:使用Spring AOP时,如何获取目标方法上的注解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。