`

[Spring引用文档] 9. Spring AOP

阅读更多

 

9.1介绍

面向切面编程(AOP)结合面向对象编程(OOP)来提供另一种思考方式的程序结构。OOP模块化的关键单元是类型(class),而AOP模块化的单元则是切面(aspect)。切面能使关注点(concerns)更具模块化,而关注点横跨多个类型和对象切割,这样的关注点也称为横切关注点(crosscutting concerns)

AOP框架是Spring的关键组件之一,AOP结合IoC容器来提供一种强大的中间件解决方案

Spring 2.0 AOP

Spring 2.0引入了一种简单而又强大的编写自定义切面的方式,使用基于schema方法或者@AspectJ注解方式实现。这两种方式都支持所有类型的AspectJ切点语言的通知和使用,同时仍然可以使用Spring AOP进行织入(weaving)

AOP用途

  • Ÿ   提供声明式企业服务。最重要的此类服务是声明式事务管理
  • Ÿ   允许用户实现自定义切面,结合使用OOPAOP

AOP概念 (☆☆☆☆☆)

Ÿ   Aspect(切面)一个模块化的关注点(a concern),其横跨多个类。在企业应用中,事务管理就是横切关注点的一个好例子。在Spring AOP中,切面使用普通类(基于schema方法)或者使用@Aspect注解注释的普通类实现

Ÿ   Join point(连接点)程序执行过程中的一个点(a point),如方法执行异常处理。在Spring AOP中,一个连接点总是代表方法的执行

Ÿ   Advice(通知)一个切面一个特定连接点上采取的动作(action)。很多AOP框架都将一个通知作为一个拦截器(an interceptor),来维护环绕连接点的一系列拦截器。

Ÿ   Pointcut(切点)匹配连接点一个断言(a predicate)通知是与一个切点表达式(a pointcut expression)相关联的,且运行在任何切点匹配的连接点切点表达式匹配连接点的概念是AOP的核心,Spring默认使用AspectJ切点表达式语言。

Ÿ   Introduction(引入):在被通知类中声明其它方法和字段

Ÿ   Target object(目标对象)切面通知的对象,也被称为被通知的对象(advised object)。由于Spring AOP使用动态代理实现,这个对象永远是一个被代理的对象(proxied object)

Ÿ   AOP proxy(AOP代理)AOP框架创建的一个对象,为了实现切面契约(通知方法执行等)。在Spring框架中,一个AOP代理可以是JDK动态代理,或者CGLIB代理

Ÿ   Weaving(织入)切面类型或者创建目标对象的对象连接起来。这个过程可以在编译时、加载时或者运行时完成,Spring AOP是在运行时进行织入

(上面介绍的概念中,切面连接点、通知和切点是需要开发者关心的,而目标对象、AOP代理和织入则是由AOP框架实现的。)

通知类型:

  • Ÿ   Around advice(环绕通知):环绕一个连接点(如方法调用)执行。环绕通知是最强大的一种通知,其可以在方法执行之前和之后执行自定义行为。它可以选择是否继续执行连接点,或者是否通过返回值和抛出一个异常来直接终止被通知的方法执行
  • Ÿ   Before advice(前置通知):在连接点之前执行,其无法阻止执行流程行进到连接点
  • Ÿ   After returning advice(后置通知):在一个连接点正常执行完成后执行
  • Ÿ   After throwing advice(异常通知):在一个方法抛出异常之后执行
  • Ÿ   After (finally) advice(最终通知):只要相关的连接点存在就会被执行,不管方法是正常或异常返回。通常用它来释放资源(类似Javafinally语法功能)

环绕通知是最常用的一种通知。由于Spring AOP支持所有通知类型,所以建议尽量使用最简单的满足你需求的通知。例如,你仅仅需要使用方法返回值来更新缓存,那么实现一个后置通知比环绕通知更好。

连接点(被切点匹配)的概念是AOP的关键切点能使通知成为面向对象层次的独立目标,如一个提供声明式事务管理的环绕通知可以被应用于横跨多个对象的一系列方法上

Spring AOP功能和目标

Spring AOP目前仅支持方法执行连接点(通知beans方法执行),未实现字段拦截。如果需要通知字段访问和更新连接点,可以考虑AspectJ

Spring AOP目标是不提供最完整的AOP实行,而是提供AOP实现和IoC容器的紧密集成,帮助解决企业应用中的常见问题

AOP代理

Spring AOP默认使用JDK动态代理,其能代理任何接口

Spring AOP也可以使用CGLIB代理,它只能代理类型,而非接口。

重要的是要把握Spring AOP是基于代理的事实。

AOP应用范围

JBoss 4.0正是使用AOP框架进行开发。具体功能如下:

  • Ÿ   Authentication 权限(环绕通知)
  • Ÿ   接口或appkey限流、服务降级(环绕通知)
  • Ÿ   Caching 缓存(后置通知)
  • Ÿ   Context passing 上下文传递
  • Ÿ   Error handling 错误处理(异常通知)
  • Ÿ   Lazy loading 延迟加载(前置通知)
  • Ÿ   Debugging 调试(环绕通知)
  • Ÿ   logging, tracing, profiling and monitoring 日志记录、跟踪、分析和监测
  • Ÿ   Performance optimization 性能优化(环绕通知)
  • Ÿ   Persistence 持久化
  • Ÿ   Resource pooling 资源池(初始化使用前置通知,释放使用最终通知)
  • Ÿ   Synchronization 同步(try-finally,环绕和最终通知的组合)
  • Ÿ   Transactions 事务(try-catch-finally,环绕、异常和最终通知的组合)

参考自AOP基本概念

XML配置示例

 spring-aop-3.2.xsd 源文件:
	<xsd:element name="config">
		<xsd:annotation>
			<xsd:documentation><![CDATA[
	A section (compartmentalization) of AOP-specific configuration (including aspects, pointcuts, etc). (AOP相关的配置部分)
			]]></xsd:documentation>
		</xsd:annotation>
		<xsd:complexType>
			<xsd:sequence>
				...
				<xsd:element name="aspect" type="aspectType" minOccurs="0" maxOccurs="unbounded">
					...
	A named aspect definition. (切面定义)
					...
				</xsd:element>
				...

	<xsd:complexType name="aspectType">
		<xsd:choice minOccurs="0" maxOccurs="unbounded">
			...
			<xsd:element name="around" type="basicAdviceType">
				...
	An around advice definition. (通知定义)
				...
			</xsd:element>
		</xsd:choice>
		<xsd:attribute name="id" type="xsd:string">
			...
	The unique identifier for an aspect. (一个切面的唯一标识符)
			...
		<xsd:attribute name="ref" type="xsd:string">
			...
	The name of the (backing) bean that encapsulates the aspect. (封装切面的bean名称)
			...
		<xsd:attribute name="order" type="xsd:int">
			...
source="java:org.springframework.core.Ordered"><![CDATA[
	Controls the ordering of the execution of this aspect when multiple
	advice executes at a specific joinpoint. (控制本切面的执行顺序,当多个通知在一个特点连接点上执行时)
				...

	<xsd:complexType name="basicAdviceType">
		<xsd:attribute name="pointcut" type="xsd:string">
			...
	The associated pointcut expression. (相关的切点表达式)
			...
		</xsd:attribute>
		<xsd:attribute name="pointcut-ref" type="pointcutRefType">
			...
	The name of an associated pointcut definition. (相关的切点定义名称)
				<xsd:appinfo>
					<tool:annotation kind="ref">
						<tool:expected-type type="org.springframework.aop.Pointcut"/>
					</tool:annotation>
				</xsd:appinfo>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="method" type="xsd:string" use="required">
			...
	The name of the method that defines the logic of the advice. (定义通知逻辑的方法名)
			...
		</xsd:attribute>
		<xsd:attribute name="arg-names" type="xsd:string">
			...
	The comma-delimited list of advice method argument (parameter) names
	that will be matched from pointcut parameters. (通知方法参数名列表,其会被切点参数匹配)
			...
		</xsd:attribute>
	</xsd:complexType>
 
<aop:config>
    <aop:aspect id="切面ID" ref="封装切面的bean">
        <aop:around 
                method="切面中的通知(Advice)方法"
                pointcut="匹配连接点的表达式,选择要横切的类或方法集"/>
    </aop:aspect>
</aop:config>
 
<aop:config>
    <aop:aspect id="xxxAspect" ref="aspectBean">
        <aop:around 
                method="anAspectMethod"
                pointcut="within(com.xyz.myapp.web..*) and @annotation(com.xyz.myapp.util.config.annotation.XxxAnnotation)"/>
    </aop:aspect>
</aop:config>

<bean id="aspectBean" class="com.xyz.myapp.util.config.XxxAspect">
    <property name="maxRetries" value="3" />
</bean>
 

 

玩的开心!^_^

分享到:
评论

相关推荐

    spring-aop-5.2.0.RELEASE-API文档-中文版.zip

    赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom; 包含翻译后的API文档:spring-aop-5.2.0....

    spring-aop-5.0.10.RELEASE-API文档-中文版.zip

    赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE.pom; 包含翻译后的API文档:spring-aop-5.0.10...

    spring-aop-5.0.8.RELEASE-API文档-中英对照版.zip

    赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom; 包含翻译后的API文档:spring-aop-5.0.8....

    spring-aop-4.2.2.RELEASE-API文档-中文版.zip

    赠送原API文档:spring-aop-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-4.2.2.RELEASE.pom; 包含翻译后的API文档:spring-aop-4.2.2....

    spring-aop-4.3.20.RELEASE-API文档-中英对照版.zip

    赠送原API文档:spring-aop-4.3.20.RELEASE-javadoc.jar 赠送源代码:spring-aop-4.3.20.RELEASE-sources.jar 包含翻译后的API文档:spring-aop-4.3.20.RELEASE-javadoc-API文档-中文(简体)-英语-对照版.zip 对应...

    spring-aop-5.1.3.RELEASE-API文档-中英对照版.zip

    赠送原API文档:spring-aop-5.1.3.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.1.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.1.3.RELEASE.pom; 包含翻译后的API文档:spring-aop-5.1.3....

    spring-aop-5.2.15.RELEASE-API文档-中文版.zip

    赠送原API文档:spring-aop-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.15.RELEASE.pom; 包含翻译后的API文档:spring-aop-5.2.15...

    spring-aop-5.1.0.RELEASE.jar

    - 以RELEASE-jdoc.jar结尾的包是spring框架API文档的压缩包。 - 以RELEASE-sources.jar结尾的是Spring框架源文件的压缩包。 ### 在libs目录中,有四个Spring的基础包 spring-**core**-4.3.6.RELEASE.jar :包含...

    springAOP demo 带错误解决文档

    nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: ...

    spring-aop-5.2.7.RELEASE-API文档-中英对照版.zip

    赠送原API文档:spring-aop-5.2.7.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.7.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.7.RELEASE.pom; 包含翻译后的API文档:spring-aop-5.2.7....

    spring-aop-5.0.5.RELEASE-API文档-中文版.zip

    赠送原API文档:spring-aop-5.0.5.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.5.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.5.RELEASE.pom; 包含翻译后的API文档:spring-aop-5.0.5....

    spring-aop-4.3.12.RELEASE-API文档-中文版.zip

    赠送原API文档:spring-aop-4.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.3.12.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-4.3.12.RELEASE.pom; 包含翻译后的API文档:spring-aop-4.3.12...

    spring-aop-5.0.8.RELEASE-API文档-中文版.zip

    赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom; 包含翻译后的API文档:spring-aop-5.0.8....

    spring-aop-5.1.3.RELEASE-API文档-中文版.zip

    赠送原API文档:spring-aop-5.1.3.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.1.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.1.3.RELEASE.pom; 包含翻译后的API文档:spring-aop-5.1.3....

    spring-aop-5.2.7.RELEASE-API文档-中文版.zip

    赠送原API文档:spring-aop-5.2.7.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.7.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.7.RELEASE.pom; 包含翻译后的API文档:spring-aop-5.2.7....

    spring-aop-5.3.15-API文档-中英对照版.zip

    赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring-aop-5.3.15-javadoc-API文档-中文...

    spring-aop-4.3.20.RELEASE-API文档-中文版.zip

    赠送原API文档:spring-aop-4.3.20.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.3.20.RELEASE-sources.jar; 包含翻译后的API文档:spring-aop-4.3.20.RELEASE-javadoc-API文档-中文(简体)版.zip 对应Maven...

    spring-aop-5.2.15.RELEASE-API文档-中英对照版.zip

    赠送原API文档:spring-aop-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.15.RELEASE.pom; 包含翻译后的API文档:spring-aop-5.2.15...

    spring-aop-5.0.5.RELEASE-API文档-中英对照版.zip

    赠送原API文档:spring-aop-5.0.5.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.5.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.5.RELEASE.pom; 包含翻译后的API文档:spring-aop-5.0.5....

    spring-aop-4.2.2.RELEASE-API文档-中英对照版.zip

    赠送原API文档:spring-aop-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-4.2.2.RELEASE.pom; 包含翻译后的API文档:spring-aop-4.2.2....

Global site tag (gtag.js) - Google Analytics