Spring学习(六)——基于xml配置文件进行AOP编程

本次进行Spring的AOP编程是基于xml配置的,算是对之前通过JavaConfig进行AOP编程的一个补充。将之前通过javaConfig进行AOP编程的链接放上,以便对比的学习。

一、主体类

这是一个普通的类,有一个方法将来作为我们的“切点”。
Car接口

/**
 * 车
 *
 * @author ACE_GJH
 */
public interface Car {

    /**
     * 驾驶
     */
    void drive();

}

Bicycle类:

package cool.gjh.beans;

import org.springframework.stereotype.Component;

/**
 * 自行车
 *
 * @author ACE_GJH
 */
@Component
public class Bicycle implements Car{
    /**
     * 驾驶
     */
    @Override
    public void drive() {
        System.out.println("骑自行车开始兜风......");
    }
}

二、编写通知类

根据自己的需要,实现前置通知、后置通知、环绕通知和异常通知的接口方法。

package cool.gjh.aspect;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;

import java.lang.reflect.Method;

/**
 * 通知类
 * <p>
 * 驾驶过程的提醒
 * </p>
 *
 * @author ACE_GJH
 */
public class DrivingNotes implements MethodBeforeAdvice, AfterReturningAdvice, MethodInterceptor, ThrowsAdvice {

    /**
     * 前置通知的方法
     *
     * @param method  要调用的方法
     * @param objects 要调用的方法参数
     * @param o 方法调用的对象(可能为null)
     * @throws Throwable 抛出异常
     */
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("出发前:注意安全");
    }

    /**
     * 后置通知
     * @param o 方法返回值
     * @param method 调用的方法
     * @param objects 调用的方法参数
     * @param o1 方法调用的对象(可能为null)
     * @throws Throwable 抛出的异常
     */
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("到达后:停放好车辆");
    }

    /**
     * 环绕通知
     * @param mi 方法调用联结点
     * @return 方法调用的结果
     * @throws Throwable 抛出的异常
     */
    @Override
    public Object invoke(MethodInvocation mi) throws Throwable {
        System.out.println("百度地图持续为您导航......");
        Object proceed = mi.proceed();
        System.out.println("百度地图结束导航......");
        return proceed;
    }

    /**
     * 异常通知
     *
     * @param e 异常对象
     */
    public void afterThrowing(Exception e) {
        System.out.println("检查到系统异常:" + e.getMessage());
    }
}

三、spring.xml配置

    <!-- 通知类 -->
    <bean class="cool.gjh.aspect.DrivingNotes" id="notes"/>

    <!-- 面向切面编程 -->
    <aop:config>
        <aop:pointcut id="pointcut1" expression="execution(* cool.gjh.beans.*.*(..))"/>
        <aop:advisor advice-ref="notes" pointcut-ref="pointcut1"/>
    </aop:config>

关于execution的说明:
execution(返回值 包.类.方法名(参数表))

四、测试

1. 不抛异常测试

测试类:

    @Autowired
    private Car car;

    /**
     * 测试依赖注入
     */
    @Test
    public void testInject(){
        car.drive();
    }

测试结果:
测试结果

2. 抛异常测试

本体类:

package cool.gjh.beans;

import org.springframework.stereotype.Component;

/**
 * 自行车
 *
 * @author ACE_GJH
 */
@Component
public class Bicycle implements Car{
    /**
     * 驾驶
     */
    @Override
    public void drive() {
        System.out.println("骑自行车开始兜风......");
        throw new RuntimeException("爆胎了!!!");
    }
}

测试类:

    @Autowired
    private Car car;

    /**
     * 测试依赖注入
     */
    @Test
    public void testInject(){
        car.drive();
    }

测试结果:
测试结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring AOPSpring框架中的一个重要模块,它提供了面向切面编程AOP)的支持。AOP是一种编程思想,它可以在不改变原有代码的情况下,通过在程序运行时动态地将代码“织入”到现有代码中,从而实现对原有代码的增强。 Spring AOP提供了基于注解的AOP实现,使得开发者可以通过注解的方式来定义切面、切点和通知等相关内容,从而简化了AOP的使用。 下面是一个基于注解的AOP实现的例子: 1. 定义切面类 ```java @Aspect @Component public class LogAspect { @Pointcut("@annotation(Log)") public void logPointcut() {} @Before("logPointcut()") public void beforeLog(JoinPoint joinPoint) { // 前置通知 System.out.println("执行方法:" + joinPoint.getSignature().getName()); } @AfterReturning("logPointcut()") public void afterLog(JoinPoint joinPoint) { // 后置通知 System.out.println("方法执行完成:" + joinPoint.getSignature().getName()); } @AfterThrowing(pointcut = "logPointcut()", throwing = "ex") public void afterThrowingLog(JoinPoint joinPoint, Exception ex) { // 异常通知 System.out.println("方法执行异常:" + joinPoint.getSignature().getName() + ",异常信息:" + ex.getMessage()); } } ``` 2. 定义业务逻辑类 ```java @Service public class UserService { @Log public void addUser(User user) { // 添加用户 System.out.println("添加用户:" + user.getName()); } @Log public void deleteUser(String userId) { // 删除用户 System.out.println("删除用户:" + userId); throw new RuntimeException("删除用户异常"); } } ``` 3. 在配置文件中开启AOP ```xml <aop:aspectj-autoproxy/> <context:component-scan base-package="com.example"/> ``` 在这个例子中,我们定义了一个切面类LogAspect,其中通过@Aspect注解定义了一个切面,通过@Pointcut注解定义了一个切点,通过@Before、@AfterReturning和@AfterThrowing注解分别定义了前置通知、后置通知和异常通知。 在业务逻辑类中,我们通过@Log注解标注了需要增强的方法。 最后,在配置文件中,我们通过<aop:aspectj-autoproxy/>开启了AOP功能,并通过<context:component-scan>扫描了指定包下的所有组件。 这样,当我们调用UserService中的方法时,就会触发LogAspect中定义的通知,从而实现对原有代码的增强。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

郭建華

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

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

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

打赏作者

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

抵扣说明:

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

余额充值