java_spring:day06

AspectJ

通知类型:

  1. 前置通知 before
  2. 后置通知 after-returning
  3. 最终通知 after
  4. 环绕通知 around
  5. 抛出异常通知 after-throwing

写一个切面类

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect03 {

    public void before(){
        //前置通知
        System.out.println("这是前置通知。。。");
    }

    public void afterReturning(){
        //后置通知
        System.out.println("这是后置通知。。。");
    }

    public void afterThrowing(Throwable e){
        //异常通知
        System.out.println("这是异常通知。。。");
    }

    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        System.out.println("这是环绕通知。。。前");
        System.out.println(pjp.getSignature().getName());
        Object obj = pjp.proceed();
        //环绕通知
        System.out.println("这是环绕通知。。。后");

        return obj;
    }
}


//测试:
    @Test
    public void test14(){

        ApplicationContext c = new ClassPathXmlApplicationContext("beans7.xml");
        PersonService ps = (PersonService) c.getBean("personService");
        ps.addPerson();
        ps.delPerson();
    }

/*输出
这是前置通知。。。
这是环绕通知。。。前
addPerson
增加用户。。。
这是环绕通知。。。后
这是后置通知。。。
这是前置通知。。。
这是环绕通知。。。前
delPerson
这是异常通知。。。
*/

配置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: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/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">

    <!--配置PersonService-->
    <bean id="personService" class="com.hetl.day04.service.PersonServiceImpl"></bean>
    <!--配置切面对象-->
    <bean id = "myAspect" class="com.hetl.day05.aspect.MyAspect03"/>
    
    <!--配置aop-->
    <aop:config>
        <aop:aspect ref="myAspect">

            <!--定义一个切入点-->
            <aop:pointcut id="myPointcut" expression="execution(* com.hetl.day04.service.*.*(..))"/>

            <!--配置前置通知-->
            <aop:before method="before" pointcut-ref="myPointcut"></aop:before>
            <!--配置后置通知-->
            <aop:after-returning method="afterReturning" pointcut-ref="myPointcut"></aop:after-returning>
            <!--抛出异常通知-->
            <aop:after-throwing method="afterThrowing" pointcut-ref="myPointcut" throwing="e"></aop:after-throwing>
            <!--环绕通知-->
            <aop:around method="around" pointcut-ref="myPointcut"></aop:around>
        </aop:aspect>
    </aop:config>

</beans>

AspectJ基于注解

package com.hetl.day05.aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect03 {
    //声明一个公共切入点

    @Pointcut("execution(* com.hetl.day04.service.*.*(..))")
    public void myPointcut(){

    }

    @Before("execution(* com.hetl.day04.service.*.*(..))")
    public void before(JoinPoint point){
        //前置通知
        System.out.println("这是前置通知。。。"+point.getSignature().getName());
    }
    @AfterReturning("execution(* com.hetl.day04.service.*Impl.*(..))")
    public void afterReturning(){
        //后置通知
        System.out.println("这是后置通知。。。");
    }
    @AfterThrowing(pointcut = "myPointcut()",throwing = "e")
    public void afterThrowing(Throwable e){
        //异常通知
        System.out.println("这是异常通知。。。"+e.getMessage());
    }
    @Around("myPointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        System.out.println("这是环绕通知。。。前");
        System.out.println(pjp.getSignature().getName());
        Object obj = pjp.proceed();
        //环绕通知
        System.out.println("这是环绕通知。。。后");

        return obj;
    }
}


	@Test
    public void test15(){

        ApplicationContext c = new ClassPathXmlApplicationContext("beans8.xml");
        PersonService ps = (PersonService) c.getBean("personService");
        ps.addPerson();
    }

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: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/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="com.hetl.day04.service"/>
    <context:component-scan base-package="com.hetl.day05.aspect"/>
    <!--配置aop注解生效-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值