【Spring】注解方式实现AOP

本文介绍了如何使用注解在Spring框架中实现面向切面编程(AOP)。首先创建目标接口和目标类,然后创建切面类并使用@Aspect、@Before等注解配置增强逻辑。接着在配置文件中启用组件扫描和AOP的自动代理。最后通过测试类验证AOP的前置、后置、环绕通知等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前介绍了如何用xml方式实现AOP,这次介绍一下如何用注解方式实现。

步骤如下:

1. 创建目标接口和目标类

2. 创建切面类

3. 将目标类和切面类的对象创建权交给spring

4. 在切面类中使用注解配置织入关系

5. 在配置文件中开启组件扫描和AOP的自动代理

创建目标接口和目标类同时将目标类对象创建权交给spring

package cn.leohams.anno;

public interface TargetInterface {
    public void save();
}
package cn.leohams.anno;

import org.springframework.stereotype.Component;

@Component("target")
public class Target implements TargetInterface {
    @Override
    public void save() {
        System.out.println("nihao, save...");
    }
}

创建切面类同时将切面类对象创建权交给spring,在切面类中使用注解配置织入关系

package cn.leohams.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component("myAspect")
@Aspect
public class MyAspect {

    @Before("pointcut()")
    public void before(){
        System.out.println("前置增强...");
    }

    public void afterReturning(){
        System.out.println("后置增强...");
    }

    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");
        Object proceed = pjp.proceed();
        System.out.println("环绕后增强...");
        return proceed;
    }

    public void  afterThrowing() {
        System.out.println("抛出异常");
    }

    //定义一个切点表达式
    @Pointcut("execution(void cn.leohams.anno.*.*(..))")
    public void pointcut(){
    }
}

在配置文件中开启组件扫描和AOP的自动代理

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="cn.leohams.anno"/>

<!--    配置aop代理-->
    <aop:aspectj-autoproxy/>
</beans>

测试:

package com.leo.test;

import cn.leohams.anno.Target;
import cn.leohams.anno.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
public class AnnoTest {
    @Autowired
    private TargetInterface target;
    @Test
    public void test1(){
        target.save();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Leo&&Eva

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

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

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

打赏作者

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

抵扣说明:

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

余额充值