zoukankan      html  css  js  c++  java
  • Spring横切面(advice),增强(advisor),切入点(PointCut)(转)

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

    1.Spring管理事务有2种,其中一种是HibernateTransactionManager管理

    <bean id="txManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
        </bean>

    这样当定义切面时可以注入会话工厂属性如下:

    <!-- 配置事务处理的Bean,定义切面(advice) -->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="query*" read-only="true"/>
                <tx:method name="find*" read-only="true"/>
                <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
                <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
                <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
                <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
                <tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
            </tx:attributes>
        </tx:advice>

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

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

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

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

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

    <!-- 配置AOP -->
      <aop:config>
            <aop:pointcut id="daoMethod" expression="execution(public * com.dvp.module.*.*.*.dao.impl..*.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethod"/>
        </aop:config>

    <aop:config>
            <aop:pointcut id="daoMethod2" expression="execution(public * com.dvp.base.dao.impl..*.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethod2"/>
        </aop:config>

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

  • 相关阅读:
    RocketMQ性能压测分析(转载)
    利用Fiddler或Charles进行mock数据
    Linux中buffer/cache,swap,虚拟内存和page ++
    AVA 8 :从永久区(PermGen)到元空间(Metaspace)
    jstat 监控调整GC很好用
    Jmeter常用函数
    关于Oracle新建表空间,添加用户及新建表数据
    关于Oracle增加表空间大小方法
    关于手动删除Oracle数据数据,导致Oracle无法连接处理过程
    解决jquery easyui combotree(下拉树)点击文字无法展开下级菜单的解决方法
  • 原文地址:https://www.cnblogs.com/sandea/p/7069185.html
Copyright © 2011-2022 走看看