AOP面向切面编程

本文介绍了AOP(面向切面编程)的概念,作为对OOP(面向对象编程)的补充。通过示例展示了如何在不修改原有类代码的情况下,使用aspectj框架在Java中实现AOP,包括定义切面类、前置通知和后置通知的使用,并提供了Spring框架配置和测试用例。
摘要由CSDN通过智能技术生成

AOP面向切面编程

导读:曾经在学习Java的时候,我们刚开始接触到的是OOP面向对象的思想。

OOP:面向对象编程-我们定义一个类,在里面设置一些属性、方法,我们在其他类只需要new一下对象就能访问类的属性、方法。

何为面向切面编程呢AOP?

AOP:面向切面编程-我们定义了很多类(A、B类),这些类里面有很多方法,这个时候我们在不去动这些源代码的情况下去增加功能,增加业务,我们会设置一个 切面类(C类),在这个类里增加我们需要的新功能。在这里插入图片描述

AOP的实现方式?

通过框架aspectj 实现。

案例:

定义如下结构:
在这里插入图片描述

public interface UsbInterface {
    void work();
}
@Component
public class Phone implements UsbInterface{
    @Override
    public void work() {
        System.out.println("this is a phone");
    }
}
@Component
public class Camera implements UsbInterface{
    @Override
    public void work() {
        System.out.println("this is a camera");
    }
}

定义切面类:SmartTechAspect

@Aspect
@Component
public class SmartTechAspect {

    //AOP切入表达式
    @Before(value="execution(public void com.linghu.aop.usb.Phone.work())")
    public void showBeginLog(JoinPoint joinPoint){
        System.out.println("前置通知-日志信息~");
        Signature signature = joinPoint.getSignature();
        System.out.println("日志-方法名:"+signature.getName());
    }

    @After(value = "execution(public void com.linghu.aop.usb.Camera.work())")
    public void showFinallydLig(JoinPoint joinPoint){
        System.out.println("后置通知信息~");
        Signature signature = joinPoint.getSignature();
        System.out.println("日志-方法名:"+signature.getName());
    }
}

在这里 @Aspect就说明该类是一个切面类,再通过 @Component将该类自动注入容器。

@Before表示前置通知, value="execution(public void com.linghu.aop.usb.Phone.work())"表示将方法 showBeginLog()插入到 com.linghu.aop.usb.Phone.work()之前。

这里要认识到:

  • 前置通知:@Before
  • 返回通知:@AfterReturning
  • 异常通知:@AfterThrowing
  • 后置通知:@After
  • 环绕通知:@Around

一定要做的bean处理:

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


    <context:component-scan base-package="com.linghu.aop.usb"/>//自动扫描包

    <aop:aspectj-autoproxy/>


</beans>

<aop:aspectj-autoproxy/>一定要添加,它表示开启注解AOP的功能,有了它,切面类的@Aspect注解才能起作用。

做一下测试:

public class AopAspectjTest {
    @Test
    public void phoneTestByProxy() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
//通过接口来
        UsbInterface phone = (UsbInterface) ioc.getBean("phone");
        UsbInterface camera = (UsbInterface) ioc.getBean("camera");
        phone.work();
        camera.work();
        System.out.println("-----------------");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CodeLinghu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值