使用Spring AOP 拦截 方法,给被拦截的方法加上,前置通知,后置通知,例外通知,最终通知 的两个例子,见 附件
需要 spring.jar,cglib.jar,commons-logging.jar
方式1. 被拦截的类没有实现任何接口
关键代码:
package junit.test; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.aop.framework.ProxyFactoryBean; import org.springframework.aop.support.DefaultPointcutAdvisor; import cn.com.xinli.common.BusinessMethodInterceptor; import cn.com.xinli.service.BusinessInterface; import cn.com.xinli.service.impl.BusinessImpl; /*springAOP 测试方式1 的测试用例 只需要spring.jar 支持 * * */ public class AOPTest { static BusinessInterface face=null; @BeforeClass public static void setUpBeforeClass() throws Exception { BusinessImpl bi=new BusinessImpl(); ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean(); proxyFactoryBean.setTarget(bi); proxyFactoryBean.addAdvisor(new DefaultPointcutAdvisor(new BusinessMethodInterceptor())); proxyFactoryBean.setInterfaces(new Class[]{BusinessInterface.class}); face= (BusinessInterface)proxyFactoryBean.getObject(); } @Test public void testAdd() throws Exception { face.add(); } @Test public void testDel() { face.del(); } @Test public void testFind() { face.find(); } @Test public void testUpdate() { face.update(); } }
方式2. 被拦截的类没有实现任何接口
关键代码:
package junit.test; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.framework.ProxyFactoryBean; import org.springframework.aop.support.DefaultPointcutAdvisor; import cn.com.xinli.common.BusinessMethodInterceptor; import cn.com.xinli.service.impl.Business; /*springAOP 测试方式2 的测试用例 需要spring.jar和cglib.jar 支持 * * */ public class AOPTest2 { static Business business =null; @BeforeClass public static void setUpBeforeClass() throws Exception { /**/ ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean(); proxyFactoryBean.addAdvisor(new DefaultPointcutAdvisor( new BusinessMethodInterceptor())); proxyFactoryBean.setTarget(new Business()); business = (Business) proxyFactoryBean.getObject(); } @Test public void testAdd() throws Exception { /*另一种调用方式*/ Business target = new Business(); ProxyFactory pf = new ProxyFactory(); pf.addAdvice(new BusinessMethodInterceptor()); pf.setTarget(target); Business proxy = (Business) pf.getProxy(); //输出代理对象调用的信息 proxy.add(); } @Test public void testDel() { business.del(); } @Test public void testFind() { business.find(); } @Test public void testUpdate() { business.update(); } }
参考:
相关推荐
死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理(csdn)————程序
spring aop demo 通过注解和配置文件的两种实现方式 包含jar包 下载直接运行
sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释。
AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理
Spring AOP 使用的动态代理主要有两种方式:JDK 动态代理和 CGLIB 代理。 JDK 动态代理:用于代理实现了接口的类。Spring 会使用 java.lang.reflect.Proxy 类来创建代理对象。 CGLIB 代理:用于代理没有实现接口的...
SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo
详细讲解了aop的xml配置和注解的方式配置,和本人的博客想应
主要为大家详细介绍了spring aop两种配置方式,主要是注解配置AOP和xml配置aop,需要的朋友可以参考下
spring-aop标签和配置文件两种方式实例,spring要求2.5及以上
SpringAop实例,包括xml和配置两种方式.jar齐全
3、了解Spring中两种动态代理方式的区别; 4、掌握基于XML和注解的AspectJ开发。 二、 实验内容 1、按图所示的类图结构,设计接口及其实现类,并完成另外两附加要求:(1)日志功能:在程序执行期间追踪正在发生的...
本资源将深入探讨Spring框架中使用的两种关键的动态代理方式:JDK动态代理和CGLib动态代理。这两种代理方式在Spring AOP中起到关键作用,用于实现横切关注点的切面编程。通过学习它们的原理和实际应用,您将能够更好...
Spring 使用AspectJ 实现 AOP(基于xml文件、基于注解)
SpringAOP与SpringMVC拦截器两种方式实现权限管控,前台xml读取配置根据登录用户判断button是否enable
SpringAop配置事务管理,有两种配置方式。一种直接使用注解的方式,另外一种非注解
写了SpringAop的两种代理的实现,jdk动态代理和cglib代理的代码,及测试。
有关于Spring,我们最常用的两个功能就是IOC和AOP,前几篇文章从源码级别介绍了Spring容器如何为我们生成bean及bean之间的依赖关系... 确实,Spring也就是通过这两种方式来实现AOP相关功能,下面就通过源码来简单求证下
java手动实现、注解方式实现两种方法实现springaop编程,包含源码+jar包+解释
所以从本文开始介绍@Pointcut切点表达式,虽然Spring AOP的切点表达式尚未 全部实现 @AspectJ 的切点指示符,但是也已经足够多和灵活,本文主要介绍@Pointcut切点表达式中的@within、within这两种切点指示符,结合...