Spring Aop支持的两种方式

1.xml配置方式,需要将myInterceptor类中的注解去掉

<?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:context
="http://www.springframework.org/schema/context" 
       xmlns:aop
="http://www.springframework.org/schema/aop"      
       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="com.westsoft.serviceimpl.PersonServiceBean"></bean>
        <bean id="aspetbean" class="com.westsoft.serviceimpl.MyInterceptor"/>
        <aop:config>
            <aop:aspect id="asp" ref="aspetbean">
                <aop:pointcut id="mycut" expression="execution(* com.westsoft.serviceimpl.PersonServiceBean.*(..))"/>
                <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
                <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> 

2.注解方式

package com.westsoft.serviceimpl;

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.Service;

@Aspect @Service
public class MyInterceptor {

    /*
     * 切入点
     
*/
    @Pointcut("execution(* com.westsoft.serviceimpl.PersonServiceBean.*(..))")
    private void anyMethod(){}
    
    /*
     * 传入参数
     
*/
    @Before("anyMethod() && args(name)")
    public void doSomthind(String name)
    {
        System.out.println("前置通知:"+name);
    }
    
    
    /*
     * returning="result" 返回结果
     
*/
    @AfterReturning(pointcut="anyMethod()",returning="result")
    public void doAfterreturning(String result)
    {
        System.out.println("后置通知"+result);
    }
    
    /*
     * 获取例外
     
*/
    @AfterThrowing(pointcut="anyMethod()",throwing="e")
    public void doAftertrhrowing(Exception e)
    {
        System.out.println("例外通知"+e);
    }
    
    @After("anyMethod()")
    public void doAfter()
    {
        System.out.println("最终通知");
    }
    
    @Around("anyMethod()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable
    {
        System.out.println("环绕通知");
        Object rst=pjp.proceed();
        System.out.println("环绕通知");
        return rst;
    }
    
    
    
    
    

原文地址:https://www.cnblogs.com/kuailewangzi1212/p/2408509.html