spring 中使用 @Aspect 注解实现 aop 切面—06

该博客介绍了如何在Spring框架中使用AOP(面向切面编程)来实现日志管理。通过创建一个切面类`LogAspect`,定义了四个通知(Before、After、AfterReturning、AfterThrowing),分别在方法执行前、后以及正常返回和抛出异常时记录日志。配置文件`bean.xml`开启了Spring的AOP支持,使得切面类能够生效。在业务代码中,通过测试类展示了如何在实际操作中应用这些日志切面。

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

1、创建切面类

@Component
@Aspect
public class LogAspect {

    @Before(value = "execution(* com.cl.service..*.*(..))")
    public void before(JoinPoint joinPoint){
        //获取方法名
        String name = joinPoint.getSignature().getName();
        System.out.println("[ before ]"+name+"执行");
    }

    @After(value = "execution(* com.cl.service..*.*(..))")
    public void after(JoinPoint joinPoint){
        //获取方法名
        String name = joinPoint.getSignature().getName();
        System.out.println("[ after ]"+name+"执行");
    }

    @AfterReturning(value = "execution(* com.cl.service..*.*(..))",returning = "result")
    public void afterReturning(JoinPoint joinPoint,Object result){
        //获取方法名
        String name = joinPoint.getSignature().getName();
        System.out.println("[ afterReturning ]"+name+"方法的结果为:"+result);
    }

    @AfterThrowing(value = "execution(* com.cl.service..*.*(..))",throwing = "exception")
    public void afterThrowing(JoinPoint joinPoint,Exception exception){
        //获取方法名
        String name = joinPoint.getSignature().getName();
        System.out.println("[ afterReturning ]");
        exception.printStackTrace();
    }
}
  • @Aspecet 注解 将当前类标记为切面类
  • @Component注解 通过包扫描,将含有此标签的类注入到 IoC

2、配置 bean.xml 文件

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


    <!-- 扫描 @component、@Repository、@service、@control 等注解,扫描到了之后自动添加到 IoC 容器中 -->
    <context:component-scan base-package="com.cl"></context:component-scan>

    <!-- 开启 @aspect 注解 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    
</beans>

3、编写业务代码并测试

  • 业务接口
public interface IRandomTest {
    int runTest();
}
  • 业务接口实现
@Component
public class RandomTest implements IRandomTest {
    public int runTest() {
        System.out.println("执行了业务代码");
        return 4;
    }
}
  • 测试类
public class Text {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring.xml");
        IRandomTest randomTest = (IRandomTest) applicationContext.getBean("randomTest");
        randomTest.runTest();
    }
}

注意点: 因为业务接口实现类使用了 @Component 注解包扫描会扫描到这个注解,它会将 RandomTest 自动注入到 IoC 容器中,所以可以在测试类中直接到 IoC 容器中取出 bean 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值