一.MethodInterceptor特點:
方法攔截器,它是一個接口,用于Spring AOP編程中的動態(tài)代理.實現(xiàn)該接口可以對需要增強的方法進行增強.
二.使用步驟:
1.增強類,如Arroud,實現(xiàn)MethodInterceptor接口,重寫invoke方法,在方法中對其進行額外功能增強.
2.applicationContext.xml配置文件中,進行切入點和切面的配置;并且把增強類和額外類交給Spring工廠.
三.代碼演示:
1.Arroud額外功能類:
/**
* spring動態(tài)代理之MethodInterceptor攔截器
* @param methodInvocation :額外功能增加的那個原始方法,如:register(),login()
* @return: 因為每個方法的返回值都不一樣,所以需要object類來接受
* @throws Throwable
*/
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("額外功能----");
//表示:該原始方法執(zhí)行了,如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]
額外功能----
注冊功能的核心代碼...
額外功能----
登錄功能的核心代碼...
額外功能----