Spring动态代理之MethodInterceptor拦截器详解

一.MethodInterceptor特点:

方法拦截器,它是一个接口,用于Spring AOP编程中的动态代理.实现该接口可以对需要增强的方法进行增强.

二.使用步骤:

1.增强类,如Arroud,实现MethodInterceptor接口,重写invoke方法,在方法中对其进行额外功能增强.
2.applicationContext.xml配置文件中,进行切入点和切面的配置;并且把增强类和额外类交给Spring工厂.

三.代码演示:
1.Arroud额外功能类:


    /**
     * spring动态代理之MethodInterceptor拦截器
     * @param methodInvocation :额外功能增加的那个原始方法,如:register(),login()
     * @return: 因为每个方法的返回值都不一样,所以需要object类来接受
     * @throws Throwable
     */
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("额外功能----");
        //表示:该原始方法执行了,如register,login方法
        Object proceed = methodInvocation.proceed();
        return proceed;
    }
}

2.配置文件ApplicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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 https://www.springframework.org/schema/aop/spring-aop.xsd">


    <bean class="com.baizhiedu.proxy.UserServiceImpl" id="service"></bean>


<!--    1.额外功能-->
<!--    <bean class="com.baizhiedu.dynamic.Before" id="before"></bean>-->
    <bean class="com.baizhiedu.dynamic.Arroud" id="arroud"></bean>

    <aop:config>
<!--  2. 配置切入点-->
        <aop:pointcut id="pc" expression="execution(* *(..))"/>
        <!--3.配置切面,组合,组合什么? 整合额外功能-->
        <aop:advisor advice-ref="arroud" pointcut-ref="pc"/>
    </aop:config>
</beans>

3.测试代码:

    public void test(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext5.xml");
        UserService service = (UserService) ctx.getBean("service");
        service.register("Nisy",10);
        UserService bean = (UserService) ctx.getBean("service");
        bean.login(new User());
        System.out.println(service);
        System.out.println(bean);
    }

4.效果:

2020-06-05 13:39:22 DEBUG ClassPathXmlApplicationContext:590 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@25618e91
2020-06-05 13:39:23 DEBUG XmlBeanDefinitionReader:395 - Loaded 5 bean definitions from class path resource [applicationContext5.xml]
2020-06-05 13:39:23 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator'
2020-06-05 13:39:23 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'service'
2020-06-05 13:39:23 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
2020-06-05 13:39:23 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'arroud'
2020-06-05 13:39:23 DEBUG AspectJAwareAdvisorAutoProxyCreator:521 - Creating implicit proxy for bean 'service' with 0 common interceptors and 2 specific interceptors
2020-06-05 13:39:23 DEBUG JdkDynamicAopProxy:118 - Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [com.baizhiedu.proxy.UserServiceImpl@649bec2e]
额外功能----
注册功能的核心代码...
额外功能----
登录功能的核心代码...
额外功能----