Spring AOP通知的类型+通知的案例

AOP通知描述了抽取的共性功能,根据共性功能,抽取的位置不同,最终运行代码时,要将其1添加到合理的位置

AOP通知分为五种类型

1.前置通知

before

2.后置通知

after

3.环绕通知(重点,有标准写法1)(权限校验之类的信息)

around

4.返回后通知(了解)

afterReturn

5.抛出异常后通知(了解)

afterThrowing

 由于Around能模拟出其余四个的结构,所有我们把around当作本次的重点

AOP通知类型的案例

MyAdvise通知类:

package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component    //让1配置类知道是bean
@Aspect       //让配置类知道是造Aop,去识别一下的内容
public class MyAdvise {

    @Pointcut("execution(void com.itheima.dao.BookDao.update())")
    private void a(){}

    @Pointcut("execution(int com.itheima.dao.BookDao.select())")
    private void b(){}

    //@Before("a()")
    public void before(){
        System.out.println("通知类中的before被触发");
    }

    //@After("a()")
    public void after(){
        System.out.println("通知类中的after被触发");
    }

    //@Around("a()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("通知类中的around.before被触发");
        //必须有一句话表述对原始操作的调用,因为无法预期调用的原始操作有没有异常,所以在这里卖弄,要先抛出最高异常
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("通知类中的around.after被触发");
        return proceed;
    }

    //@Around("b()")
    public Object aroundSelect(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("通知类中的around.before被触发");
        //必须有一句话表述对原始操作的调用,因为无法预期调用的原始操作有没有异常,所以在这里卖弄,要先抛出最高异常
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("通知类中的around.after被触发");
        return proceed;//返回值可以被更改,此时的返回为原始返回值
    }

    @AfterReturning("b()") //与After区别在于AfterReturning表示的是在最后
    public void afterReturning() throws Throwable {
        System.out.println("通知类中的afterReturning被触发");
    }
    @AfterThrowing("b()")//只有抛异常的时候才触发此通知
    public void afterThrowing(){
        System.out.println("通知类中的afterThrowing被触发");
    }
}

SpringConfig  Spring核心配置文件Java类

package com.itheima.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration                   //说明此文件为Spring配置类
@ComponentScan("com.itheima")    //包扫描,加载bean
@EnableAspectJAutoProxy          //启动了MyAdvise内的Aspect注解,
public class SpringConfig {
}

BookDaoImpl实现类,

package com.itheima.dao.impl;

import com.itheima.dao.BookDao;
import org.springframework.stereotype.Repository;

@Repository
public class BookDaoImpl implements BookDao {

    @Override
    public void update() {
        System.out.println("update is running.......");
    }

    @Override
    public int select() {
        System.out.println("select is running.......");
        int i = 1/0;
        return 100;
    }

}

app(mian方法)

package com.itheima;

import com.itheima.config.SpringConfig;
import com.itheima.dao.BookDao;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class App {
    public static void main(String[] args) {

        //获取Java配置类
        AnnotationConfigApplicationContext acct = new AnnotationConfigApplicationContext(SpringConfig.class);

        //获取bean
        BookDao bean = acct.getBean(BookDao.class);

        int select = bean.select();

        System.out.println(select);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是一个使用Spring AOP around通知的示例: 假设我们有一个服务类 `UserService`,其中有一个方法 `getUserById`,通过用户ID从数据库中获取用户信息。我们想要在方法执行前后添加日志记录,可以使用Spring AOP的around通知来实现。 首先,我们需要创建一个切面类 `LogAspect`,并在其中定义around通知方法 `logAround`: ```java @Aspect @Component public class LogAspect { @Around("execution(* com.example.UserService.getUserById(..))") public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { // 记录方法执行之前的日志 System.out.println("Before getUserById method execution"); // 执行目标方法 Object result = joinPoint.proceed(); // 记录方法执行之后的日志 System.out.println("After getUserById method execution"); return result; } } ``` 上述代码中,我们使用了`@Aspect`注解将类声明为切面类,并使用`@Around`注解定义了around通知方法`logAround`。在`@Around`注解中,我们指定了需要拦截的方法为`com.example.UserService.getUserById`,并使用`ProceedingJoinPoint`参数来执行目标方法。 在`logAround`方法中,我们先记录了方法执行之前的日志,然后通过`joinPoint.proceed()`执行了目标方法`getUserById`,最后记录了方法执行之后的日志,并返回了方法执行的结果。 接下来,我们需要在Spring配置文件中添加以下配置: ```xml <aop:aspectj-autoproxy /> <bean id="logAspect" class="com.example.LogAspect" /> ``` 上述配置中,`<aop:aspectj-autoproxy />`用于启用AspectJ自动代理,并通过`<bean>`标签定义了切面类`LogAspect`的bean。 最后,我们可以在`UserService`中调用`getUserById`方法并观察控制台输出的日志: ```java @Service public class UserService { public User getUserById(Long userId) { // 根据用户ID从数据库中获取用户信息 User user = userDao.getUserById(userId); return user; } } ``` 当我们调用`getUserById`方法时,控制台会输出以下日志: ``` Before getUserById method execution After getUserById method execution ``` 这说明我们成功地在方法执行前后添加了日志记录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

很丧

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

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

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

打赏作者

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

抵扣说明:

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

余额充值