码迷,mamicode.com
首页 > 其他好文 > 详细

参考Shiro的自定义AOP横切模式

时间:2018-05-21 12:30:28      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:print   ext   cto   stat   span   set   inter   nbsp   ntc   

调用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横切模式

标签:print   ext   cto   stat   span   set   inter   nbsp   ntc   

原文地址:https://www.cnblogs.com/BINGJJFLY/p/9065798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!