Spring - aop-配置文件实现

1.增强类(通知 / 增强)

public class MyBook {

    public void before1() {
        System.out.println("前置增强........");
    }
    
    public void after1() {
        System.out.println("后置增强.......");
    }
    
    // 环绕通知
    public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 方法之前
        System.out.println("方法之前.........");
        
        // 执行被增强的方法
        proceedingJoinPoint.proceed();
        
        // 方法之后
        System.out.println("方法之后.........");
    }
}

2.配置文件applicationContext.xml

<!-- 1.配置对象 -->
<bean id="myBook" class="com.bjxb.aop.MyBook"></bean>

<!-- 2.配置AOP操作 -->
<aop:config>
    <!-- 2.1配置切入点 -->
    <aop:pointcut expression="execution(* com.levi..*.*(..))" id="pointcut1"/>
    
    <!-- 2.2配置切面
        把增强用到方法上面
     -->
    <aop:aspect ref="myBook">
        <!-- 配置增强类型 
            method:增强类中使用哪个方法作为前置
        -->
        <aop:before method="before1" pointcut-ref="pointcut1"/>    
        
        <aop:after method="after1" pointcut-ref="pointcut1"/>
        
        <aop:around method="around1" pointcut-ref="pointcut1"/>
    </aop:aspect>
</aop:config>

posted on 2021-12-08 16:37  每天积极向上  阅读(64)  评论(0编辑  收藏  举报

导航