Spring框架实现AOP的两种方式详解

2023-01-21 0 868

目录

第一种AOP实现方式

AfterLog

package com.xxx.demo.service1;

import org.junit.After;
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    @Override
    //returnValue:返回值
    public void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println(
                \"执行了\"+method.getName()+\"返回的结果:\"+returnValue
        );
    }
}

Log

package com.xxx.demo.service1;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

//前置通知
public class log implements MethodBeforeAdvice {
    @Override
    //method:要执行的目标对象的方法 args:参数 target:目标读写
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+\"的\"+method.getName()+\"被执行了\");
    }
}

配置文件

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\"
       xmlns:aop=\"http://www.springframework.org/schema/aop\"
       xsi:schemaLocation=\"http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd\">

<!--    注册bean-->
    <bean id=\"userService\" class=\"com.xxx.demo.service1.UserServicelmp\"></bean>
    <bean id=\"log\" class=\"com.xxx.demo.service1.log\"></bean>
    <bean id=\"afterLog\" class=\"com.xxx.demo.service1.AfterLog\"></bean>


<!--    配置aop:需要导入aop的约束-->
    <aop:config>
        <!--        切入点:expression:表达式,execution(要执行的位置!* * * *)-->
        <aop:pointcut id=\"pointcut\" expression=\"execution(* com.xxx.demo.service1.UserServicelmp.*(..))\"/>

<!--        执行环绕增加  把log的类添加到切入点里面-->
        <aop:advisor advice-ref=\"log\" pointcut-ref=\"pointcut\"/>
        <aop:advisor advice-ref=\"afterLog\" pointcut-ref=\"pointcut\"></aop:advisor>
    </aop:config>
</beans>

实例调用

package com.xxx.demo.service1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext(\"applicationContext.xml\");
        //动态代理代理的是接口
        UserService userService =(UserService) context.getBean(\"userService\");
        userService.add();
        userService.delete();
        userService.select();
        userService.update();
    }
}

定义接口

package com.xxx.demo.service1;

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

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

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

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

Spring框架实现AOP的两种方式详解

第二种AOP实现方式

<?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
       https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd\">

<!--    注册bean-->
    <bean id=\"userService\" class=\"com.xxx.demo.service1.UserServicelmp\"></bean>
    <bean id=\"log\" class=\"com.xxx.demo.service1.log\"></bean>
    <bean id=\"afterLog\" class=\"com.xxx.demo.service1.AfterLog\"></bean>
<!--    方式二:自定义类-->
    <bean id=\"diy\" class=\"com.xxx.demo.service1.DiyPointCut\"></bean>
    <aop:config>
<!--        自定义切面 ref 要引用的类-->
        <aop:aspect ref=\"diy\">
<!--            切入点-->
            <aop:pointcut id=\"point\" expression=\"execution(* com.xxx.demo.service1.UserServicelmp.*(..))\"/>
<!--                通知-->
            <aop:before method=\"before\" pointcut-ref=\"point\"></aop:before>
            <aop:after method=\"after\" pointcut-ref=\"point\"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

Spring框架实现AOP的两种方式详解

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 Spring框架实现AOP的两种方式详解 http://www.20zxx.cn/463188/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务