AOP的Annotation注解配置

  1. 引入maven依赖

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>6.0.3</version>
    </dependency>
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.19</version>
    </dependency>
  1. 创建通知类代码

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

@Component
@Aspect
public class MyAdvice4 {
    //配置统一切点表达式
    @Pointcut("execution(void org.example.service.Impl.*.*(..))")
    public void myPointcut() {}
    @Before("MyAdvice4.myPointcut()")
    public void beforeAdvice() {
        System.out.println("前置增强");
    }
    @AfterReturning("MyAdvice4.myPointcut()")
    public void afterAdvice() {
        System.out.println("后置增强");
    }
    @Around("MyAdvice4.myPointcut()")
    public Object around(ProceedingJoinPoint joinPoint) {
        Object proceed = null;
        try {
            System.out.println("环绕通知的前置通知。。。");
            proceed = joinPoint.proceed();
            System.out.println("环绕通知的后置通知。。。");
        } catch (Throwable e) {
            System.out.println("异常通知。。。");
            ;
        } finally {
            System.out.println("最终通知。。。");
        }
        return proceed;
    }
    @AfterThrowing(pointcut = "MyAdvice4.myPointcut()", throwing = "e")
    public void afterThrowingAdvice(Throwable e) {
        System.out.println("异常信息为:" + e);
    }
}

3、创建增强类接口代码

package org.example.service;

public interface UserService {
    void show();
}

4、创建增强类实现类代码

package org.example.service.Impl;

import org.example.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public void show() {
        System.out.println("我是增强类代码");
    }
}

5、创建配置类

package org.example.config;

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

@Configuration          //替代配置文件
@ComponentScan(basePackages = "org.example")          //替代包扫描
@EnableAspectJAutoProxy         //使能够开启代理
public class SpringConfig {
}

6、编写测试类

import org.example.config.SpringConfig;
import org.example.service.UserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ApplicationContextTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = applicationContext.getBean(UserService.class);
        userService.show();
    }
}

7、测试结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值