用代码学习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类,观察控制台输出结果,重新审查代码,反思为什么会出现这种结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值