Jimoshi_Spring 框架学习(二)--AOP(面向切面)、AOP管理事务

Jimoshi成长经历:前面的笔记后面再慢慢整理-------方便自己

目录:AOP(面向切面)、AOP管理事务、

Spring 框架学习(二):

一、AOP(面向切面)

  1、AOP 目的:在不改变业务逻辑的前提下,横向业务逻辑进行纵向的额外的切入一些功能

  2、主要能够实现的功能:日志记录,性能统计,安全控制,事务处理,异常处理等等

  3、Spring AOP :在业务流程上面进行所谓的通知:1.前置通知 2.后置通知 3.环绕通知 4.返回通知 5.异常通知

  4、使用AOP,进行切面额外加入功能

    4.1.引入相关的jar包(pom.xml)

    代码示例:

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>4.0.6.RELEASE</version>
    </dependency>

    4.2.在(com.zr.service)中创建UserService接口

    代码示例:

     public interface UserService {
    /**
     * 添加用户
     * @param name 传入用户名字
     * @return 返回用户名字
     */
        public String addUser(String name);
      }

    4.3.在(com.zr.service.impl)中创建UserServiceImpl类实现UserService接口

    代码示例:

     public class UserServiceImpl implements UserService{
    public String addUser(String name) {
        // TODO Auto-generated method stub
        //加入所谓的日志
        //System.out.println("开始记录日志");
        System.out.println("添加用户");
        //加入日志
        //System.out.println("日志记录完毕");
        return name;
    }
      }

     4.4.创建(com.zr.other包)创建切面类(UserServiceAop)

     代码示例:

     public class UserServiceAop {
          //前置通知方法
        public void doBefore(JoinPoint jp) {
            System.out.println("doBefore");
            System.out.println("类名"+jp.getTarget().getClass().getName());
            System.out.println("方法名"+jp.getSignature().getName());
            System.out.println("参数"+jp.getArgs()[0]);
        }
        //后置通知方法
        public void doAfter(JoinPoint jp) {
            System.out.println("doAfter");
            System.out.println("类名"+jp.getTarget().getClass().getName());
            System.out.println("方法名"+jp.getSignature().getName());
            System.out.println("参数"+jp.getArgs()[0]);
        }
        }

     4.5.配置bean.xml文件

     代码示例:

     <!-- 注入service -->
     <bean id="userService" class="com.zr.service.impl.UserServiceImpl"></bean>
     <!-- 注入userServiceAop -->
     <bean id="userServiceAop" class="com.zr.other.UserServiceAop"></bean>
     <!-- 切面和切点的配置 -->
     <aop:config>
      <!-- 定义切面 -->
      <aop:aspect id="userAop" ref="userServiceAop">
      <!-- 定义切点 -->
      <aop:pointcut id="ywService" expression="execution(* com.zr.service.impl.*.*(..))"/>
          <!-- 定义切面的规则 -->
          <aop:before method="doBefore" pointcut-ref="ywService"/>
            <aop:after method="doAfter" pointcut-ref="ywService"/>
      </aop:aspect>
     </aop:config>

     4.6.测试(Test包中添加测试类)

     代码示例:

     public class TestAop {
    @Test
    public void testAop(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        UserService us = (UserService) ac.getBean("userService");
        us.addUser("zhj");
    }
      }

     4.7.运行结果
    
     代码示例:

     dobefore
     类名com.zr.service.impl.UserServiceImpl
     方法名addUser
     参数zhj
     ------------------添加用户
     doAfter
     类名com.zr.service.impl.UserServiceImpl
     方法名addUser
     参数zhj

二、AOP管理事务

   1、事务在处理数据:持久性,一致性,隔离型,原子性

   2、事务在处理数据可能会产生情况:第一类更新丢失,脏读,虚读,不可重复读,第二类丢失更新

   3、怎么解决这种情况产生:乐观锁(时间戳或者版本号) 悲观锁(for update)
   Spring 管理事务:一种使用编程式事务在服务定义事务类,粒度细),一种使用声明式事务(注解或者是XML配置,粒度粗)
   
   管理事务xml文件,

   代码示例:

    <!-- 配置事务管理器 -->  
    <bean id="transactionManager"  
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean>  
 
    <!-- 配置事务通知属性 -->  
    <tx:advice id="txAdvice" transaction-manager="transactionManager">  
        <!-- 定义事务传播属性 -->  
        <tx:attributes>  
            <tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" />  
        </tx:attributes>  
    </tx:advice>  
      
   
    <!-- 配置事务切面 -->  
    <aop:config>  
        <aop:pointcut id="serviceOperation"  
            expression="execution(* com.zr.service..*.*(..))" />  
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
    </aop:config>  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值