• springaopxml


    1.还是建立前面的依赖注入的例子上。
    2.导入aopalliance.jar   aspectjrt.jar  aspectjweaver.jar三个包
    3.新建LogAspect .java,注意此时的方法已经没有annotation(废话)。所以需要在bean.xml做配置
     
     
    package org.zttc.itat.spring.proxy;
     
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.springframework.stereotype.Component;
     
    @Component("logAspect")//让这个切面类被Spring所管理
    public class LogAspect {
     
    public void logStart(JoinPoint jp) {
    //得到执行的对象
    System.out.println(jp.getTarget());
    //得到执行的方法
    System.out.println(jp.getSignature().getName());
    Logger.info("加入日志");
    }
    public void logEnd(JoinPoint jp) {
    Logger.info("方法调用结束加入日志");
    }
     
    public void logAround(ProceedingJoinPoint pjp) throws Throwable {
    Logger.info("开始在Around中加入日志");
    pjp.proceed();//执行程序
    Logger.info("结束Around");
    }
     
    }
     
    4.bean.xml
     
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd
             http://www.springframework.org/schema/aop
             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
       <!-- 打开Spring的Annotation支持 -->
       <context:annotation-config/>
       <!-- 设定Spring 去哪些包中找Annotation -->
       <context:component-scan base-package="org.zttc.itat.spring"/>
       
       <aop:config>
       <!-- 定义切面 -->
       <aop:aspect id="myLogAspect" ref="logAspect">
       <!-- 在哪些位置加入相应的Aspect -->
       <aop:pointcut id="logPointCut" expression="execution(* org.zttc.itat.spring.dao.*.add*(..))||
       execution(* org.zttc.itat.spring.dao.*.delete*(..))||
       execution(* org.zttc.itat.spring.dao.*.update*(..))"/>
       <aop:before method="logStart" pointcut-ref="logPointCut"/>
       <aop:after method="logEnd" pointcut-ref="logPointCut"/>
       <aop:around method="logAround" pointcut-ref="logPointCut"/>
       </aop:aspect>
       </aop:config>
       
    </beans>
     
    一般的aop都用xml,不用annotion
     
     
  • 相关阅读:
    课堂例子验证
    大道至简第三章读后感
    动手动脑例子验证
    各数相加的思路、流程图、源代码及实现截图
    大道至简第二章读后感
    《大道至简》第一章读后感
    个人冲刺08
    个人冲刺07
    构建之法读后感04
    个人冲刺06
  • 原文地址:https://www.cnblogs.com/yujianjingjing/p/2953070.html
Copyright © 2020-2023  润新知