• applicationContext.xml配置AOP切面编程


    Computer.java

    package com.wh.aop2;
    
    public class Computer {
    
    	public void play01(){
    		System.out.println("一号玩家!");
    	}
    	public void play02(){
    		System.out.println("二号玩家!");
    		System.out.println(10/0); 
    	}
    	public void play03(){
    		System.out.println("三号玩家!");
    	}
    	public void play04(){
    		System.out.println("四号玩家!");
    	}
    	public void play05(){
    		System.out.println("五号玩家!");
    	}
    	public void play06(){
    		System.out.println("六号玩家!");
    	}
    }
    

    AopProxy.java

    package com.wh.aop2;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    
    public class AopProxy {
    
    	public void doBefore01() {
    		System.out.println("无参数前置通知:");
    	}
    
    	public void doBefore02() {
    		System.out.println("有参数前置通知:");
    	}
    
    	public void doAround(ProceedingJoinPoint p) {
    		System.out.println("环绕之前置通知:");
    		Object obj = null;
    		try {
    			obj = p.proceed();
    			System.out.println("环绕之后置通知:   " + obj);
    		}
    		catch (Throwable e) {
    			System.out.println("环绕之异常通知:   " + e.getMessage());
    		}
    		finally {
    			System.out.println("环绕之最终通知:");
    		}
    	}
    }
    

    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">
    	
    	<bean id="computer" class="com.wh.aop.Computer"/>
    	<bean id="testBefore" class="com.wh.aop.TestBefore"/>
    	
    	<aop:config>
    		<aop:pointcut expression="execution(* com.wh.aop.Computer.add(*,*))" id="computerAddCut"/>
    		<aop:pointcut expression="execution(* com.wh.aop.Computer.div(*,*))" id="computerDivCut"/>
    		<!-- AOP前置通知 -->
    		<aop:aspect ref="testBefore">
    			<aop:before method="doBefore" pointcut-ref="computerAddCut"/>
    		</aop:aspect>
    		<!-- AOP后置通知 -->
    		<aop:aspect ref="testBefore">
    				<aop:after-returning method="doAfter" pointcut-ref="computerAddCut" returning="ret"/>
    		</aop:aspect>
    		<!-- AOP异常通知 -->
    		<aop:aspect ref="testBefore">
    			<aop:after-throwing method="doThrow" pointcut-ref="computerDivCut" throwing="e"/>
    		</aop:aspect>
    	</aop:config>	
    	
    	<import resource="applicationContext2.xml"/>
    	
    	
    </beans>
    

    applicationContext2.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">
    	
    	<bean id="computer2" class="com.wh.aop2.Computer"/>
    	<bean id="aopProxy2" class="com.wh.aop2.AopProxy"/>	
    	<aop:config>
    		<aop:pointcut expression="execution(* com.wh.aop2.Computer.*(..))" id="computerCut2"/>
    		<aop:aspect ref="aopProxy2">
    			<aop:around method="doAround" pointcut-ref="computerCut2"/>
    		</aop:aspect>
    	</aop:config>
    	
    	
    </beans>
    

    TestAop.java

    package com.wh.aop2;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestAop {
    
    	@Test
    	public void test01(){
    		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
    		Computer c=(Computer)ac.getBean("computer2");
    		c.play01();
    	}
    	
    	@Test
    	public void testAround(){
    		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
    		Computer c=(Computer)ac.getBean("computer2");
    		c.play01();
    	}
    	/**
    		 环绕之前置通知:
    		一号玩家!
    		环绕之后置通知:   null
    		环绕之最终通知:
    	 */
    	
    	@Test
    	public void testAround2(){
    		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
    		Computer c=(Computer)ac.getBean("computer2");
    		c.play02();
    	}
    	/**
    		环绕之前置通知:
    		二号玩家!
    		环绕之异常通知:   / by zero
    		环绕之最终通知:
    	 */
    	//小结:在环绕通知中:正常情况下,四个通知都会出现,若出现异常,只有后置通知不会出现!
    	//通知顺序为:前置、异常、最终、后置
    }
    

      

      

      

     

  • 相关阅读:
    Oracle主库存在Online Patch,备库该如何打上该补丁
    Oracle中如何构造一条在去年运行不报错今年运行报错的SQL语句
    Linux双网卡绑定启动网卡报错Error: Connection activation failed: Master connection not found or invalid
    Oracle备库GV$ARCHIVED_LOG.APPLIED的最新归档日志状态为"IN-MEMORY"(已经应用成功)对应主库的状态为"NO"
    Oracle关于ARCHIVELOG DELETION POLICY的配置解释以及RMAN-08137/RMAN-08591的原因探究
    Oracle Logminer的测试使用
    Oracle间隔分区(interval分区)的分区字段无法为NULL值
    Oracle绑定变量类型为timestamp导致V$SQL_BIND_CAPTURE不显示值
    即时性能分析工具 Pyroscope
    Go之Zap日志库集成Gin
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/6869560.html
Copyright © 2020-2023  润新知