Spring面向切面的简单示例(基于XML文件配置)

1.  建立工程,导入Spring AOP所需的jar包。


2. Spring配置文件applicationContext.xml:

<?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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<!-- 自动扫描指定包及其子包下的所有Bean类 -->
	<context:component-scan base-package="com.huey" />

	<aop:config>
		<!-- 定义一个切入点 -->
		<aop:pointcut id="myPointcut" expression="execution(* com.huey.service.*.*(..))"/>
		<!-- 定义切面,ref属性指定所引用的普通Bean作为切面Bean -->
		<aop:aspect id="logAspect" ref="logAspect">
			<!-- 定义前置通知 -->
			<aop:before pointcut-ref="myPointcut" method="doBefore"/>
			<!-- 定义后置通知 -->
			<aop:after-returning pointcut-ref="myPointcut" method="doAfterReturning" returning="rvt"/>
			<!-- 定义异常通知 -->
			<aop:after-throwing pointcut-ref="myPointcut" method="doAfterThrowing" throwing="ex"/>
			<!-- 定义最终通知 -->
			<aop:after pointcut-ref="myPointcut" method="doAfter"/>
			<!-- 定义环绕通知 -->
			<aop:around pointcut-ref="myPointcut" method="doAround"/>
		</aop:aspect>
	</aop:config>

</beans>

3. 定义切面Bean:

package com.huey.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

/**
 * 切面类
 * @author huey2672
 *
 */
@Component("logAspect")
public class LogAspect {

	/**
	 * 前置通知
	 */
	public void doBefore() {
		System.out.println("Before Advice");
	}
	
	/**
	 * 后置通知
	 */
	public void doAfterReturning(Object rvt) {
		System.out.println("AfterReturning Advice");
		System.out.println("目标方法的返回值:" + rvt);
	}
	
	/**
	 * 异常通知
	 */
	public void doAfterThrowing(Throwable ex) {
		System.out.println("AfterReturning  Advice");
		System.out.println("目标方法中抛出的异常:" + ex);
	}
	
	/**
	 * 最终通知
	 */
	public void doAfter() {
		System.out.println("After Advice");
	}
	
	/**
	 * 环绕通知
	 */
	public Object doAround(ProceedingJoinPoint jp) throws Throwable {
		System.out.println("Around Advice 在目标方法之前织入");
		// 获取目标方法的参数,可以对其进行修改
		Object[] args = jp.getArgs();
		
		// 执行目标方法,并得到返回值,可以对其进行修改
		Object rvt = jp.proceed(args);
		
		System.out.println("Around Advice 在目标方法之后织入");
		return rvt;
	}
	
}

4. 目标类:

package com.huey.service;

import org.springframework.stereotype.Component;

/**
 * 
 * @author Huey
 *
 */
@Component("helloService")
public class HelloService {

	public void sayHello(String name) {
		System.out.println("Hello " + name);
	}
}

5. 测试用例:

package com.huey.test;

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

import com.huey.service.HelloService;

/**
 * 
 * @author huey2672
 *
 */
public class AopTest {
	
	private HelloService helloService;
	
	{
		// 创建Spring容器
		ApplicationContext appCtx = 
			new ClassPathXmlApplicationContext("applicationContext.xml");
		// 通过容器获得EntityServ的实例
		helloService = appCtx.getBean("helloService", HelloService.class);
	}
	
	@Test
	public void testAop() throws Exception {
		helloService.sayHello("sugar");
	}

}

6. 结果输出:

Around Advice 在目标方法之前织入
Before Advice
Hello sugar
Around Advice 在目标方法之后织入
After Advice
AfterReturning Advice
目标方法的返回值:null


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值