Spring的AOP配置详解

AOP是一种切面编程的思想,纵向抽取,横向重复使其核心。Spring提供了对AOP编程的支持,原理是Spring在对象创建时可以动态生成代理对象,而且对这个对象的指定进行逻辑加强。下面说一下AOP怎么写在配置文件中。

假设我们现在的需求是在操作数据库的代码前后加上事务处理。

首先编写目标对象类

public class UserServiceImpl implements UserService {
	@Override
	public void save() {
		System.out.println("保存用户!");
	}
	@Override
	public void delete() {
		System.out.println("删除用户!");
	}
	@Override
	public void update() {
		System.out.println("更新用户!");
	}
	@Override
	public void find() {
		System.out.println("查找用户!");
	}
}

编写通知类

public class MyAdvice {

	//前置通知
	public void before(){
		System.out.println("这是前置通知!!");
	}
	//后置通知
	public void afterReturning(){
		System.out.println("这是后置通知(如果出现异常不会调用)!!");
	}
	//环绕通知
	public Object around(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("这是环绕通知之前的部分!!");
		Object proceed = pjp.proceed();//调用目标方法
		System.out.println("这是环绕通知之后的部分!!");
		return proceed;
	}
	//异常通知
	public void afterException(){
		System.out.println("出事啦!出现异常了!!");
	}
	//后置通知
	public void after(){
		System.out.println("这是后置通知(出现异常也会调用)!!");
	}
}

配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

<!-- 准备工作: 导入aop(约束)命名空间 -->
<!-- 1.配置目标对象 -->
	<bean name="userService" class="test.service.UserServiceImpl" ></bean>
<!-- 2.配置通知对象 -->
	<bean name="myAdvice" class="test_springaop.MyAdvice" ></bean>
<!-- 3.配置将通知织入目标对象 -->
	<aop:config>
		<!-- 配置切入点 -->
		<aop:pointcut expression="execution(* test.service.*ServiceImpl.*(..))" id="pc"/>
		<aop:aspect ref="myAdvice" >
			<!-- 指定名为before方法作为前置通知 -->
			<aop:before method="before" pointcut-ref="pc" />
			<!-- 后置 -->
			<aop:after-returning method="afterReturning" pointcut-ref="pc" />
			<!-- 环绕通知 -->
			<aop:around method="around" pointcut-ref="pc" />
			<!-- 异常拦截通知 -->
			<aop:after-throwing method="afterException" pointcut-ref="pc"/>
			<!-- 后置 -->
			<aop:after method="after" pointcut-ref="pc"/>
		</aop:aspect>
	</aop:config>
</beans>

开始配置

首先要导入SpringAOP的命名空间,然后把目标对象和通知类配置到配置文件中,然后配置将通知织入到目标对象。在<config>的标签下进行配置,先配置切入点<pointcut>,切入点是你想要增强和已经增强的方法,id属性指定这个切点的一个名称,expression属性必须用表达式来写,格式为execution(完整方法名),如(public void test.service.UserServiceImpl.save()),表示一个公用的返回值为void的方法save()作为想要增强的方法,public可以不写,因为这是个默认值,而且增强private方法意义不太大。在表达式中,如果我们想一次对多个方法进行增强,可以用*代替一些文字,达到通配的效果,例如

     void test.service.UserServiceImpl.save():去掉public

    * test.service.UserServiceImpl.save():返回值任意

    * test.service.UserServiceImpl.*():返回值任意且对est.service.UserServiceImp类下所有空参方法进行增强。

    *test.service.*ServiceImpl.*(..):返回值任意且对est.service包中以ServiceImpl为结尾的类下所有方法(参数任意)增强。

    * test.service..*ServiceImpl.*(..):返回值任意且对est.service包和其子包中以ServiceImpl为结尾的类下所有方法(参数任意)增强。

然后配置<aspect>,其配置的主要目的是把通知织入切入点中,首先通过ref属性引入配置好的通知类,然后通过三个参数确定要织入的对象和位置

aop:指定位置,有五个候选字 
      before:前置通知,目标方法运行之前调用
      alter-Returning:后置通知(如果出现异常不会调用),在目标方法运行之后调用
      around:环绕通知在目标方法之前和之后都调用
      after-throwing:异常拦截通知,如果出现异常,就会调用
      after:后置通知(无论是否出现 异常都会调用),在目标方法运行之后调用

method:选定要织入到切入点的通知类中的方法。(注:环绕通知的方法编写时要确认环绕点)

pointcut-ref:选定切入点依赖对象,值为配置切入点时的id属性。

这样我们在获取目标对象是,获取到的就是Spring动态代理后进行增强过的代理对象。

猜你喜欢

转载自blog.csdn.net/yan245294305/article/details/81280682