zoukankan      html  css  js  c++  java
  • 注解配置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
    	 */	
    	
    }
    

      

      

      

  • 相关阅读:
    vue中$refs、$slot、$nextTick相关的语法
    js中hash、hashchange事件
    js中filter的用法
    ES6新特性-函数的简写(箭头函数)
    js中把ajax获取的数据转化成树状结构(并做成多级联动效果)
    jq中get()和eq()的区别
    new Date() 日期格式处理
    微信小程序 加载图片时,先拉长,再恢复正常
    一个例子理解ES6的yield关键字
    eclipse在光标停留在同一对象的背景色提示,开启与关闭
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/6869767.html
Copyright © 2011-2022 走看看