`
free9277
  • 浏览: 106591 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Spring AOP配置与管理的补充—环绕通知

阅读更多

 Spring AOP 之 环绕通知

    在所有的通知类型中,环绕通知最为强大。因为它能完全控制方法的执行过程,所以可以把前一篇文章中所有通知动作都合并到一个单独的通知里。甚至可以控制是否以及何时继续执行原始方法。

    在Spring AOP中,环绕通知必须实现MethodInterceptor接口。这个接口是AOP联盟为了保持不同AOP框架之间的兼容性而定义的。当编写环绕通知时,必须记住——如果要继续执行原始方法,那么就必须调用methodInvocation.proceed()。如果忘记执行这一步,那么原始方法将不会被调用。下面的环绕通知合并了前面所创建的前置、后置和异常通知。

package org.mahz.easyaop.calculator.aop;

import java.util.Arrays;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class LoggingAroundAdvice implements MethodInterceptor {

	public Object invoke(MethodInvocation invocation) throws Throwable {
		String methodName = invocation.getMethod().getName();

		printTitle(methodName);

		System.out.println("The method " + methodName + "()begin with "
				+ Arrays.toString(invocation.getArguments()));
		try {
			Object returnValue = invocation.proceed();
			System.out.println("The Method " + methodName + "() ends with "
					+ returnValue);
			return returnValue;
		} catch (IllegalArgumentException e) {
			System.out.println(e.getMessage());
			throw e;
		}
	}

	private void printTitle(String methodName) {
		System.out.println("==========================================");
		System.out.println("============ Test " + methodName
				+ " Method =============");
		System.out.println("==========================================");
	}
}

    环绕通知类型非常强大、非常灵活,你甚至可以改变原始的方法参数值以及最终的返回结果。但是因为很容易忘记执行原始的方法,所以在使用这种通知时必须非常小心。

    因为这个通知结合了前面所有通知,所以只需要为代理指定这一个通知即可。

 

<bean id="arithmeticCalculator" class="org.mahz.easyaop.calculator.impl.ArithmeticCalculatorImpl" />
	<bean id="unitCalculator" class="org.mahz.easyaop.calculator.impl.UnitCalculatorImpl" />

	<!-- 
	<bean id="logginBeforeAdvice" class="org.mahz.easyaop.calculator.aop.LoggingBeforeAdvice" />
	<bean id="logginAfterAdvice" class="org.mahz.easyaop.calculator.aop.LoggingAfterAdvice" />
	<bean id="logginThrowsAdvice" class="org.mahz.easyaop.calculator.aop.LoggingThrowsAdvice" />
	 -->
	<bean id="logginAroundAdvice" class="org.mahz.easyaop.calculator.aop.LoggingAroundAdvice" />
	<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
	<property name="beanNames">
		<list>
			<value>*Calculator</value>
		</list>
	</property>
	<property name="interceptorNames">
		<list>
			<!-- 
			<value>logginBeforeAdvice</value>
			<value>logginAfterAdvice</value>
			<value>logginThrowsAdvice</value>
			-->
			<value>logginAroundAdvice</value>
		</list>
	</property>
</bean>

 

    执行结果:

 

==========================================
============ Test add Method =============
==========================================
The method add()begin with [4.0, 0.0]
4.0 + 0.0 = 4.0
The Method add() ends with 4.0
==========================================
============ Test sub Method =============
==========================================
The method sub()begin with [4.0, 0.0]
4.0 - 0.0 = 4.0
The Method sub() ends with 4.0
==========================================
============ Test mul Method =============
==========================================
The method mul()begin with [4.0, 0.0]
4.0 * 0.0 = 0.0
The Method mul() ends with 0.0
==========================================
============ Test div Method =============
==========================================
The method div()begin with [4.0, 0.0]
Division by zero

 

1
3
分享到:
评论

相关推荐

    Spring AOP 16道面试题及答案.docx

    Spring AOP,全称为Aspect Oriented Programming,是面向切面编程的一种编程范式,它是对传统的面向对象编程(OOP)的一种补充。在OOP中,核心是对象,而在AOP中,核心则是切面。切面是关注点的模块化,即程序中的...

    spring aop 切面添加日志

    AOP是面向对象编程(OOP)的一种补充,它关注的是“横切关注点”,比如日志、事务管理、安全检查等,这些关注点通常会横跨多个对象。在Spring中,切面由两个主要部分组成:切点(Pointcut)和通知(Advice)。切点...

    SpringAOP是什么?为什么要有SpringAOP?

    4. **简化事务管理**:Spring AOP提供了一种声明式事务管理方式,通过简单的配置就可以实现复杂的事务规则,极大地减少了手动管理事务的工作量。 5. **增强可扩展性**:切面允许添加新的功能而不修改现有代码,这...

    spring aop小实例

    3. **Spring AOP配置** - XML配置:在Spring的配置文件中定义切面、切入点表达式和通知。 - 注解配置:使用`@Aspect`、`@Pointcut`、`@Before`、`@After`等注解声明切面和通知。 4. **小实例详解** - 创建一个切...

    详解Spring AOP 拦截器的基本实现

    Spring AOP是Spring框架中处理AOP的一种实现,它允许你通过在Spring的配置文件中使用&lt;aop:config&gt;元素来配置切面、通知以及切点。Spring AOP使用代理模式和动态代理技术来拦截方法调用,根据切点表达式匹配的方法会...

    hualinux spring 3.15:Spring AOP.pdf

    根据提供的文件内容,可以提取出以下知识点: ...文档中提到的实践示例,例如前置通知、后置通知、返回通知、异常通知和环绕通知的具体编码实现,都是通过具体的代码示例来说明如何在Spring中应用AspectJ进行AOP编程。

    spring aop简单示例.rar

    2. **通知(Advice)**:通知是在特定连接点(Join Point)执行的代码,有前置通知、后置通知、异常通知、最终通知和环绕通知五种类型。例如,我们可以在方法调用前、调用后或发生异常时插入自定义代码。 3. **切入...

    深入浅析Spring AOP的实现原理1

    Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是对传统Object-Oriented Programming(面向对象编程,OOP)的一种补充。在OOP中,为了实现共享行为,如日志、权限验证和事务管理,往往需要在多个类...

    spring aop

    Spring支持五种不同类型的建议:前置通知(Before),后置通知(After),返回后通知(After Returning),异常后通知(After Throwing)和环绕通知(Around)。 3. **切点(Pointcut)**:切点是匹配特定方法执行...

    8Spring AOP盗梦空间之三 ——AfterThrowing

    Spring AOP支持五种通知类型:前置通知(Before)、后置通知(After)、返回通知(After Returning)、异常通知(After Throwing)和环绕通知(Around)。 `AfterThrowing`通知在方法抛出异常时执行,它允许我们在...

    spring+aspectjweaver-1.7.1.jar+aspectj-1.8.9.jar+aopalliance.jar

    首先,Spring AOP是Spring框架对传统面向对象编程的补充,它主要处理那些分散在应用程序各处的横切关注点,如日志、事务管理、性能监控等。通过AOP,这些关注点可以被模块化为独立的切面,从而提高代码的可读性和可...

    spring 之aop

    3. **通知(Advice)**:在特定连接点执行的代码,也就是切面的具体实现,包括前置通知、后置通知、异常通知、最终通知和环绕通知。 4. **切点(Pointcut)**:定义连接点的选择规则,用于指定哪些连接点将执行通知...

    AOP面向切面编程总结

    在Spring中,AOP代理的创建由`AopProxyFactory`根据`AdvisedSupport`对象的配置来决定。如果目标类实现了接口,则使用JDK动态代理;否则使用CGLib动态代理。 #### 六、AOP的实现示例 下面是一个简单的基于Spring ...

    java框架-Spring2复习题.docx

    - Spring AOP的通知类型包括前置通知、后置通知、环绕通知、返回通知和异常通知,不包括代理通知。 10. **Spring与Hibernate集成的误区** - 选项C中的错误描述是将SessionFactory注入到DataSource中,实际上应该...

    spring试题(含答案)

    环绕通知的目标对象需要实现的接口是 MethodInterceptor。 7. AOP 将软件系统分为两个部分:切面和业务处理。AOP 是一种设计模式,Spring 提供了一种实现。 8. 在 Spring 中配置 Bean 的 id 属性时,id 属性值不...

    王勇_JAVA教程_Spring_源代

    - 文件中的例子可能包括如何定义切面、通知类型(前置、后置、异常、环绕通知等)、切入点表达式以及如何使用Spring的代理机制实现AOP。 2. **自动装配(Autowired)** - `spring_autowrire_byName`和`spring_...

    Spring考试试卷.docx

    - Spring AOP包括前置通知、后置通知、环绕通知、返回通知和异常通知,代理通知不在这个列表中。 10. **Spring与Hibernate集成的错误说法**: - 所有选项均是关于Spring与Hibernate集成的正确说法。 11. **依赖...

    Spring第02天1

    - **MethodInterceptor**:环绕通知,包围整个方法调用,需要手动调用目标方法。 - **ThrowsAdvice**:异常抛出通知,当方法抛出异常时执行。 - **IntroductionInterceptor**:引介通知,向目标类添加新的方法和...

Global site tag (gtag.js) - Google Analytics