zoukankan      html  css  js  c++  java
  • Spring4之AOP

    [www.dev1234.com]一头扎进Spring4视频教程一头扎进Spring4源码[www.java1234.com]《一头扎进Spring4》第七讲 源码

    [www.dev1234.com]一头扎进Spring4视频教程一头扎进Spring4源码[www.java1234.com]《一头扎进Spring4》第八讲 源码 

    配置:以下重点划出的是新添加进去的 

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd">

    、、切面配置如下

        <bean id="studentServiceAspect" class="com.java1234.advice.StudentServiceAspect"></bean> //先创建好两个bean
        <bean id="studentService" class="com.java1234.service.impl.StudentServiceImpl"></bean>
        
        <aop:config>//config  aop标志
            <aop:aspect id="studentServiceAspect" ref="studentServiceAspect"> //aspect定义一个切面,studentServiceAspect是切面类
            //定义切点,是方法级别的,要写表达式{第一个*表示任何是任意,后面的两点..表示参数是任意的},指定方法
                <aop:pointcut expression="execution(* com.java1234.service.*.*(..))" id="businessService"/>
                <aop:before method="doBefore" pointcut-ref="businessService"/>
                <aop:after method="doAfter" pointcut-ref="businessService"/>
                <aop:around method="doAround" pointcut-ref="businessService"/>//环绕
                <aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>//返回
                <aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>//异常
            </aop:aspect> 
        </aop:config>
    package com.java1234.advice;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    
    public class StudentServiceAspect {
    
        public void doBefore(JoinPoint jp){
            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("类名:"+jp.getTarget().getClass().getName());
            System.out.println("方法名:"+jp.getSignature().getName());
            System.out.println("学生添加完成:"+jp.getArgs()[0]);
        }
        
        public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
            System.out.println("添加学生前");
            Object retVal=pjp.proceed();
            System.out.println(retVal);
            System.out.println("添加学生后");
            return retVal;
        }
        
        public void doAfterReturning(JoinPoint jp){
            System.out.println("返回通知");
        }
        
        public void doAfterThrowing(JoinPoint jp,Throwable ex){
            System.out.println("异常通知");
            System.out.println("异常信息:"+ex.getMessage());
        }
    }
  • 相关阅读:
    dd命令测试IO
    手工释放linux内存——/proc/sys/vm/drop_caches
    CNN中的卷积理解和实例
    Python map() 函数
    Python filter() 函数
    Python 用 os.walk 遍历目录
    在python中读写matlab文件
    人生, 不要在别扭的事上纠结
    javascript 最佳实践 ( 24 章 )
    CSS权威指南(第3版)
  • 原文地址:https://www.cnblogs.com/rogge7/p/7026041.html
Copyright © 2011-2022 走看看