AOP增强

AOP联盟为通知Advice定义了org.aopalliance.aop.Interface.Advice

Spring按照通知Advice在目标类方法的连接点位置,可以分为5类

1、前置通知org.springframework.aop.MethodBeforeAdvice

	在目标方法执行前实施增强

2、后置通知org.springframework.aop.AfterReturningAdvice

	在目标方法执行后实施增强

3、环绕通知org.aopalliance.intercept.MethodInterceptor

	在目标方法执行前后实施增强

4、异常抛出通知org.springframework.aop.ThrowsAdvice

	在方法抛出异常后实施增强

5、最终通知

	目标方法执行之后执行的通知

通过案例来讲解

有接口的案例

  • BeforeAdvice类
public class BeforeAdvice implements MethodBeforeAdvice {
   
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
   
        System.out.println("前置增强==================");
    }
}
  • StudentDao 类
public interface StudentDao {
   
    public void demo1();
    public void demo2();
}

  • StudentDaoImpl 类
public class StudentDaoImpl implements StudentDao {
   
    @Override
    public void demo1() {
   
        System.out.println("查询...");
    }
    @Override
    public void demo2() {
   
        System.out.println("保存...");
    }
 
}

  • 创建一个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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--配置目标类======================-->
    <bean id="studentDao" class="com.aop.demo.StudentDaoImpl"></bean>

    <!--前置通知类型=================-->
    <bean id="myBeforeAdvice" class="com.aop.demo.BeforeAdvice"></bean>

    <!--Spring的aop产生代理对象-->
    <bean id="studentDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置的目标类-->
        <property name="target" ref="studentDao"></property>
        <
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值