您的位置:首页 > 编程语言 > Java开发

用代码学习Spring:IoC、AOP(下)

2007-02-14 08:59 543 查看
用代码学习Spring:IoC、AOP(上)

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(上)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: