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)、待拦截的服务
(2)、拦截服务类
(3)、服务注册,采用代理方式实现
(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; } }
相关文章推荐
- 问题简单汇总(待续)
- 零基础myeclipse下springBoot+maven+thymeleaf的开发(4) 修改端口以及打包
- java编程规范
- spring3.2及其以下版本和jdk8不兼容的问题.
- Spring security实现权限管理
- java 集合系列(1) --------------基本介绍
- java.lang.UnsatisfiedLinkError解决方法
- SpringMVC 源码深度解析<context:component-scan>(扫描和注冊的注解Bean)
- myeclipse 中Js文件报错
- Java并发编程系列之二十六:ConcurrentModificationException
- JAVA经典算法40题
- 关于RxJava的from()和 flatMap()方法
- ADT中找不到NDK解决方法
- Java 是目前被采用得最多的语言
- Spring中的AOP
- JavaEE通过response实现请求重定向
- javaweb 学习资源
- @suppressWarnings("unchecked") java 中是什么意思
- Spring源码分析——BeanFactory体系之接口详细分析
- Mybatis-Spring-注入映射器