7 建立配置文件组织上面的类之间的关系,AOP有切入点和增强这两个重要的概念,把两个概念结合到一起,就是一个在某个方法执行的时候附加执行,切入点表示 在哪里附加,增强表示附加什么,配置文件中的myPointcut表示切入点,myInterceptor表示增强的内容,myAdvisor表示增强 器, 即两者的结合,在bo这个bean中,我们把这个增强器附加到了bo这个bean上。
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <bean id="businessObjectImpl" class="BusinessObjectImpl"> <property name="words"> <value>正在执行业务方法</value> </property> </bean> <bean id="myInterceptor" class="MyInterceptor"> <property name="before"> <value>执行业务方法前</value> </property> <property name="after"> <value>执行业务方法后</value> </property> </bean> <bean id="myPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <property name="patterns"> <list> <value>BusinessObject.doSomething</value> </list> </property> </bean> <bean id="myAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="pointcut" ref="myPointcut"/> <property name="advice" ref="myInterceptor"/> </bean> <bean id="bo" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <ref local="businessObjectImpl"/> </property> <property name="proxyInterfaces"> <value>BusinessObject</value> </property> <property name="interceptorNames"> <list> <value>myInterceptor</value> <value>myAdvisor</value> </list> </property> </bean></beans><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <bean id="businessObjectImpl" class="BusinessObjectImpl"> <property name="words"> <value>正在执行业务方法</value> </property> </bean> <bean id="myInterceptor" class="MyInterceptor"> <property name="before"> <value>执行业务方法前</value> </property> <property name="after"> <value>执行业务方法后</value> </property> </bean> <bean id="myPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <property name="patterns"> <list> <value>BusinessObject.doSomething</value> </list> </property> </bean> <bean id="myAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="pointcut" ref="myPointcut"/> <property name="advice" ref="myInterceptor"/> </bean> <bean id="bo" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <ref local="businessObjectImpl"/> </property> <property name="proxyInterfaces"> <value>BusinessObject</value> </property> <property name="interceptorNames"> <list> <value>myInterceptor</value> <value>myAdvisor</value> </list> </property> </bean></beans>
8 运行Main类,观察控制台输出结果,重新审查代码,反思为什么会出现这种结果。
用代码学习Spring:IoC、AOP(上)