AOP基础 : http://wayfarer.cnblogs.com/articles/241024.html
实现AOP的技术,主要分为两大类:
一是采用动态代理技术,利用截取消息的方式,对该消息进行装饰,以取代原有对象行为的执行;
二是采用静态织入的方式,引入特定的语法创建“方面”,从而使得编译器可以在编译期间织入有关“方面”的代码。
然而殊途同归,实现AOP的技术特性却是相同的,分别为:
1、join point(连接点):是程序执行中的一个精确执行点,例如类中的一个方法。它是一个抽象的概念,在实现AOP时,并不需要去定义一个join point。
2、point cut(切入点):本质上是一个捕获连接点的结构。在AOP中,可以定义一个point cut,来捕获相关方法的调用。
3、advice(通知):是point cut的执行代码,是执行“方面”的具体逻辑。包括前置通知,后置通知,环绕通知,
4、aspect(方面):point cut和advice结合起来就是aspect,它类似于OOP中定义的一个类,但它代表的更多是对象间横向的关系。
5、introduce(引入):为对象引入附加的方法或属性,从而达到修改对象结构的目的。有的AOP工具又将其称为mixin。
上述的技术特性组成了基本的AOP技术,大多数AOP工具均实现了这些技术。它们也可以是研究AOP技术的基本术语。
AOP有些特有的概念,如:advisor、advice和pointcut等等,使用或配置起来有点绕,让人感觉有些距离感,其实它的实现就是一组标准的设计模式的组合使用:Factory、Proxy、Chain of Responsibility,只要搞清楚这几个设计模式,读AOP的源码是比较容易的。
spring AOP配置中,可以通过三种方式配置:在 Spring AOP 第一代中, AOP 的 Advice 必须实现特定接口,而配置设置依赖于 XML 的繁琐设置。在 Spring2.0 之后,对于 AOP 功能的实现与设置新增了两种方法:一种是基于 XML Schema 的设置;一种是基于 Annotation 的支持。两种方式对于 AOP 在使用上的简化都有极大地帮助。
一是基于Spring API的配置文件,基于sprint API的方式时,advice类必须implements MethodInterceptor, AfterAdvice等接口。
二是使用Spring 2.0基于XML Schema的AOP设置方式,此种方式您必须在XML设档的开头加入AOP的names,此种方式advice无须实现implements MethodInterceptor, AfterAdvice等接口。
三是基于Annotation的AOP设置方式,此种方式在XML中必须配置<aop:aspectj-autoproxy/>,此种方式advice无须实现implements MethodInterceptor, AfterAdvice等接口。
配置方法一: 使用代理工厂的方式配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>
<bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>
<bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor"></bean>
<bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean>
<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.dragon.study.IStudent</value>
</property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>compareInterceptor</value>
</list>
</property>
<property name="target">
<ref bean="studenttarget"/>
</property>
</bean>
</beans>
ApplicationContext ctx = new FileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");
IStudent person = (IStudent)ctx.getBean("student");
person.addStudent("dragon");
配置方法二:使用advisor的方式配置
<beans>
<bean id="log" class="com.gc.action.LogAround"/>
<bean id="timeBook" class="com.gc.action.TimeBook"/>
<!--代理目标类的指定方法-->
<bean id="logAdvisor"class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref bean="log"/>
</property>
<!--指定要代理的方法-->
<property name="patterns">
<value>.*doAuditing.* </value>
</property>
</bean>
<!--设定代理类-->
<bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.gc.impl.TimeBookInterface</value>
</property>
<property name="target">
<ref bean="timeBook"/>
</property>
<property name="interceptorNames">
<list>
<value>logAdvisor</value>
</list>
</property>
</bean>
</beans>
配置方法三: 基于XML Schema的AOP设置方式
<bean id="userManager" class="cn.com.manager.UserManagerImpl"></bean>
<bean id="securitHandler" class="cn.com.aop.SecurityHandler"></bean>
<aop:config>
<!-- 面相当于拦截器中的通知 -->
<aop:aspect ref="securitHandler">
<!-- 对所有的类以add开头的任何方法使用该方法。相当于拦截器中的目标 -->
<aop:pointcut id="poicut" expression="execution(* add*(..))" />
<!-- method 指出before要调用的方法 指定在接入点处调用的面的方法,如果securitHandler 继承 mothdBefore可不这样写-->
<aop:before method="check" pointcut ref="poicut"/>
</aop:aspect>
</aop:config>
<!-- 加入aop名称空间后,就可以使用spring2.0的标签aop了 -->
<bean id="logBeforeAdvise" class="AOP2Schema.LogBeforeAdvice"/>
<bean id="helloSpeaker" class="AOP2Schema.HelloSpeaker"/>
<aop:config>
<aop:aspect id="logging" ref="logBeforeAdvise">
<aop:before pointcut="execution(* AOP2Schema.ISpeaker.* (..))" method="before"/>
</aop:aspect>
</aop:config>
配置方式四:基于Annotation的AOP设置方式
@Aspect
public class LogBeforeAdvice {
@Pointcut("execution(* com.edu.cug.IHello.*(..)) ")
public void anymethod(){}
@Before("anymethod()")
public void before(JoinPoint joinPoint) {
System.out.println(joinPoint.getSignature().getName()
+ ",start.............LogBeforeAdvice");
}
}
分享到:
相关推荐
展示了java aop原理 展示了java aop原理
spring ioc容器初始化流程图 spring ioc容器依赖注入流程图 spring aop实现原理流程图
spring中动态代理机制的实现原理及AOP实现原理,JDK的反射,cglib类。
aop原理使用demo
代理模式与动态代理--Spring AOP原理.doc
为了说明Spring的AOP原理,本人使用代理模式中的动态代理完成演示AOP编程的原理的演示。相信,如果你耐心看完整个程序(几乎一行注释一行代码),那么你对Spring这个东西就不是觉得有什么神秘了! 阅读对象:凡是喜爱...
Spring 框架之 AOP 原理剖析.html 抓下来打包成了HTML文件, 方便离线观看
SpringAOP原理及拦截器.pdf
SpringAOP原理及拦截器.docx
SpringAOP原理及拦截器.doc
NULL 博文链接:https://zhang-yingjie-qq-com.iteye.com/blog/319927
Spring注解驱动开发第33讲——AOP原理总结
AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理
Java核心知识体系4:AOP原理和切面应用
浅尝Spring注解开发_AOP原理及完整过程分析(源码).doc
Spring AOP面向方面编程原理:AOP概念,主要介绍面向对象的概念及原理,及作者的一些理解。
AOP原理1
介绍Spring AOP的原理,还有例子
NULL 博文链接:https://jadeluo.iteye.com/blog/1940019
Spring AOP 学习记录 AOP(面向切面编程)方面的知识又是看了忘忘了看,今天有空记录下AOP的知识点。主要分为以下几方面: 1、AOP相关术语 2、基础知识及示例 3、增强分类 1、AOP相关术语 连接点...