spring-aop 详细介绍 -- 方法增强

介绍 利用 spring 对方法实现增强 

1.定义一个需要增强的接口

package com.dl.code.service;

/**
 * Created with IntelliJ IDEA.
 * 作者: 代蒙恩
 * 日期: 2021/2/24
 * 时间: 10:56
 * 描述: spring练习
 * 这里面的四个方法 是等待增强的方法
 */
public interface UserService {

    public void add();
    public void delete();
    public void update();
    public void select();
}

2.实现这个接口

package com.dl.code.service.impl;

import com.dl.code.service.UserService;

/**
 * Created with IntelliJ IDEA.
 * 作者: 代蒙恩
 * 日期: 2021/2/24
 * 时间: 10:57
 * 描述: spring练习
 * \
 */
public class UserServiceImpl implements UserService {
    @Override
    public void add() {
        System.out.println("add....");
    }

    @Override
    public void delete() {
        System.out.println("delete....");
    }

    @Override
    public void update() {
        System.out.println("update....");
    }

    @Override
    public void select() {
        System.out.println("select....");
    }
}

3.定义增强类
 

package com.dl.code.advice;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * Created with IntelliJ IDEA.
 * 作者: 代蒙恩
 * 日期: 2021/2/24
 * 时间: 10:58
 * 描述: spring练习
 */
public class MyAdvice {

    public void before(){
        System.out.println("前置通知");
    }

    public void afterReturning(){
        System.out.println("后置通知");
    }

    public void round(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("环绕之前通知");
        //执行原来的方法
        joinPoint.proceed();
        System.out.println("环绕之后的通知");
    }

    public void afterThrowing(){
        System.out.println("异常通知");
    }

    public void after(){
        System.out.println("最终通知");
    }

}

4.在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: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 definitions here -->
        <!-- 需要增强的目标对象 -->
        <bean id="userService" class="com.dl.code.service.impl.UserServiceImpl"></bean>
        <!-- 提供增强 的 类对象 -->
        <bean id="myAdvice" class="com.dl.code.advice.MyAdvice"></bean>
        <!-- 使用spring aop 对目标对象的指定方法进行增强 -->
        <aop:config>
            <!--
                设置指定的连接点
                execution(public void com.dl.code.service.impl.UserServiceImpl.add()) :表示对指定包路径UserServiceImpl 下面的
                add方法进行增强
            -->
            <aop:pointcut id="pointcut" expression="execution(public void com.dl.code.service.impl.UserServiceImpl.add())"/>

            <!-- 设置增强的过程 -->
            <aop:aspect ref="myAdvice">
                <!-- 添加 前置通知 -->
                <aop:before method="before" pointcut-ref="pointcut"/>
                <!-- 添加 最终通知 -->
                <aop:after method="after" pointcut-ref="pointcut"/>
                <!-- 添加 后置通知 -->
                <aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>
                <!-- 添加 异常通知 -->
                <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/>
                <!-- 添加 环绕通知 -->
                <aop:around method="round" pointcut-ref="pointcut"/>
            </aop:aspect>
        </aop:config>
</beans>

6.测试方法是否已经被增强

package com.qf.test;

import com.dl.code.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {

    @Test
    public void testAdd(){
        //加载 配置文件
        ClassPathXmlApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取 spring 生成的对象
        UserService userService = (UserService)applicationContext.getBean("userService");
        //执行方法
        userService.add();

        userService.delete();

        /**
         * 运行结束侯,你会发现,这里的add() 方法已经被增强了,而 delete方法没有被增强
         */
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值