Spring:使用Spring AOP时,如何获取目标方法上的注解
Posted 愤怒的绿萝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring:使用Spring AOP时,如何获取目标方法上的注解相关的知识,希望对你有一定的参考价值。
当使用spring AOP时,判断目标方法上的注解进行相关操作,如缓存,认证权限等
自定义注解
package com.agent.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.stereotype.Component; @Component @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { public boolean isEnable() default true; }
Spring AOP的AspectJ
package com.agent.aop; import java.lang.reflect.Method; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import com.agent.annotation.MyAnnotation; @Component @Aspect public class LogUtil { @Around("@annotation(com.agent.annotation.MyAnnotation)") public Object logWrited(ProceedingJoinPoint point) throws Throwable { Object[] args = point.getArgs(); Class<?>[] argTypes = new Class[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()); return point.proceed(); } }
Service接口
package com.agent.service; public interface UserService { void addUser(String name, String password); }
service接口的实现类,被自定义注解所注解
package com.agent.service.impl; import org.springframework.stereotype.Service; import com.agent.annotation.MyAnnotation; import com.agent.service.UserService; @Service(value="userService") public class UserServiceImpl implements UserService { @Override @MyAnnotation public void addUser(String name, String password) { System.out.println("UserServiceImpl.addUser()...... name: " + name + "; password: " + password); } }
测试类:
package com.agent.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.agent.service.UserService; public class AOPTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService us = (UserService)ac.getBean("userService"); us.addUser("张三", "188"); } }
Spring的配置文件:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans 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"> <context:component-scan base-package="com.agent" /> <!-- <bean id="aspect" class="com.agent.aop.LogUtil" /> <aop:config> <aop:aspect ref="aspect"> <aop:pointcut expression="execution(* add*(..))" id="mypointcut"/> <aop:after method="logWrited" pointcut-ref="mypointcut"/> <aop:around method="logWrited" pointcut-ref="mypointcut" /> </aop:aspect> </aop:config> --> <aop:aspectj-autoproxy/> </beans>
测试结果:
如果使用的是接口的模式,而注解在实现类上,则不能使用如下方式获取目标方法的对象,因为该方式获取的是该类的接口或者顶级父类的方法的对象
MethodSignature methodSignature = (MethodSignature)point.getSignature();
Method method = methodSignature.getMethod();
以上是关于Spring:使用Spring AOP时,如何获取目标方法上的注解的主要内容,如果未能解决你的问题,请参考以下文章
Spring中的AOP——在Advice方法中获取目标方法的参数
spring---aop---Spring AOP的拦截器链