Spring AOP

非 Y 不嫁゛ 提交于 2020-03-09 17:51:35

Spring AOP 手动配置通知类

前置通知

import java.lang.reflect.Method;
import org.springframework.aop.BeforeAdvice;

public class GreetingBeforAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method,Object []params,Object obj) throws Throwable{
        String name=String.valueOf(params[0]);
        System.out.println("###前置通知:参数," + name );
    }
}

后置通知

import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;

public class GreetingAfterAdvice implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object obj, Method method, Object[] params,Object arg3) throws Throwable {
        String name=String.valueOf(params[0]);
        System.out.println("###后置通知:参数," + name );
    }

}
环绕通知

import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class GreetingCircleAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation arg0) throws Throwable {
        Method method = arg0.getMethod();
        Object[] par = arg0.getArguments();
        System.out.println("###进入环绕通知");
        Object obj = arg0.proceed();
        System.out.println("###完成环绕通知");
        return obj;
    }
}

异常通知

import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class GreetingExceptionAdvice implements ThrowsAdvice {
    public void afterThrowing(Exception ex){
        System.out.println("###异常通知");
        System.out.println("异常信息:"+ex.getMessage());
    }
}

配置applictionConte

    <bean id="beforAdvice" class="com.easeye.springaop.GreetingBeforAdvice"></bean>
    <bean id="afterAdvice" class="com.easeye.springaop.GreetingAfterAdvice"></bean>
    <bean id="circleAdvice" class="com.easeye.springaop.GreetingCircleAdvice"></bean>
    <bean id="exceptionAdvice" class="com.easeye.springaop.GreetingExceptionAdvice"></bean>


    <bean id="waiter" class="com.easeye.springaop.WaiterImpl"></bean>


    <bean id="proxy1" class="org.springframework.aop.framework.ProxyFactoryBean"
      p:proxyInterfaces="com.easeye.springaop.Waiter"
      p:interceptorNames="beforAdvice,afterAdvice"
      p:target-ref="waiter"
      p:proxyTargetClass="true">
    </bean>


    <bean id="proxy2" class="org.springframework.aop.framework.ProxyFactoryBean"
      p:proxyInterfaces="com.easeye.springaop.Waiter"
      p:interceptorNames="circleAdvice,exceptionAdvice"
      p:target-ref="waiter"
      p:proxyTargetClass="true">
    </bean>

测试

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Waiter waiter1 =(Waiter) context.getBean("proxy1");
        waiter1.SayHello("CWGA");
     
        Waiter waiter2 =(Waiter) context.getBean("proxy2");
        waiter2.SayHello("CWGB");
        try {
            waiter2.error("CWGC");
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

结果:

###前置通知:参数,CWGA
你好:CWGA
###后置通知:参数,CWGA
###进入环绕通知
你好:CWGB
###完成环绕通知
###进入环绕通知
###异常通知
异常信息:手动抛出异常

 

Spring AOP 自动装载通知类(不使用面向接口编程也可以使用)

applicationContext.xml添加配置

<bean  id="son" class="com.easeye.asp.SonImpl"></bean>
<bean  class="com.easeye.asp.NoticeAdvice"></bean>

<context:component-scan base-package="com.easeye.asp"></context:component-scan>


<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

 <bean  class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>

目标类

public class SonImpl  {

    //@Override
    public String DrinkMilk(String name) {
        String str="好喝的牛奶"+name;
        System.out.println(str);
        return str;
    }
    
    public void DrinkMilkS(String name){
        String str="真好喝的牛奶"+name;
        System.out.println(str);
        
    }
    
    //@Override
    public String AddMilk(String name){
        String str="添加牛奶:"+name;
        System.out.println(str);
        return str;
    }
    
    //@Override
    public String FindMilk(String name){
        String str="找到牛奶:"+name;
        System.out.println(str);
        return str;
    }

}

通知类

@Aspect
@Component
public class NoticeAdvice {
    
    @Before("execution(* com.easeye.asp.*.DrinkMilk(..))")
    //@AfterReturning
    //@AfterThrowing
    //@Around
    public void Before(JoinPoint point){
        System.out.println( String.format("###前置通知,%1$s  %2$s",point.getSignature().getClass(),point.getSignature().getName()));
    }
    
    @Around("execution(* com.easeye.asp.*.DrinkMilkS(..))")
    public void Around(ProceedingJoinPoint point) throws Throwable{
        System.out.println("###开始环绕通知"+point.getSignature().getName());
        point.proceed();
        System.out.println("###结束环绕通知");
    }
}

测试

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
         SonImpl son = (SonImpl)context.getBean("son");
         son.DrinkMilk("牛栏");
         son.DrinkMilkS("牛栏2段");
         son.AddMilk("爱他美");
    }

执行结果 :

###前置通知,class org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$MethodSignatureImpl  DrinkMilk
好喝的牛奶牛栏
###开始环绕通知DrinkMilkS
真好喝的牛奶牛栏2段
###结束环绕通知
添加牛奶:爱他美

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!