您的位置:首页 > 编程语言 > Java开发

spring AOP

2016-04-08 14:58 218 查看
一、简单的拦截

(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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: