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");
    
        }
    }
    

  • 相关阅读:
    反序列化中的对象逃逸——安洵杯2019 easy_serialize_php
    html中内联元素和块级元素
    Spring Boot 中使用WebJars
    @Valid Date 日期全局的格式化转换
    请使用“ MongoMappingContext#setAutoIndexCreation(boolean)”或覆盖“MongoConfigurationSupport#autoIndexCreation()”以明确
    Windows netstat 查看端口、进程占用
    Spring中@Async注解执行异步任务 & @Async Could not find unique TaskExecutor bean; NoUniqueBeanDefinitionException;
    Spring Data Mongodb的API及案例(exists、gt、in、is、orOperator 、regex、size)
    在java中使用JMH(Java Microbenchmark Harness)做性能测试
    Http option 请求是怎么回事
  • 原文地址:https://www.cnblogs.com/nidegui/p/11072014.html
Copyright © 2011-2022 走看看