zoukankan      html  css  js  c++  java
  • Spring XML配置实现AOP

    1:  首先我们要定义 配置成切面的类

    package cn.gbx.example;
    
    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;
    
    
    public class MyXmlIntercept {
    	
    	public void doAccessCheck() {
    	
    		System.out.println("前置通知");
    	}
    	
    	
    	public void doAfterReturning() {
    		System.out.println("后置通知");
    	}
    	
    	public void doAfter() {
    		System.out.println("最终通知");
    	}
    	
    	public void doAfterThrowing() {
    		System.out.println("例外通知");
    	}
    	
    	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
    		System.out.println("进入方法");
    		Object value = pjp.proceed();
    		System.out.println("退出方法");
    		return value;
    	}
    }
    
    	
    

      

    然后再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:aop="http://www.springframework.org/schema/aop" 
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop 
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
            
             <aop:aspectj-autoproxy/>
            
             <bean id="personService" class="cn.gbx.serviceimpl.PersonServiceImpl"></bean>
            
             <bean id="myXmlIntercept" class="cn.gbx.example.MyXmlIntercept"></bean>
             <aop:config>
             	<aop:aspect id="myasp" ref="myXmlIntercept">
             		<aop:pointcut expression="execution (* cn.gbx.serviceimpl.PersonServiceImpl.*(..))" id="mycut"/>
             		<aop:before method="doAccessCheck" pointcut-ref="mycut"/>
             		<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
    			  	<aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
    			  	<aop:after pointcut-ref="mycut" method="doAfter"/>
    			  	<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
             	</aop:aspect>
             </aop:config>
    </beans>
    

      

    然后测试即可。

     简单说明

     expression="execution (* cn.gbx.serviceimpl.PersonServiceImpl.*(..))"
    如果我们要要返回值烈性为Stirng的 方法才被拦截
    execution (java.lang.String cn.gbx.serviceimpl.PersonServiceImpl.*(..))

    如果我们要求参数第一个为String , 后边不限
    expression="execution (* cn.gbx.serviceimpl.PersonServiceImpl.*(java.lang.String, ..))"

    要求返回至为非void
    expression="execution (!void cn.gbx.serviceimpl.PersonServiceImpl.*(..))"




  • 相关阅读:
    30分钟掌握ES6/ES2015核心内容[上和下], 不错的说
    根据HTML5 获取当前位置的经纬度【百度地图】【高德地图】
    vue2 入门 教程 单页应用最佳实战[*****]
    JavaScript如何比较两个数组的内容是否相同【转】
    推荐下:开源ckplayer 网页播放器, 跨平台(html5, mobile),flv, f4v, mp4, rtmp协议. webm, ogg, m3u8 !
    浅谈 Underscore.js 中 _.throttle 和 _.debounce 的差异[转]
    原生JavaScript插件开发[转]
    性能监控之Spotlight
    Jmeter(三十五)聚合报告
    Jmeter(三十四)Jmeter-Question之“Cookie获取”
  • 原文地址:https://www.cnblogs.com/E-star/p/3560964.html
Copyright © 2011-2022 走看看