spring(14)——实现aop的第一种方式,通过实现spring的接口创建前置日志和后置日志,并在beans.xml中配置并使用它们

1.准备好一个接口和实现类

  1. 接口
package com.lixv.service;

public interface UserService {
    public void add(int a);
    public void delete();
    public void update();
    public void selete();
}

  1. 实现类
package com.lixv.service;

public class UserServiceImpl implements UserService{
    @Override
    public void add(int a) {
        System.out.println("增加了一个用户");
    }

    @Override
    public void delete() {
        System.out.println("删除了一个用户");
    }

    @Override
    public void update() {
        System.out.println("更新了一个用户");
    }

    @Override
    public void selete() {
        System.out.println("查询了一个用户");
    }
}

2.创建前置日志

package com.lixv.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class BeforeLog implements MethodBeforeAdvice {

    //method要执行的目标对象的方法
    //args参数
    //target目标对象

    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}

  1. 前置日志实现spring的MethodBeforeAdvice 接口。
  2. before方法会在目标方法执行之前执行。
  3. before的method参数是目标方法的反射。
  4. before的args参数是目标方法的参数。
  5. before的target参数是目标对象。

3.创建后置日志

package com.lixv.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue+"参数是"+args[0]);
    }
}


  1. 后置日志实现spring的AfterReturningAdvice 接口。
  2. afterReturning方法会在目标方法执行之后执行。
  3. afterReturning的returnValue参数是方法的返回值。
  4. afterReturning的method参数是目标方法的反射。
  5. afterReturning的args参数是目标方法的参数。
  6. afterReturning的target参数是目标对象。

4.在beans中配置aop

<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"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

5.创建bean

<bean id="userService" class="com.lixv.service.UserServiceImpl"/>
<bean id="beforeLog" class="com.lixv.log.BeforeLog"/>
<bean id="afterLog" class="com.lixv.log.AfterLog"/>

6.使用aop标签来配置前置日志和后置日志

<aop:config>
    <aop:pointcut id="pointcut" expression="execution(* com.lixv.service.UserServiceImpl.*(..))"/>
    <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
    <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
  1. aop:pointcut的expression属性是选择那些方法,上例是UserServiceImpl中的全部方法。
  2. aop:advisor将前置日志或者后置日志配置进去。
    advice-ref指向之前实现前置日志或后置日志接口的类。
    pointcut-ref指向aop:pointcut的id。

7.测试

@Test
    public void test2(){
        ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService = (UserService) classPathXmlApplicationContext.getBean("userService");
        userService.add(5);
    }

8.运行结果

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木子 旭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值