Spring的AOP配置(使用配置文件配置)

一、什么是AOP?

  AOP的全称是Aspect Orient Programming ,翻译过来就是“面向切面编程”的意思。所谓切面,通俗的讲就是一个球体被切了一刀得出的一个横切面。

二、为什么要使用AOP?

  在项目中,我们把项目当成一个球体,然后给它一个切面,我们只有处理这个切面,就能处理这个切面上的东西。例如我们进行事务处理的时候,难道每一个方法我们都要给它一个try{}catch{}吗?要是有100个方法难道就要写100个try{}catch{}吗?
  这时候我们就要通过AOP来解决这个难题了,我们只需要给方法一个切面,然后就可以将其单独抽出来,抽象为单独的模块。我们只要在这个模块里写一个try{}catch{}就能实现多个方法的处理。
  简单来说,AOP就是可以处理在开发中,在多个模块间有某段重复的代码。

三、切面类

public class MyAspec{
	public boolean before(JoinPoint jp) {
		System.out.println("before");
		return true;
	}
		
	public Object around(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("before around==========");
		Object result = pjp.proceed();
		System.out.println("after around==========");
		System.out.println("在" + new Date() + "使用参数" + Arrays.deepToString(pjp.getArgs()) + "调用了"+ pjp.getSignature().getName() + "并且得到了" + result + "结果");
		return result;
	}

	public void afterRunning(JoinPoint jp, Object result) {
		System.out.println("afterReturning");
	}

	public void afterThrowing(JoinPoint jp, Exception e) {
		System.out.println("afterThrowing");
	}
	
	public void after(JoinPoint jp) {
		System.out.println("after");
	}
}
		

四、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"
	 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
	  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

<!-- 开启注解扫描 base-package 从哪一个包开始扫描类型是字符串 但是如果要从多个包开始扫描,中间可以用,隔开,而且支持通配符 -->
<context:component-scan base-package="net.seehope"></context:component-scan>

<!-- 开启对切面的注解支持 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

<bean id="myAspec" class="net.seehope.spring.aspect.MyAspec"></bean>
<!--面向切面的编程 最主要的目的就是批量处理或者在不影响源代码的情况下,单独修改某个方法的逻辑 -->
<aop:config>
	<aop:aspect ref="myAspec">
		<!-- execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)
		  表示对方法签名进行选择
		  execution(* net.seehope..*.service.impl.*.*(..))
		  * 表示返回类型任意
		  net.seehope..*.sevice.impl 表示方法的全称以net.seehope 开头,以service.impl结尾即可,中间可以省略任意层级
		  .*任意类名
		  .*类中的任意方法
		  (..)表示参数可以有任意个任意类型 -->

		<aop:pointcut expression="execution(* net.seehope..*.service.impl.*.*(..))" id="servicePc"/>
		<aop:before method="before" pointcut-ref="servicePc"/>
		 <aop:around method="around" pointcut-ref="servicePc"/>
		<aop:after-returning method="afterRunning" returning="result" pointcut-ref="servicePc"/>
		<aop:after-throwing method="afterThrowing" throwing="e" pointcut-ref="servicePc"/>
		<aop:after method="after" pointcut-ref="servicePc"/>
	 </aop:aspect>
</aop:config>
</beans>		
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值