• 注解配置AOP切面编程


    1、导入先关jar包

    2、编写applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
    	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
    	xmlns:cache="http://www.springframework.org/schema/cache"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    	
    	<context:component-scan base-package="com.wh.aop"></context:component-scan> 
    	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    	
    </beans>  

    3、编写被切的类

    package com.wh.aop;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Computer {
    
    	public void play01(){
    		System.out.println("play01玩家");
    	}
    	
    	public String play02(){
    		System.out.println("play02玩家");
    		return "play02";
    	}
    	
    	public String play03(){
    		System.out.println("play03玩家");
    		return "play03"+(10/0);
    	}
    	
    	public String play04(){
    		System.out.println("play04玩家");
    		return "play04";
    	}
    	
    	
    }
    

    4、编写切面类

    package com.wh.aop;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    @Component
    @Aspect
    public class AopProxy {
    	
    	@Pointcut("execution(* com.wh.aop.Computer.play01(..))")
    	public void cp(){}
    	
    	@Before("cp()")
    	public void doBefore(JoinPoint p){
    		System.out.println("前置通知!");
    	}
    	
    	@AfterReturning(value="execution(* com.wh.aop.Computer.play02())",returning="result")
    	public void doAfterReturning(JoinPoint p,Object result){
    		System.out.println("后置通知   "+result);
    	}
    	
    	@After("execution(* com.wh.aop.Computer.play02())")
    	public void doAfter(JoinPoint p){
    		System.out.println("最终通知   ");
    	}
    	
    	@AfterThrowing(value="execution(* com.wh.aop.Computer.play03())",throwing="e")
    	public void doAfterThrowing(JoinPoint p,Throwable e){
    		System.out.println("异常通知:   "+e);
    	}
    	
    	@Around("execution(* com.wh.aop.Computer.play04())")
    	public void doAround(ProceedingJoinPoint p){
    		System.out.println("前置通知");
    		Object obj=null;
    		try { 
    			obj=p.proceed();
    		} catch (Throwable e) {
    			System.out.println("异常通知:   "+e.getMessage());
    		}finally{
    			System.out.println("最终通知!");
    		}
    		System.out.println("后置通知!"+obj);
    	}
    	
    }
    

    5、编写测试类

    package com.wh.aop;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestAop {
    
    	@Test
    	public void testdoBefore(){
    		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
    		Computer c=(Computer)ac.getBean("computer");
    		c.play01();
    	}
    	/*
    	     前置通知!
    	     play01玩家
    	 */
    	
    	@Test
    	public void testdoAfterReturning(){
    		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
    		Computer c=(Computer)ac.getBean("computer");
    		c.play02();
    	}
    	/*
    	     play02玩家
    	     后置通知   play02
    	 */
    	
    	@Test
    	public void testdoAfter(){
    		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
    		Computer c=(Computer)ac.getBean("computer");
    		c.play02();
    	}
    	/*
    	      play02玩家
    	      最终通知   
    	      后置通知   play02
    	 */
    	
    	
    	@Test
    	public void testdoAfterThrowing(){
    		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
    		Computer c=(Computer)ac.getBean("computer");
    		c.play03();
    	}
    	/*
    	       play03玩家
                   异常通知:   java.lang.ArithmeticException: / by zero
    	 */
    	
    	@Test
    	public void testdoAround(){
    		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
    		Computer c=(Computer)ac.getBean("computer");
    		c.play04();
    	}
    	/*
    	   	前置通知
    		play04玩家
    		最终通知!
    		后置通知!play04
    	 */	
    	
    }
    

      

      

      

  • 相关阅读:
    关于自适应屏幕方向和大小的一些经验
    在线升级Android应用程序完善版
    H263&H264&MPEG4
    PyCharm2019 激活
    VMware Workstation下载安装破解秘钥
    linux/kali安装及更新源以及输入法等配置
    python推倒式(列表、字典、集合)
    协程
    Flask中获取参数(路径,查询,请求体,请求头)
    Flask中获取参数(路径,查询,请求体,请求头)
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/6869767.html
Copyright © 2020-2023  润新知