AOP学习二——注解方式

注解方式

注解配置AOP,大致可以分为四步:
1.开发需要被拦截的类。
2.使用注解@Aspect来定义一个切面。
3.使用注解@Pointcut定义切入点表达式
4.在使用入口的类中开启注解方式。
示例:
需要被拦截的类

//接口
public interface Calc {
    void say(String name);
    int aa(int a,int b);
}
//实现接口的类
@Component
public class CalcLate implements Calc{
    @Override
    public void say(String name) {
        System.out.println("你好啊!!!");
    }
    @Override
    public int aa(int a, int b) {
        //异常通知
        int i=9/0;
        return a+b;
    }
}

切入点表达式

@Component
public class Pointcuta {
    /*
    通过@Pointcut注解定义切入点表达式
     */
    @Pointcut("execution(* com.lanou3g.spring.zhujie..*.*(..))")
    public void as(){}

}

切面类

@Aspect // 表示该类是一个切面
@Component  // Aspect切面首先必须也是一个普通的bean
public class CalcAspect {
    //环绕通知
    @Around("com.lanou3g.spring.zhujie.Pointcuta.as()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("开始执行环绕通知");
        Object ret=joinPoint.proceed();
        System.out.println("执行结束环绕通知");
        return ret;
    }
    //后置通知
    @After("com.lanou3g.spring.zhujie.Pointcuta.as()")
    public void af(){
        System.out.println("后置通知");
    }
    //前置通知
    @Before("com.lanou3g.spring.zhujie.Pointcuta.as()")
    public void before(){
        System.out.println("前置通知");
    }
    //返回通知
    @AfterReturning("com.lanou3g.spring.zhujie.Pointcuta.as()")
    public void returning(){
        System.out.println("返回通知");
    }
    //异常通知
    @AfterThrowing("com.lanou3g.spring.zhujie.Pointcuta.as()")
    public void throwss(){
        System.out.println("异常通知");
    }
}

使用入口

@Configuration
@ComponentScan(basePackages = "com.lanou3g.spring.zhujie")  
@EnableAspectJAutoProxy //开启注解方式的aop
public class AppZhuJie {
    public static void main(String[] args) {
        ApplicationContext a=new AnnotationConfigApplicationContext(AppZhuJie.class);
        Calc calc=a.getBean(Calc.class);
        calc.say("张三");
        calc.aa(8,4);
    }
}

主要使用的注解:
1.@Aspect 表示该类是一个切面
2.@Pointcut 定义切入点表达式
3.@Component 表示xml中bean
4.@Configuration 表示xml中的beans
5.@ComponentScan 扫秒路径
6.@EnableAspectJAutoProxy 开启注解方式aop
以及5个通知 @Around,@After,@Before,@AfterReturning,@AfterThrowing。
注解的方式使用起来比xml方式的方便一些,但是注解方式对代码有很大的侵入性,如果要修改代码会很麻烦。

Spring AOPSpring框架中的一个重要模块,它提供了面向切面编程(AOP)的支持。AOP是一种编程思想,它可以在不改变原有代码的情况下,通过在程序运行时动态地将代码“织入”到现有代码中,从而实现对原有代码的增强。 Spring AOP提供了基于注解AOP实现,使得开发者可以通过注解方式来定义切面、切点和通知等相关内容,从而简化了AOP的使用。 下面是一个基于注解AOP实现的例子: 1. 定义切面类 ```java @Aspect @Component public class LogAspect { @Pointcut("@annotation(Log)") public void logPointcut() {} @Before("logPointcut()") public void beforeLog(JoinPoint joinPoint) { // 前置通知 System.out.println("执行方法:" + joinPoint.getSignature().getName()); } @AfterReturning("logPointcut()") public void afterLog(JoinPoint joinPoint) { // 后置通知 System.out.println("方法执行完成:" + joinPoint.getSignature().getName()); } @AfterThrowing(pointcut = "logPointcut()", throwing = "ex") public void afterThrowingLog(JoinPoint joinPoint, Exception ex) { // 异常通知 System.out.println("方法执行异常:" + joinPoint.getSignature().getName() + ",异常信息:" + ex.getMessage()); } } ``` 2. 定义业务逻辑类 ```java @Service public class UserService { @Log public void addUser(User user) { // 添加用户 System.out.println("添加用户:" + user.getName()); } @Log public void deleteUser(String userId) { // 删除用户 System.out.println("删除用户:" + userId); throw new RuntimeException("删除用户异常"); } } ``` 3. 在配置文件中开启AOP ```xml <aop:aspectj-autoproxy/> <context:component-scan base-package="com.example"/> ``` 在这个例子中,我们定义了一个切面类LogAspect,其中通过@Aspect注解定义了一个切面,通过@Pointcut注解定义了一个切点,通过@Before、@AfterReturning和@AfterThrowing注解分别定义了前置通知、后置通知和异常通知。 在业务逻辑类中,我们通过@Log注解标注了需要增强的方法。 最后,在配置文件中,我们通过<aop:aspectj-autoproxy/>开启了AOP功能,并通过<context:component-scan>扫描了指定包下的所有组件。 这样,当我们调用UserService中的方法时,就会触发LogAspect中定义的通知,从而实现对原有代码的增强。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值