拦截器
package com.example.interceptor; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * @Author: sun * @Email: szw3366@126.com * @Date: 2017/12/5 * @Time: 13:17 * @Description: ${DESCRIPTION} */ public class MyMethodInterceptor implements MethodInterceptor{ @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("调用方法前调用"); Object o = methodInvocation.proceed(); System.out.println("调用方法后调用"); return o; } }
注册器
package com.example.interceptor; import org.aopalliance.aop.Advice; import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Author: sun * @Email: szw3366@126.com * @Date: 2017/12/5 * @Time: 13:20 * @Description: ${DESCRIPTION} */ @Configuration public class AppConfig { /** * 加载前注册 * @return */ @Bean public Advice myMethodInterceptor(){ return new MyMethodInterceptor(); } @Bean public BeanNameAutoProxyCreator beanNameAutoProxyCreator() { BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator(); beanNameAutoProxyCreator.setBeanNames("*userInfoService*"); beanNameAutoProxyCreator.setInterceptorNames("myMethodInterceptor"); return beanNameAutoProxyCreator; } }