Spring-AOP实现二(自定义类实现AOP与注解实现AOP)

##一、 方式二:Spring自定义类来实现Aop

依然完成我们之前的demo,实现userServiceImpl日志功能
在这里插入图片描述

  1. 写我们自己的一个切入类
public class DiyPointCut {
    public void before(){
        System.out.println("======方法执行前======");
    }
    public void after(){
        System.out.println("======方法执行后======");
    }
}
  1. 去spring中配置
    切面—对应一个类,之前方式一的配置是切入点,这里是切面
<?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
        https://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">

<!--  注册bean-->
    <bean id="userService" class="com.lding.service.UserServiceImpl"></bean>
    <bean id="log" class="com.lding.log.Log"></bean>
    <bean id="afterlog" class="com.lding.log.AfterLog"></bean>

<!--    方式二:自定义类实现-->
    <bean id="diy" class="com.lding.diy.DiyPointCut"></bean>
    <aop:config>
<!--        自定义切面,ref为要引用的类 -->
        <aop:aspect ref="diy">
<!--            切入点-->
            <aop:pointcut id="point" expression="execution(* com.lding.service.UserServiceImpl.*(..))"/>
<!--            通知-->
            <aop:before method="before" pointcut-ref="point"></aop:before>
            <aop:after method="after" pointcut-ref="point"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>
  1. 测试
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是接口
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

测试结果
在这里插入图片描述

二、利用注解方式实现AOP

  1. 编写一个注解实现的增强类
//方式三:使用注解方式实现aop
@Aspect//标注这个类是一个切面
public class AnnotationPointcut {
    @Before("execution(* com.lding.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("-----方法执行前-------");
    }
    @After("execution(* com.lding.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("-----方法执行后-------");
    }
    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点;
    @Around("execution(* com.lding.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前:");
        Signature signature = joinPoint.getSignature();//获得签名
        System.out.println("signature:"+signature);
        //执行方法
        Object proceed = joinPoint.proceed();

        System.out.println("环绕后");
        System.out.println(proceed);
    }
}
  1. 第二步:在Spring配置文件中,注册bean,并增加支持注解的配置
    <!--方式三-->
    <bean id="annotationPointCut" class="com.lding.config.AnnotationPointcut"></bean>
<!--    开启注解支持!-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

aop:aspectj-autoproxy:说明

通过aop命名空间的<aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面
的bean创建代理,织入切面。当然,spring 在内部依旧采用
AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被
<aop:aspectj-autoproxy />隐藏起来了
<aop:aspectj-autoproxy />有一个proxy-target-class属性,默认为false,表示使用jdk动态
代理织入增强,当配为<aop:aspectj-autoproxy poxy-target-class="true"/>时,表示使用
CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接
口,则spring将自动使用CGLib动态代理。

如果对您有帮助,免费的赞点一个~~~感谢🙏

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值