17Aspectij

17Aspectij-2018/07/31

  • 1.Aspectj基于xml
    • 前置通知
      • method : 通知,及方法名
      • pointcut :切入点表达式,此表达式只能当前通知使用。
      • pointcut-ref : 切入点引用,可以与其他通知共享切入点。
      • 通知方法格式:public void myBefore(JoinPoint joinPoint){参数1:org.aspectj.lang.JoinPoint } 用于描述连接点(目标方法),获得目标方法名等
    • 后置通知:目标方法后执行,获得返回值
      • 通知方法格式:public void myAfterReturning(JoinPoint joinPoint,Object ret){
        • 参数1:连接点描述
        • 参数2:类型Object,参数名 returning="ret" 配置的
    • 环绕通知
      • 通知方法格式:public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
        • 返回值类型:Object
        • 方法名:任意
        • 参数:org.aspectj.lang.ProceedingJoinPoint
        • 抛出异常
      • 执行目标方法:Object obj = joinPoint.proceed();
    • 抛出异常
      • <aop:after-throwing method="" pointcut-ref="" throwing=""/>
        • throwing :通知方法的第二个参数名称
      • 通知方法格式:public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
        • 参数1:连接点描述对象
        • 参数2:获得异常信息,类型Throwable ,参数名由throwing="e" 配置
  • 2.注解在方法前面注解
    • 声明公共切入点
      • @Pointcut("execution(* com.itheima.daspect.banno.UserServiceImpl.*(..))")private void myPointCut(){}
    • @Before(value="myPointCut()")value可以省略
    • @AfterReturning(value="myPointCut()" ,returning="ret")
    • @Around("myPointCut()")
    • @AfterThrowing(value="execution(* com.itheima.daspect.banno.UserServiceImpl.*(..))" ,throwing="e")
  • 3.JdbcTemplate
    • 创建数据源(连接池) dbcp 
      BasicDataSource dataSource = new BasicDataSource(); 
      // 基本4项
      dataSource.setDriverClassName("com.mysql.jdbc.Driver");
      dataSource.setUrl("jdbc:mysql:
      //localhost:3306/ee19springday02");
      dataSource.setUsername("root");
      dataSource.setPassword("1234");
    • 创建模板 
      JdbcTemplate jdbcTemplate = new JdbcTemplate();
      jdbcTemplate.setDataSource(dataSource);
    • 通过api操作 
      jdbcTemplate.update("insert into t_user(username,password) values(?,?);", "tom","998");
  • 4.传播行为:在两个业务之间如何共享事务
    • PROPAGATION_REQUIRED , required , 必须 【默认值】 支持当前事务,A如果有事务,B将使用该事务。 如果A没有事务,B将创建一个新的事务。
    • PROPAGATIONREQUIRESNEW , requires_new ,必须新的 如果A有事务,将A的事务挂起,B创建一个新的事务 如果A没有事务,B创建一个新的事务
    • PROPAGATION_NESTED ,nested ,嵌套 A和B底层采用保存点机制,形成嵌套事务。
  • 5.手动管理事务
    • 1.service 需要获得 TransactionTemplate
    • 2.spring 配置模板,并注入给service
    • 3.模板需要注入事务管理器
    • 4.配置事务管理器:DataSourceTransactionManager ,需要注入DataSource
posted @ 2018-08-29 21:54  菜白小系瓦  阅读(126)  评论(0编辑  收藏  举报