Springboot之AOP面向切片编程代码demo

1. 添加依赖项

确保在你的pom.xml中添加了Spring AOP的依赖:

<dependencies>
    <!-- Spring AOP -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.3.20</version>
    </dependency>
    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.7</version>
    </dependency>
    <!-- Spring Context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.20</version>
    </dependency>
</dependencies>
2. 创建业务服务类 UserService
package com.example.demo;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    public void addUser(String username) {
        System.out.println("Adding user: " + username);
        // 模拟业务逻辑
    }
}
3. 创建切面类 LoggingAspect
package com.example.demo;

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

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.example.demo.UserService.*(..))")
    public void logBefore() {
        System.out.println("LoggingAspect: Before method execution");
    }
}
4. 配置Spring AOP
package com.example.demo;

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

@Configuration
@ComponentScan("com.example.demo")
@EnableAspectJAutoProxy
public class AppConfig {
    // 其他Bean配置(如果有)
}
5. 运行示例的主类 Application
package com.example.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        UserService userService = context.getBean(UserService.class);
        userService.addUser("John Doe");
    }
}

代码解释

  1. 依赖项

    • spring-aopaspectjweaver是实现AOP所必需的。
    • spring-context用于管理Spring容器和应用上下文。
  2. 业务服务类 UserService

    • 这是一个简单的服务类,包含一个addUser方法,模拟添加用户的操作。
  3. 切面类 LoggingAspect

    • 使用@Aspect注解标识为切面。
    • @Before注解定义了一个前置通知,在UserService的任何方法执行前打印日志。
    • 切点表达式execution(* com.example.demo.UserService.*(..))表示匹配UserService类中的所有方法。
  4. 配置类 AppConfig

    • 使用@Configuration标识为配置类。
    • @ComponentScan指定扫描的包。
    • @EnableAspectJAutoProxy启用AspectJ的自动代理功能,允许Spring处理AOP。
  5. 主类 Application

    • 创建Spring应用上下文并加载配置。
    • 获取UserService的Bean并调用其addUser方法。由于AOP的配置,调用该方法时会先执行切面的前置通知,打印日志。

运行结果

当运行上述代码时,输出将是:

LoggingAspect: Before method execution Adding user: John Doe

总结

这个简单的示例展示了如何使用Spring AOP实现面向切面编程。通过定义切面和切点,可以在不修改业务逻辑代码的情况下,添加横切关注点(如日志记录)。这种模式有助于提高代码的模块化和可维护性,特别是在大型应用中非常有用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值