Spring横切面(advice),增强(advisor),切入点(PointCut)

关于Spring横切面(advice),增强(advisor),切入点(PointCut)的一点理解:

1.Spring管理事务由2中,其中一种是HibernateTransactionManager管理,这样当定义切面时可以注入会话工厂属性

如下:

<!-- 配置事务处理的Bean,定义切面 -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

2.对数据库的操作都属于事务的操作,而数据库默认是读已提交 read-only="true"形式

 当读写操作时会调用DAO方法,而操作DAO的核心业务是Service,即 Spring,然而Spring

要对调用DAO的方法加以控制,所以就产生了切面(advice)//这个切面相当于一道防护门。

如下:

<!-- 配置连接点 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="mod*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>

3.切面有了,Spring自己会找到应该执行哪些DAO里的方法了,(因为DAO已经注入了Spring中)

 所以自身的防护就变得复杂了,所以需要紧密耦合,自然增强就产生了 。如下:

<!-- 配置AOP -->
    <aop:config>
        <aop:pointcut id="txPointcut"
            expression="execution(* com.hello.service.*.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
     </aop:config>

//OK所以一个Spring增强(advisor)=切面(advice)+切入点(PointCut)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值