zoukankan      html  css  js  c++  java
  • AOP通过反射机制实现动态代理/IOC依赖注入

    功能:

      日志记录,事务处理

    简单描述:

      把几个类的共有代码,抽取到一个切片中,在运行时,动态地将代码切入到类的指定方法中。

    使用方法:

      通过AOP代理,被调用到InvocationHandler类的invoker方法执行
      配置:被代理接口,被代理接口实现类,各类拦截器

    代码实现:

     1 <!-- 配置事务管理器 -->
     2     <bean id="transactionManager"
     3           class="org.springframework.orm.hibernate4.HibernateTransactionManager">
     4         <property name="sessionFactory" ref="sessionFactory"/>
     5     </bean>
     6 
     7     <!-- 配置事务的传播特性 -->
     8     <tx:advice id="txAdvice" transaction-manager="transactionManager">
     9         <tx:attributes>
    10             <tx:method name="*" propagation="REQUIRED"/>
    11         </tx:attributes>
    12     </tx:advice>
    13 
    14     <!-- 那些类的哪些方法参与事务 -->
    15     <aop:config>
    16         <aop:pointcut id="allServiceMethod"
    17                       expression="execution(* com.selfpackage.service.*.*(..))"/>
    18         <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice"/>
    19     </aop:config><!-- 配置事务管理器 -->
    20     <bean id="transactionManager"
    21           class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    22         <property name="sessionFactory" ref="sessionFactory"/>
    23     </bean>
    24 
    25     <!-- 配置事务的传播特性 -->
    26     <tx:advice id="txAdvice" transaction-manager="transactionManager">
    27         <tx:attributes>
    28             <tx:method name="*" propagation="REQUIRED"/>
    29         </tx:attributes>
    30     </tx:advice>
    31 
    32     <!-- 那些类的哪些方法参与事务 -->
    33     <aop:config>
    34         <aop:pointcut id="allServiceMethod"
    35                       expression="execution(* com.selfpackage.service.*.*(..))"/>
    36         <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice"/>
    37     </aop:config>

     四、IOC

    IoC(控制反转),将类的创建和依赖关系写在配置文件里,由配置文件注入,实现了松耦合

    博客园:http://www.cnblogs.com/zhuziyu/
    Copyright ©2018 不是植物
    【转载文章务必保留出处和署名,谢谢!】
  • 相关阅读:
    静态方法和类方法
    DEL: Restore Boxes after VirtualBox Upgrade
    DEL: IE "Your current security settings put your computer at risk. Click h
    EV: Using GitHub Repository
    EV: Windows Commands 命令
    EV: Notepad++ Regular Express syntax
    html页面的三个width: document, window, screen
    DEL: View web content zone in IE9
    EV: 关于min-width样式的使用
    EV: Linux Shell Commands
  • 原文地址:https://www.cnblogs.com/zhuziyu/p/8544356.html
Copyright © 2011-2022 走看看