Spring中AOP下的AspectJ基于xml配置使用

Spring中AOP下的AspectJ使用

1、导包:spring-aspects-4.3.18.RELEASE.jar(新版IDEA创建Spring项目时会自动下载这个包,没有的就需要下载,确认有aspects这个包)

2、配置:

<?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:annotation-config/>
    <!--注解的位置-->
    <context:component-scan base-package="com.hwt"></context:component-scan>

    <aop:config proxy-target-class="true">
        <aop:aspect ref="myAspect2">
            <aop:pointcut id="myPointcut" expression="execution(* com.hwt.service.*.*(..))"/>
            <!--配置前置通知-->
            <aop:before method="myBefore" pointcut-ref="myPointcut"/>
            <!--配置后置通知-->
            <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="retValue"/>
            <!--配置环绕通知-->
            <aop:around method="myAround" pointcut-ref="myPointcut"/>
            <!--配置异常通知-->
            <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="throwable"/>
            <!--配置最终通知-->
            <aop:after method="myAfter" pointcut-ref="myPointcut" />
        </aop:aspect>
    </aop:config>
</beans>

3、切面类(增强类):

PS:此种方法不用继承类

package com.hwt.service;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

@Component("myAspect2")
public class MyAspect2{

    public void myBefore(){
        System.out.println("myBefore前置通知。。。");
    }
    public void myAfter(){
        System.out.println("myAfter最终通知。。。");
    }

    public void myAfterReturning(JoinPoint joinPoint,Object retValue) throws Throwable {
        System.out.println("myAfterReturning后置通知+返回值。。。"+retValue);
    }

    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("myAround环绕通知。。。");
        System.out.println("myAround开启事务。。。");
        System.out.println("myAround关闭事务。。。");
        proceedingJoinPoint.proceed();
        return proceedingJoinPoint.proceed();
    }

    public void myAfterThrowing(JoinPoint joinPoint,Throwable throwable) throws Throwable {
        System.out.println("myAfterThrowing异常通知+异常。。。"+ throwable.getMessage());
    }
}

4、原业务类:

package com.hwt.service;

import org.springframework.stereotype.Service;

@Service("studentService")
public class StudentService {
    public void delete(){
        System.out.println("删除学生!");
    }
    public void add(){
        System.out.println("添加学生!");
    }
    public void update(){
        System.out.println("修改信息!");
    }
}

5、测试类:

package com.hwt.test;

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

public class Test05 {
    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans05.xml");

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

        studentService.add();
//        studentService.delete();
//        studentService.update();
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值