AOP在XML中的配置

applicationContext.xml中的约束

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">

配置增强的对象要增强的对象:

<bean id="UserDao" class="com.xx.spring.demo.UserDaoImpl" />

将切面类交给Spring来管理

<bean id="myAspect" class="com.xxx.spring.demo.MyAspectXML" />

通过AOP的配置完成目标类产生代理

<aop:config>
		<!-- 表达式配置哪些类的哪些方法需要进行增强  -->
		<aop:pointcut expression="execution(* com.xxx.spring.demo3.ProductDaoImpl.save(..))" id="pointcut1"/>
		<aop:pointcut expression="execution(* com.xxx.spring.demo3.ProductDaoImpl.delete(..))" id="pointcut2"/>
		<aop:pointcut expression="execution(* com.xxx.spring.demo3.ProductDaoImpl.update(..))" id="pointcut3"/>
		<aop:pointcut expression="execution(* com.xxx.spring.demo3.ProductDaoImpl.find(..))" id="pointcut4"/>
		
		<!-- 配置切面 -->
		<aop:aspect ref="myAspect">
			<!-- 前置通知  -->
			<aop:before method="checkPri" pointcut-ref="pointcut1" />
			<!-- 后置通知  -->
			<aop:after-returning method="writeLog" pointcut-ref="pointcut2" returning="result"/>
			<!-- 环绕通知  -->
			<aop:around method="around" pointcut-ref="pointcut3" />
			<!-- 异常抛出通知  -->
			<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" />
			<!-- 最终通知  -->
			<aop:after method="after" pointcut-ref="pointcut4" />
		</aop:aspect>
</aop:config>

编写增强类:

public class MyAspectXML {
	/**
	 * 前置通知
	 * @param joinpoint
	 */
	public void checkPri(JoinPoint joinpoint) {
		System.out.println("权限校验!===================="+joinpoint);
	}
	/**
	 * 后置通知
	 * @param result
	 */
	
	public void writeLog(Object result) {
		System.out.println("日志记录!===================="+result);
	}
	/**
	 * 环绕通知--性能监控.
	 * @throws Throwable 
	 */
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("环绕前通知================");
		//相当于执行目标程序
		Object object = joinPoint.proceed();
		System.out.println("环绕后通知================");
		return object;
	}
	/**
	 * 异常抛出通知
	 */
	public void afterThrowing() {
		System.out.println("异常抛出通知================");
	}
	/**
	 * 最终通知.相当于finally代码块中的内容
	 */
	public void after() {
		System.out.println("最终通知================");
	}
}

其中知识点 :
        前置通知:在目标方法执行之前进行操作
            前置通知:可以获得切入点的通知.
        后置通知:在目标方法执行之后进行操作
            后置通知:可以获得方法的返回值.
        环绕通知:在目标方法执行之前和之后进行操作
            环绕通知:可以阻止目标方法的执行.
        异常抛出通知:在程序出现异常的时候,进行的操作 .
        最终通知:无论代码是否有异常,总是会执行.


         切入点表达式写法:基于execution函数完成的.
         语法:
             1_[访问修饰符] 方法返回值 包名.类名.方法名(参数)
             2_public void com.mute.spring.CustomerDao.save(..) 其中public可以省略   *表示任意值返回类型
             3_例如:* *.*.*.*Dao.save(..)   
             4_* com.mute.spring.CustomerDao+save(..) 其中+表示当前的类和其子类都可以被增强.
             5_* com.mute.spring..*.*(..) 表示这个包以及子包下面所有类的所有方法
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值