注解实现Aop(Spring学习笔记十一)

发布于:2024-04-03 ⋅ 阅读:(70) ⋅ 点赞:(0)

创建测试类:

package com.li.diy;

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

//使用注解的方式实现AOP
@Aspect  //标注这个类是一个切面
public class AnnotationPointCut {
    //@Before(接切入点)  表示在切入点之前切入下面方法
    @Before("execution(* com.li.service.UserServiceImp.*(..))")
    public void  before(){
        System.out.println("====通过注解实现Aop方法执行前====");
    }
//     @After(切入点) 表示在切入点之后切入下面方法
    @After("execution(* com.li.service.UserServiceImp.*(..))")
    public void after(){
        System.out.println("====通过注解实现Aop方法执行后====");
    }
}

配置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: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-->
    <!--配置aop: 需要先导入aop的约束 xmlns:aop="http://www.springframework.org/schema/aop"    http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd"-->
    <bean id="UserService" class="com.li.service.UserServiceImp"/>
    <bean id="log" class="com.li.log.Log"/>
    <bean id="AfterLog" class="com.li.log.AfterLog"/>

    <!--方式三通过注解实现Aop-->
    <!--注册bean-->
    <bean id="annot" class="com.li.diy.AnnotationPointCut"/>

    <!--开启注解支持-->
    <aop:aspectj-autoproxy/>
</beans>

测试类测试

package com.li.log;

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

public class TestGo {
    @Test
    public void test1(){
        //通过配置文件获取ClassPath
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //根据抽象类获取Bean
        UserService user = context.getBean("UserService", UserService.class);
        //调用方法测试
        user.add();

    }
}

还有一个环绕增强的注解 其它的都和上面一样

package com.li.diy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

//使用注解的方式实现AOP
@Aspect  //标注这个类是一个切面
public class AnnotationPointCut {

//    @Around(切入点) 表示在切入点执行环绕
//    ProceedingJoinPoint jp 这个参数用来执行切入点方法
    @Around("execution(* com.li.service.UserServiceImp.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {

        System.out.println("====通过注解实现Aop 环绕前====");
        Object proceed = jp.proceed();  //执行切入点的方法  环绕时不执行这个方法方法是不会被执行的
        Signature signature = jp.getSignature();  //获得签名
        System.out.println("signature:"+signature);
        System.out.println("====通过注解实现Aop 环绕后====");

    }
}

开启注解支持的两种方式

<!--开启注解支持   JDK实现(默认)或proxy-target-class="false"   cglib实现 proxy-target-class="true"
    一般默认的就够了-->


网站公告

今日签到

点亮在社区的每一天
去签到