SpringAOP编程-传统基于JDK代理的AOP开发
2018-02-10 16:27
330 查看
1、spring的传统aop编程它支持的增强(advice)有五种:
1) 前置通知 目标方法执行前增强 org.springframework.aop.MethodBeforeAdvice 2) 后置通知
目标方法执行后增强 org.springframework.aop.AfterReturningAdvice
3) 环绕通知
4) 异常抛出通知
目标方法抛出异常后的增强 org.springframework.aop.ThrowsAdvice
5) 引介通知 在目标类中添加一些新的方法或属性
2、 基本jar包
1) bean
2) core
3) context
4) expression
5) aop
6)需要aop联盟的依赖jar包
![](https://img-blog.csdn.net/20180210162044346?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTc3MjMwNDQxOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
3、编写目标(target)
![](https://img-blog.csdn.net/20180210162144379?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTc3MjMwNDQxOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
4、增强(advice)
![](https://img-blog.csdn.net/20180210162226560?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTc3MjMwNDQxOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
5、在applicationContext.xml文件中进行配置
测试代码
1) 前置通知 目标方法执行前增强 org.springframework.aop.MethodBeforeAdvice 2) 后置通知
目标方法执行后增强 org.springframework.aop.AfterReturningAdvice
3) 环绕通知
4) 异常抛出通知
目标方法抛出异常后的增强 org.springframework.aop.ThrowsAdvice
5) 引介通知 在目标类中添加一些新的方法或属性
2、 基本jar包
1) bean
2) core
3) context
4) expression
5) aop
6)需要aop联盟的依赖jar包
3、编写目标(target)
4、增强(advice)
5、在applicationContext.xml文件中进行配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 目标target --> <bean id="orderService" class="cn.nwtxxb.aop.OrderServiceImpl"></bean> <!-- 通知advice --> <bean id="orderServiceAdvice" class="cn.nwtxxb.aop.OrderHelper"></bean> <!-- 定义切点 pointcut --> <!-- <bean id="orderServicePointCut" class="org.springframework.aop.support.NameMatchMethodPointcut"> <property name="mappedNames"> <list> <value>add</value> <value>update</value> </list> </property> </bean> --> <bean id="orderServicePointCut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <property name="pattern" value=".*Order"></property> </bean> <!-- 切面aspect=pointcut+advice --> <bean id="orderServiceAspect" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="advice" ref="orderServiceAdvice"/> <property name="pointcut" ref="orderServicePointCut"/> </bean> <!-- 代理 proxy --> <bean id="orderServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="orderService"/> <property name="interceptorNames" value="orderServiceAspect"/> <property name="proxyInterfaces" value="cn.nwtxxb.aop.IOrderService"/> </bean> </beans>
测试代码
相关文章推荐
- Spring AOP编程(两种:1,传统的AOP切面编程 2,基于aspectj框架切点传统开发)
- Spring AOP编程-传统基于aspectJ切点AOP开发
- 基于Spring AOP的JDK动态代理和CGLIB代理
- 框架 day37 Spring3,AOP,代理模式(动态/CGLIB/工厂bean),传统AOP,AspectJ框架(基于xml/注解),切入点表达式,jdbcTemplate
- AOP编程之JDK动态代理和Cglib动态代理
- spring经典的基于代理的AOP编程-[控制粒度-类]
- JAVAWEB开发之Spring详解之——AOP底层剖析(基于JDK和cglib)、Spring中的AOP以及基于AspectJ的AOP实现、Spring的JDBCTemplate详解
- Spring AOP编程-传统AOP开发切点表达式写法介绍
- spring经典的基于代理的AOP编程-方法
- 基于JDK动态代理的经典Spring AOP
- spring经典的基于代理的AOP编程-[控制粒度-类](beans.xml)
- Java动态代理之JDK动态代理和CGLib动态代理 面向切面编程AOP原理
- AOP面向切面编程,JDK的动态代理方式
- 对SpringAop的思考之基于jdk的动态代理
- AOP编程学习笔记之----JDK动态代理技术
- 《精通Spring4.X企业应用开发实战》读后感第七章(AOP基础知识、jdk动态代理,CGLib动态代理)
- 基于Spring AOP的JDK动态代理和CGLIB代理
- Spring AOP之---基于JDK动态代理和CGLib动态代理的AOP实现
- Spring aop 基于JDK动态代理和CGLIB代理的原理以及为什么JDK代理需要基于接口
- aop动态代理基于jdk的的实现