spring AOP

一、简单的拦截
(1)、运行前拦截 org.springframework.aop.MethodBeforeAdvice
(2)、运行后拦截 org.springframework.aop.AfterReturningAdvice
(3)、抛出异常拦截 org.springframework.aop.ThrowsAdvice
(4)、围绕拦截 org.aopalliance.intercept.MethodInterceptor

demo如下
(1)、待拦截的服务

import org.springframework.stereotype.Component;

@Component
public class HelloService {
    public String say(String name) throws Exception {
        return "hello:" + name;
    }
}

(2)、拦截服务类

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;

@Component
public class MyBeforeAdvice implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("hello,i am before advice");
    }

}
import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.stereotype.Component;

@Component
public class MyAfterAdvice implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("hello,i am after advice");
    }


}
import org.springframework.aop.ThrowsAdvice;
import org.springframework.stereotype.Component;

@Component
public class MyThrowAdvice implements ThrowsAdvice {

    public void afterThrowing(IllegalArgumentException e) throws Throwable {
        System.out.println("HijackThrowException : Throw exception hijacked!");
    }
}
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;

@Component
public class MyAroundAdvice implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("before method");

        try {
            // 调用原方法
            Object result = methodInvocation.proceed();

            System.out.println("after method");

            return result;
        } catch (IllegalArgumentException e) {
            // 相当于 ThrowsAdvice
            System.out.println("catch exception");
            throw e;
        }
    }

}

(3)、服务注册,采用代理方式实现

import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AdviceConfig {
    @Autowired HelloService helloService;

    @Bean(name="beforeAdvice")
    public ProxyFactoryBean beforeAdvice(){
        ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
        proxyFactoryBean.setTarget(helloService);
        proxyFactoryBean.setInterfaces(MyBeforeAdvice.class);

        return proxyFactoryBean;
    }

    @Bean(name="afterAdvice")
    public ProxyFactoryBean afterAdvice(){
        ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
        proxyFactoryBean.setTarget(helloService);
        proxyFactoryBean.setInterfaces(MyAfterAdvice.class);

        return proxyFactoryBean;
    }

    @Bean(name="throwAdvice")
    public ProxyFactoryBean throwAdvice(){
        ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
        proxyFactoryBean.setTarget(helloService);
        proxyFactoryBean.setInterfaces(MyThrowAdvice.class);

        return proxyFactoryBean;
    }

    @Bean(name="aroundAdvice")
    public ProxyFactoryBean aroundAdvice(){
        ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
        proxyFactoryBean.setTarget(helloService);
        proxyFactoryBean.setInterfaces(MyAroundAdvice.class);

        return proxyFactoryBean;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值