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.*(..))"




  • 相关阅读:
    毕业设计每日博客--第一周4
    毕业设计每日博客--第一周3
    python使用chrome driver做简单爬虫--转载于简书
    毕业设计每日博客--第一周2
    毕业设计每日博客--第一周1
    每周总结9
    阅读笔记9--分析模式
    阅读笔记8--分析模式
    pycharm报错ModuleNotFoundError: No module named 'selenium'
    mac pycharm2019.3 安装和激活
  • 原文地址:https://www.cnblogs.com/E-star/p/3560964.html
Copyright © 2011-2022 走看看