Spring学习12之AOP2

本文详细介绍了Spring AOP(面向切面编程)的概念,包括横切关注点、切面、通知、目标、代理和切入点等核心概念,并通过代码示例展示了前置日志、后置日志以及自定义切入点的实现。同时,文章还探讨了注解方式实现AOP,并提供了Spring XML配置文件。通过测试类展示了AOP在实际应用中的效果,总结了AOP的不同实现方式及其执行顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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);

    }
}

自定义切入点

package com.shan.diy;

public class DiyPointCut {
    public void before(){
        System.out.println("=================方法执行前==================");
    }
    public void after(){
        System.out.println("=================方法执行后==================");

    }
}

注解实现AOP

package com.shan.diy;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect  //标注这个类是一个切面
public class AnnotationPointCut {

    @Before("execution(* com.shan.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("=====方法执行前=====");
    }

    @After("execution(* com.shan.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("=====方法执行后=====");

    }

}

Spring xml配置文件

<?xml version="1.0" encoding="UTF8" ?>
<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-->
    <bean id="userService" class="com.shan.service.UserServiceImpl"/>
    <bean id="beforeLog" class="com.shan.log.BeforeLog"/>
    <bean id="afterLog" class="com.shan.log.AfterLog"/>
    <!--配置aop方式2,自定义类-->
    <bean id="diy" class="com.shan.diy.DiyPointCut"/>
    <!--3,注解-->
    <bean id="annotation" class="com.shan.diy.AnnotationPointCut"/>
    <!--方式3,开启注解支持-->
    <aop:aspectj-autoproxy/>

    <!--配置aop方式2,自定义类-->
    <aop:config>
        <!--自定义切面,ref:要引用的类-->
        <aop:aspect ref="diy">
            <!--切入点-->
            <aop:pointcut id="point" expression="execution(* com.shan.service.UserServiceImpl.*(..))"/>
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
  
    <aop:config>
        <!--切入点-->
        <aop:pointcut id="pointcut" expression="execution(* com.shan.service.UserServiceImpl.*(..))"/>
        <!--执行环绕增加-->
        <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>
    
</beans>

2.测试

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

public class MyTest {
    @Test
    public void diyPointCutTest(){

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理 代理的是接口
        UserService userService = context.getBean("userService", UserService.class);

        userService.add();

    }
    @Test
    public void annotationPointCutTest(){
        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的实现方式:

  • 默认的执行环绕
  • 通过AOPconfig配置自定义的切入面,切入点,引用的类
  • 注解实现AOP

对比有优先级,哪个config在上面,before就最先执行,after就最后执行.这两种方式可以同时存在,个人发现对比有不同!

作者有话说

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值