参考Shiro的自定义AOP横切模式
Posted BINGJJFLY
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了参考Shiro的自定义AOP横切模式相关的知识,希望对你有一定的参考价值。
调用Demo
package com.wjz.demo; import org.springframework.aop.framework.ProxyFactory; import com.wjz.spring.CustomPointcutAdvisor; public class AopDemo { public static void main(String[] args) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisors(new CustomPointcutAdvisor()); proxyFactory.setTarget(new Foo()); Foo fooProxy = (Foo) proxyFactory.getProxy(); fooProxy.foo("wjz"); } }
PointcutAdvisor切入点
package com.wjz.spring; import java.lang.reflect.Method; import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; /** * 自定义方法切入点 * @author wjz * */ @SuppressWarnings("serial") public class CustomPointcutAdvisor extends StaticMethodMatcherPointcutAdvisor { public CustomPointcutAdvisor() { setAdvice(new CustomMethodInterceptor()); } @Override public boolean matches(Method method, Class<?> targetClass) { if ("foo".equals(method.getName())) { return true; } return false; } }
MethodInterceptor方法拦截器
package com.wjz.spring; import java.lang.reflect.Method; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * 自定义方法拦截器 * @author wjz * */ public class CustomMethodInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("invoke method before"); Object arg = invocation.getArguments()[0]; System.out.println("arg " + arg); Object ti = invocation.getThis(); System.out.println("this " + ti); Method method = invocation.getMethod(); System.out.println("method " + method); System.out.println("toString " + toString()); Object result = invocation.proceed(); System.out.println("invoke method after"); return result; } }
以上是关于参考Shiro的自定义AOP横切模式的主要内容,如果未能解决你的问题,请参考以下文章