Spring AOP概念学习(四)-基于schema的aop支持

本文通过实例演示了如何在Spring框架中实现面向切面编程(AOP),包括定义通知方法、设置连接点及配置AOP切面。展示了前置、后置、返回及异常通知等关键概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      看了文档,基于@JAspect支持的Spring AOP不知什么原因,编写相关的例子程序,没有运行成功,所以暂时不能写出来供大家交流,如果调试出来后,一定补上。现在先来看看Spring中另一种AOP的支持-基于Schema的AOP支持。

      和以前一样,还是先把例子程序写出来,有什么注意的地方会在稍后注明。

      1、通知

 

package com.javaeye.sunjiesh.aop.aspect;

public class AspectExample {

	/**
	 * 前置通知(Before advice)
	 */
	public void beforeAdvice() {
		System.out.println("beforeAdvice");
	}

	/**
	 * 带参数的前置通知(Before advice)
	 * @param parameter 
	 */
	public void beforeAdvice2(String parameter){
		System.out.println("beforeAdvice2");
		System.out.println("parameter is "+parameter);
	}
	
	/**
	 * 后通知(After (finally) advice)
	 */
	public void afterAdvice() {
		System.out.println("afterAdvice");
	}

	/**
	 * 返回后通知(After returning advice)
	 * @param parameter 连接点运行后返回的参数
	 */
	public void afterReturnAdvice(String parameter) {
		System.out.println("afterReturnAdvice");
		System.out.println("parameter is "+parameter);
	}

	/**
	 * 抛出异常后通知(After throwing advice)
	 * @param ex 连接点运行时发生的异常
	 */
	public void afterThrowingAdvice(Exception ex) {
		System.out.println("afterThrowingAdvice");
		System.out.print(ex.getMessage());
	}

	/**
	 * 环绕通知(Around Advice)
	 */
	public void aroundAdvice() {
		System.out.println("aroundAdvice");
	}
}
 

      2、连接点

 

package com.javaeye.sunjiesh.aop.aspect;

public class ServiceExample {

	public void adviceTest(){
		System.out.println("adviceTest is run");
	}
	
	public String  adviceTest2(){
		System.out.println("adviceTest2 is run");
		try {
			throw new Exception();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println(e.getMessage());
		}finally{
			return null;
		}			
	}
}
 

      3、spring配置文件

 

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
				http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<bean id="aspectExample" class="com.javaeye.sunjiesh.aop.aspect.AspectExample"></bean>
	<bean id="serviceExample" class="com.javaeye.sunjiesh.aop.aspect.ServiceExample"></bean>
	<bean id="serviceExample2" class="com.javaeye.sunjiesh.aop.aspect.ServiceExample2"></bean>
	
	<aop:config>
		<aop:aspect id="myAspect" ref="aspectExample">
			<aop:pointcut id="myPointcut"
				expression="execution(* com.javaeye.sunjiesh.aop.aspect.ServiceExample.*(..))" />
			<aop:before method="beforeAdvice" pointcut-ref="myPointcut" />
			<aop:after method="afterAdvice" pointcut-ref="myPointcut" />
			<aop:after-returning method="afterReturnAdvice"
				returning="parameter" pointcut-ref="myPointcut" />
			<aop:after-throwing method="afterThrowingAdvice"
				throwing="ex" pointcut-ref="myPointcut" />
		</aop:aspect>
	</aop:config>
	<aop:config>
		<aop:aspect id="myAspect2" ref="aspectExample">
			<aop:pointcut id="myPointcut2"
				expression="execution(* com.javaeye.sunjiesh.aop.aspect.ServiceExample2.adviceTest(String)) and args(parameter)" />
			<aop:before method="beforeAdvice2" pointcut-ref="myPointcut2" />
		</aop:aspect>
	</aop:config>
</beans>
 

      4、主函数

 

package com.javaeye.sunjiesh.aop.aspect;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AspectMain {

	public static void main(String args[]) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"spring-beans4.xml");
		ServiceExample serviceExample=(ServiceExample) ctx.getBean("serviceExample");
		serviceExample.adviceTest();
		serviceExample.adviceTest2();
		
		System.out.println("使用通知参数");
		ServiceExample2 serviceExample2=(ServiceExample2) ctx.getBean("serviceExample2");
		serviceExample2.adviceTest("hello");
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值