008 spring aop(通知)(注解)

本文介绍了如何使用SpringAOP在Java中创建LogAspect,该切面在StudentServiceImpl的所有方法执行前后及异常处理时插入日志记录,展示了前置、后置和环绕通知的使用。
摘要由CSDN通过智能技术生成

LogAspect.java


package com.aistart.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;


@Configuration
@EnableAspectJAutoProxy
@Aspect
/*
* <aop:config>
        <aop:aspect ref="logAspect">
*
* */
public class LogAspect {

    /*确定切点
    *
    *   <aop:pointcut id="studentServiceCut" expression="execution(* com.aistart.service.impl.StudentServiceImpl.*(..))"/>
    * */
    @Pointcut("execution(* com.aistart.service.impl.StudentServiceImpl.*(..))")
    public void allServiceCut(){

    }

    @After("allServiceCut()")
    public void LogInfoAfter(){
        System.out.println("后置日志");
    }
    @Before("allServiceCut()")
    public void LogInfoBefore(){
        System.out.println("前置日志");
    }

    @AfterReturning(pointcut = "allServiceCut()",returning = "result")
    public void doAfterReturning(String result) {
        System.out.println("后置通知, 返回值: " + result);
    }
    @AfterThrowing(pointcut = "allServiceCut()",throwing = "e")
    public void doAfterThrowing(Exception e) {
        System.out.println("后置通知, 异常是: " + e);
    }
    @Around("allServiceCut()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("-----------------------");
        System.out.println("环绕通知: 进入方法");
        Object o = pjp.proceed();
        System.out.println("环绕通知: 退出方法");
        return o;
    }
}

StudentServiceImpl.java



package com.aistart.service.impl;

import com.aistart.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;


@Service
public class StudentServiceImpl implements StudentService {


    @Override
    public void insertStudent() {

        System.out.println("加入一个学生的业务");
//        System.out.println(1/0);

    }

    @Override
    public String reStudent() {
        return "hello";
    }
}

StudentService.java


package com.aistart.service;

public interface StudentService {

    void insertStudent();

    String reStudent();
}


annoApplicationContext.java


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



    <!--目标类-->
    <bean id="studentService" class="com.aistart.service.impl.StudentServiceImpl"/>

    <!--增强类/切面对象-->
    <bean id="logAspect" class="com.aistart.aspect.LogAspect"/>
    <!-- bean definitions here -->



    <!--支持注解的自动代理-->
    <aop:aspectj-autoproxy/>

</beans>





StudentServiceImplTest.java


package com.aistart.service.impl;

import com.aistart.service.StudentService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.Assert.*;

public class StudentServiceImplTest {

    @Test
    public void insertStudent() {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("annoApplicationContext.xml");


        StudentService studentService = context.getBean( StudentService.class);

        studentService.reStudent();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

简 洁 冬冬

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

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

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

打赏作者

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

抵扣说明:

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

余额充值