zoukankan      html  css  js  c++  java
  • spring aop实例

    1,前置通知;

    2,后置通知;

    3,环绕通知;

    4,返回通知;

    5,异常通知;

        1.1定义一个接口

      

    package com.java.test6;
    
    /**
     * @author nidegui
     * @create 2019-06-23 9:40
     */
    public interface Student {
        public void addStudent(String name);
    }
    

    实现

    package com.java.test6;
    
    /**
     * @author nidegui
     * @create 2019-06-23 9:41
     */
    public class StudentImpl implements  Student {
        @Override
        public void addStudent(String name) {
            System.out.println("添加学生"+name);
        }
    }
    

      

    在添加学生之前添加一个切点

    package com.java.test6;

    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;

    /**
    * @author nidegui
    * @create 2019-06-23 9:45
    */
    public class StudentServiceAspect {
    //前置通知,在方法之前通知
    public void before(JoinPoint jp){
    System.out.println("类名:"+jp.getTarget().getClass().getName());
    System.out.println("方法名:"+jp.getSignature().getName());
    System.out.println("开始添加学生:"+jp.getArgs()[0]);

    System.out.println("开始添加学生");
    }
      //后置通知
    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());
    }


    }

      

    在配置文件中配置

    <?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="studentImpl" class="com.java.test6.StudentImpl"></bean>
    <bean id="studentAcpect" class="com.java.test6.StudentServiceAspect"></bean>

    <aop:config>
    <aop:aspect id="studentAcpect" ref="studentAcpect">
    <!--定义一个切点-->
    <aop:pointcut id="b" expression="execution(* com.java.test6.*.*(..))"></aop:pointcut>
    <!--定义前置通知-->
    <aop:before method="before" pointcut-ref="b"></aop:before>
    <!--后置通知-->
    <aop:after method="doAfter" pointcut-ref="b"></aop:after>
    <!--环绕通知-->
    <aop:around method="doAround" pointcut-ref="b"/>
    <!--返回通知-->
    <aop:after-returning method="doAfterReturning" pointcut-ref="b"/>
    <!--异常通知-->
    <aop:after-throwing method="doAfterThrowing" pointcut-ref="b" throwing="ex"/>

    </aop:aspect>
    </aop:config>
    </beans>

      

    测试:

    package com.java.test6;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author nidegui
     * @create 2019-06-22 14:47
     */
    public class Test {
        public static void main(String[] args) {
           ApplicationContext ac = new ClassPathXmlApplicationContext("beanss.xml");
    
            Student people =(Student) ac.getBean("studentImpl");
            people.addStudent("zhangsna");
    
        }
    }
    

  • 相关阅读:
    AutoCAD 2012安装错误,与.net framework (1603错误)以及ms2005vc++的问题。
    [转载]Thrift的C#入门Demo_图文详解版
    ASPX的Timer位置没放正确,导致整页刷新,而不是UpdatePanel里的内容刷新。
    div、ul、li等无法居中问题,text-align无效 margin auto无效
    flash导入图片缩放后出现毛边、失真、锯齿、文字模糊不清晰的情况
    table居中方法之一:设置width,然后为style设置margin:auto
    ZR普转提day1
    从“四人过桥”到“N人过桥”
    Luogu P2375 [NOI2014]动物园
    Luogu P3435 [POI2006]OKR-Periods of Words
  • 原文地址:https://www.cnblogs.com/nidegui/p/11072014.html
Copyright © 2011-2022 走看看