基于注解的AOP开发小例子

本文通过实例演示了如何使用Spring AOP注解实现对转账业务的增强,包括创建Maven项目、定义切入点、配置切面及通知等步骤。

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

1.项目流程

1)创建Maven项目,导入AOP相关坐标
2)创建目标接口和目标类,定义切入点
3)创建通知类
4)将目标类和通知类的创建权交给spring
5)在通知类中使用注解织入关系,升级为切面类
6)开启注解扫描和自动代理
7)创建测试类

1.创建Maven项目,导入AOP相关坐标

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>aop_annotation</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
    
    <!--导入spring的context坐标,context依赖aop-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.5.RELEASE</version>
    </dependency> <!-- aspectj的织入 -->

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version> </dependency>
        
    <!--spring整合junit-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.1.5.RELEASE</version>
    </dependency>
    
    <dependency>
         <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency> </dependencies>
</project>

2.创建目标接口和目标类,定义切入点

在这里插入图片描述

  • AccountService接口
package com.kk.service;

public interface AccountService {
    public void transfer();
}

  • AccountServiceImpl实现类
package com.kk.service;

import org.springframework.stereotype.Service;

public class AccountServiceImpl implements AccountService {
    @Override
    public void transfer() {
        System.out.println("开始转账了!");
    }
}

3.创建通知类

在这里插入图片描述

4.将目标类和通知类的创建权交给spring

  • AccountServiceImpl实现类
package com.kk.service;

import org.springframework.stereotype.Service;
@Service
public class AccountServiceImpl implements AccountService {
    @Override
    public void transfer() {
        System.out.println("开始转账了!");
    }
}
  • Advice类
package com.kk.advice;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
public class MyAdvice {
    // 定义一个后置通知
    public void afterReturning(){
        System.out.println("转账过程结束");
    }
}

5.在通知类中使用注解织入关系,升级为切面类

- Advice类

package com.kk.advice;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect // 定义切面
public class MyAdvice {
    // 定义一个后置通知
    @AfterReturning("execution(* com.kk.service.AccountServiceImpl.*(..))")
    public void afterReturning(){
        System.out.println("转账过程结束");
    }
}

6.开启注解扫描和自动代理

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop = "http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/context-beans.xsd">

    <context:component-scan base-package="com.kk"></context:component-scan>

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

7.创建测试类

import com.kk.service.AccountService;
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("applicationContext.xml")
public class AopAnnoTest {
    // 因为调用了JDK动态代理类(因为实现了接口),所以这里使用AccountService
    @Autowired
    private AccountService accountService;

    @Test
    public void test(){
        accountService.transfer();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值