Spring学习11之AOP

Spring学习11之AOP,第1张

Spring学习11之AOP 一、AOP是什么?

AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。
AOP核心概念

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等…
  • 切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类。
  • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
  • 目标(Target):被通知对象。
  • 代理(Proxy) :向目标对象应用通知之后创建的对象。
  • 切入点(Pointcut) :切面通知执行的“地点"的定义
  • 连接点(JointPoint):与切入点匹配的执行点。
二、代码 1.需要的类

业务的接口

package com.shan.demo04;

public interface UserService {

    void add();

    void delete();

    void update();

    void query();

}

业务的实现类

package com.shan.demo02;
//改变业务代码在公司是大忌,因为你改动代码可能让整个项目崩溃
public class UserServiceImpl 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 query() {
        System.out.println("查询了一个用户");
    }
}

前置日志类

package com.shan.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class BeforeLog implements MethodBeforeAdvice {

    //method:要执行的目标对象的方法
    // Object[]:参数
    // target:目标对象
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了!");
    }
}

后置日志类

package com.shan.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    //returnValue:返回值
    //method:要执行的目标对象的方法
    // Object[]:参数
    // target:目标对象
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {

        System.out.println("执行了!"+target.getClass().getName()+"的"+method.getName()+"方法.返回结果为:"+returnValue);

    }
}

Spring xml配置文件




    
    
    
    
    
    
        
        
        
        
        

    


2.测试
import com.shan.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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


    }
}

com.shan.service.UserServiceImpl的add被执行了!
增加了一个用户
执行了!com.shan.service.UserServiceImpl的add方法.返回结果为:null

总结

动态代理 代理的是接口
配置aop,需要导入aop的约束
xmlns:aop="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
切入点:expression:表达式,execution(要执行的位置)

作者有话说

博客创作不易,希望看到这里的读者动动你的小手点个赞,如果喜欢的小伙伴可以一键三连,作者大大在这里给大家谢谢了。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5659389.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存