`
guzizai2007
  • 浏览: 360772 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

基于Schema的AOP配置

 
阅读更多

1、Spring AOP相关描述

基于Schmeade的AOP从Spring2.0之后通过“aop”命名空间来定义切面(Aspect)、切入点(Pointcut)、增强(Advice)。
在Spring配置文件中,所有AOP相关定义都必须放在<aop:config>标签下,该标签有<aop:pointcut>、<aop:advisor>、<aop:aspect>标签,配置顺序不可改变。
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

1)、aop:pointcut:用来定义切入点,加入增强的位置
2)、aop:advisor:用来定义只有一个通知和一个切入点的切面
3)、aop:aspect:用来定义切面,内部可以有多个切入点和增强(前置通知、后置通知、异常...)
4)、aop:aspect与aop:advisor不同之处就在于advisor只能声明一个切入点和一个增强。
<aop:advisor advice-ref="" pointcut=""/>

 2、定义业务Bean:

package com.sxit;

public class HelloWorldService {
	
	public void say(String a,String b){
		System.out.println("我说....................");
	}
	
	public void look() throws Exception{
		System.out.println("我看.....................");
	}
}

 3、定义切面:

package com.sxit;

/**
 * 切面类
 */
public class HelloWorldAspect {
	
	//方法执行前执行
	public void before(){
		System.out.println("before:------------------->>>");
	}
	
	//方法执行完执行(不抛异常的情况下)
	public void after(){
		System.out.println("after======");
	}
	
	//方法执行完执行(抛异常的情况下也执行,相当于finaaly)
	public void returnFinally(){
		System.out.println("returnFinally");
	}
	
	//方法抛出异常时执行
	public void exception(Exception e){
		System.out.println("exception"+e);
	}
}

 4、配置xml:

1)、首先先声明业务Bean和切面Bean
<!-- 切面Bean -->
<bean id="helloWorldAspect" class="com.sxit.HelloWorldAspect" />
<!-- 业务Bean -->
<bean id="helloWorldService" class="com.sxit.HelloWorldService" />
2)、使用<aop:config>标签按顺序的写切入点、切面
3)、定义切入点:
<aop:pointcut id="onePointcut" expression="execution(* com.sxit..*.*(..))"/>
4)、定义切面:
<aop:aspect id="oneAspect" ref="helloWorldAspect">
			<aop:before pointcut-ref="onePointcut" method="before"/>
</aop:aspect>

 5、测试类:

public class Test {
	
	public static void main(String[] args){
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloWorldService hello = (HelloWorldService)ac.getBean("helloWorldService");
		
		hello.look();
	}
}

 6、前置通知:在目标对象方法执行前切入增强逻辑,使用<aop:before>标签声明

1)、前置通知格式:<aop:before pointcut="切入点表达式" pointcut-ref="如果已配置pointcut,这里写某个切入点的Bean Id" method="增强类中方法" arg-names="增强方法参数名"/>
2)、配置:
<aop:before pointcut-ref="onePointcut" method="before"/>
3)、打印信息:
before:------------------->>>
我说....................

4)、这里pointcut和point-ref任选一个,如果使用外部已经声明好的切入点,则直接使用point-ref引用,如果没有,则自己声明一个内部通知。method参数是指切入增强的具体方法。

5)、这里再来看下args的功能:先在切面类中加入一个带参数的before方法对应业务Bean中的say方法。
public void before2(String aa,String bb){
		System.out.println("before:------------------->>>"+aa+"===="+bb);
}
6)、修改切点表达式:这里的aa,bb用来匹配目标类中对应参数名方法
<aop:pointcut id="onePointcut" expression="execution(* com.sxit..*.*(..)) and args(aa,bb)"/>
7)、修改前置通知:这里arg-names是增强方法中与目标方法同名参数
<aop:aspect id="oneAspect" ref="helloWorldAspect">
			<aop:before pointcut-ref="onePointcut" method="before2" arg-names="aa,bb"/>
</aop:aspect>
8)、args中参数名和个数和arg-names中保持一致就是了。

 7、后置异常通知:方法执行抛出异常时会调用增强中对应方法,使用<aop:after-throwing>标签

1)、配置,这里需要加上一个throwing,这里与增强Bean中异常参数名一致
<aop:after-throwing pointcut-ref="onePointcut" method="exception" throwing="e"/>
2)、增强Bean
//方法抛出异常时执行
public void exception(Exception e){
	System.out.println("exception"+e);
}
3)、修改业务Bean,使look方法运行时抛出异常
public void look() throws Exception{
		System.out.println("我看.....................");
		throw new Exception();
}
4)、测试类:
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloWorldService hello = (HelloWorldService)ac.getBean("helloWorldService");
		
		try {
			hello.look();
		} catch (Exception e) {
			
		}
5)、运行结果:
我看.....................
exception java.lang.Exception

 8、后置返回通知:方法执行完后切入增强,抛出异常则不执行,使用<aop:after-returning>

1)、配置方式:这里的returning是增强方法中参数的名字
<aop:after-returning pointcut-ref="onePointcut" method="after" returning="o"/>
2)、业务Bean中增加一个方法:用来把返回值传递给切面Bean
public int sb(){
		System.out.println("sb+++");
		int sb = 2;
		return sb;
}
3)、修改切面Bean after方法:这里的参数o就是配置文件中的returning
public void after(Object o){
		System.out.println("after======"+o);
}
4)、打印信息:
sb+++
after======2

 9、后置最终通知,在切入点方法返回时,不论是否抛出异常均切入增强,使用<aop:after>标签声明

1)、配置:
<aop:after pointcut-ref="onePointcut" method="returnFinally" arg-names=""/>
2)、打印信息:
我看.....................
returnFinally
3)、抛出异常也会切入增强:
<aop:aspect id="oneAspect" ref="helloWorldAspect">
			<aop:after-throwing pointcut-ref="onePointcut" method="exception" throwing="ew"/>
			<aop:after pointcut-ref="onePointcut" method="returnFinally" arg-names=""/>
</aop:aspect>

public void look() throws Exception{
		System.out.println("我看.....................");
		throw new Exception();
}

hello.look();
4)、打印信息:
我看.....................
exception java.lang.Exception
returnFinally

 10、环绕通知:在切入点前后均能执行的通知,它可以决定目标方法是否执行,何时执行,执行时是否替换方法参数,执行完是否需要替换返回值,可通过<aop:around>来声明:

1)、新增环绕通知实现:
public Object around(ProceedingJoinPoint pjp){
		
		System.out.println("环绕start-----");
		Object result = null;
		try {
			result = pjp.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		System.out.println("环绕end-----");
		return result;
}
2)、配置:
<aop:around pointcut-ref="onePointcut" method="around"/>
3)、打印信息:
环绕start-----
我看.....................
环绕end-----

 

参考自:http://jinnianshilongnian.iteye.com/blog/1418598

              http://ch-space.iteye.com/blog/493956

 

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Spring AOP之基于Schema配置总结与案例

    **Spring AOP:基于Schema配置的总结与案例** 在Java企业级开发中,Spring框架以其强大的功能和灵活性深受开发者喜爱。其中,Spring AOP(面向切面编程)是解决横切关注点问题的一个重要工具,它允许我们把业务逻辑...

    Spring aop 基于schema的AOP支持及JoinPoint的使用、如何使用CGLIB代理

    Spring AOP提供了基于XML配置的元数据方式来实现切面,这被称为基于Schema的AOP支持。 1. **创建切面(Aspect)** - 切面通常是一个包含切点(Pointcut)和通知(Advice)的类。在XML配置中,我们需要定义一个`...

    基于注解配置和使用spring AOP(spring mvc框架)

    二、基于注解的AOP配置 1. **启用AOP代理**:在Spring配置文件中,通过&lt;aop:aspectj-autoproxy/&gt;元素启用基于注解的AOP。 ```xml &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...

    征服Spring AOP—— Schema

    1. `&lt;aop:config&gt;`:这是Spring AOP配置的根元素,用于开启基于XML的AOP配置。 2. `&lt;aop:pointcut&gt;`:定义一个切入点,即一组连接点。切入点表达式可以用简单的名称引用,也可以使用AspectJ的表达式语言来精确匹配...

    spring入门学习-6、AOP几种配置方式详解.pdf

    Schema-based 的 AOP 是一种基于 XML 配置的方式来实现 AOP 的方式。这种方式允许开发者在 Spring 的配置文件中直接声明 AOP 的切面、切入点以及通知等元素,而无需编写复杂的 Java 代码。 - **切面声明**:使用 `...

    Spring AOP配置源码

    此单元测试基于spring的AbstractJUnit4SpringContextTests,你需要添加spring的关于单元测试的支持 在类上标注@ContextConfiguration(locations="classpath:applicationContext.xml")意思是去classpath路径下加载...

    Spring Aop Schema实现

    本篇文章将深入探讨Spring AOP的Schema实现,即基于XML配置的方式来理解和应用AOP。 一、Spring AOP基础概念 1. 切面(Aspect):切面是关注点的模块化,例如日志、事务管理。在Spring AOP中,切面由通知(Advice...

    spring aop的两种配置方式.docx

    **一、基于XML的Spring AOP配置** 在传统的Spring AOP配置中,我们通常会在`applicationContext.xml`或类似的配置文件中定义切面、通知(advice)、切入点(pointcut)等元素。以下是一个简单的示例: ```xml ...

    基于Spring的AOP快速实现通用日志打印(csdn)————程序.pdf

    接下来,我们需要在Spring的配置文件中定义bean和aop配置。这里使用的是XML配置方式: ```xml &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...

    spring-aop-aspectj(Schema)-case

    在Spring AOP中,AspectJ有两种集成方式:一种是基于注解(@Aspect),另一种是基于XML Schema配置。本案例可能侧重于后者,即XML配置方式。这种方式通常在需要更精细控制或与老项目集成时使用。 描述中提到的"NULL...

    spring-aop.xsd

    `spring-aop.xsd`文件是Spring AOP配置的XML Schema定义文件,用于指导和验证XML配置中的元素和属性,确保其符合规范,同时也为IDE提供代码提示和自动补全功能,提高开发效率。 在Java开发中,AOP(Aspect Oriented...

    spring2-aop.pdf

    - **使用基于 Schema 的配置文件配置 Spring AOP**:使用 XML 配置文件来定义切面和通知。 - **使用 Java 5 注解配置及使用 Spring AOP**:利用注解简化 AOP 配置。 - **基于 API 方式来使用 Spring AOP**:通过编程...

    XML配置SpringAOP共2页.pdf.zip

    Spring AOP还支持基于注解的配置,这使得代码更加简洁,但本文主要关注XML配置方式。在实际应用中,你可能需要根据项目需求灵活选择配置方式。 在处理事务时,Spring AOP经常被用来实现声明式事务管理。通过定义...

    spring-aop-4.2.xsd.zip

    总的来说,这个压缩包提供了Spring AOP模块的4.2版本的XML Schema定义,对理解和调试Spring AOP配置文件非常有帮助。开发者可以通过解析和学习这个XSD文件,更好地掌握Spring AOP的配置语法,从而实现更高效和精确的...

    spring aop xml实现

    Spring AOP(面向切面编程)是Spring框架的重要组成部分,它...随着Spring的发展,基于注解的AOP配置逐渐成为主流,但理解XML配置对于深入理解AOP原理仍然非常重要。在实际项目中,可以根据需求选择合适的配置方式。

    spring-aop实例demo

    Spring AOP是基于代理的,它可以为普通Java对象(POJOs)提供拦截器模式的功能。本实例将详细介绍如何在Spring 3.2.8版本中实现AOP。 首先,我们需要理解AOP的基本概念。AOP的核心是切面(Aspect),它包含了通知...

    javaXML方式实现SpringAop编程(源码+jar包)

    本篇文章将深入探讨如何通过XML配置来实现Spring AOP,并结合提供的源码和jar包进行详细讲解。 首先,我们需要理解Spring AOP的基本概念。AOP是一种编程范式,用于将关注点(如日志、安全检查)与业务逻辑分离,...

    开发者突击·精通AOP整合应用开发 源码

    Spring AOP:以loC为基础讲解Spring下的AOP开发,讲解了3种AOP的开发方式,即使用@Aspect注释符、基于Schema的配置的开发方式和Spring API的开发方式,最后在实际的Java EE项目中实现5种Spring AOP功能(日志记录器...

    spring mvc框架下的aop例子

    这会告诉Spring容器,我们要启用基于注解的AOP,并代理所有带有切面注解的bean。配置如下: ```xml &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...

Global site tag (gtag.js) - Google Analytics