Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同。

相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述。

案例要求:

写一个简单的实现四则运算的计算器。

加入AOP功能:日志功能;检测参数中是否有负数的功能。

废话不多说了,直接上代码:

(一)基于XML配置:

定义了一个接口类:

package com.edu.aop;

public interface ArithmeticCalculator {

    int add(int i,int j);
    int sub(int i,int j);
    int mul(int i,int j);
    int div(int i,int j);
}

实现类:

package com.edu.aop;

public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

    @Override
    public int add(int i, int j) {
        return i+j;
    }

    @Override
    public int sub(int i, int j) {
        return i-j;
    }

    @Override
    public int mul(int i, int j) {
        return i*j;
    }

    @Override
    public int div(int i, int j) {
        return i/j;
    }

}

日志切片类:

package com.edu.aop;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;

public class LoggingAspect {
/**
 * 日志切面类
 */

    public void beforeMethod(JoinPoint joinPoint){
        //获取方法名
        String methodName=joinPoint.getSignature().getName();
        //获取方法实参值列表
        Object[] args=joinPoint.getArgs();
        System.out.println("The method "+methodName+" begin with "+Arrays.asList(args));
    }

    public void afterMethod(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        System.out.println("The method "+methodName+" ends");
    }
}

检测参数中是否有负数的切片类:

package com.edu.aop;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;

public class ValidationAspect {

    public void validationArgs(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        Object[] args=joinPoint.getArgs();
        System.out.println("待验证参数:"+Arrays.asList(args));
        if(args!=null&&args.length>0){
            for(int i=0;i<args.length;++i){
                if(((Integer)args[i]).intValue()<0){
                    System.out.println("警告:方法"+methodName+"()第"+(i+1)+"个参数为负数:"+args[i]);
                }
            }
        }
    }
}

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

<!-- 配置bean -->
<bean id="arithmetic" class="com.edu.aop.ArithmeticCalculatorImpl"></bean>

<!-- 分别将切面类声明配置成一个bean -->
<bean id="logging" class="com.edu.aop.LoggingAspect"></bean>
<bean id="validation" class="com.edu.aop.ValidationAspect"></bean>

<aop:config>
<!-- 配置日志切片类 -->
<aop:aspect ref="logging">
<!-- 配置前置通知 及前置通知的切入点-->
<aop:before method="beforeMethod" pointcut="execution(* com.edu.aop.ArithmeticCalculatorImpl.*(..))"></aop:before>
<!-- 配置后置通知及后置通知的切入点 -->
<aop:after method="afterMethod" pointcut="execution(* com.edu.aop.ArithmeticCalculatorImpl.*(..))"></aop:after>
</aop:aspect>

<!-- 配置检测参数切片类 -->
<aop:aspect ref="validation">
<!-- 配置前置通知 及前置通知的切入点-->
<aop:before method="validationArgs" pointcut="execution(* com.edu.aop.ArithmeticCalculatorImpl.*(..))"></aop:before>
</aop:aspect>
</aop:config>
</beans>

主方法检测类:

package com.edu.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {

        ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml");
        ArithmeticCalculator arithmetic=(ArithmeticCalculator)act.getBean("arithmetic");
        int result=arithmetic.add(10, 20);
        System.out.println("result:"+result);
        result=arithmetic.div(-36, 4);
        System.out.println("result:"+result);
    }

}

运行结果:

(二)基于AspectJ注解配置的AOP编程:

未完待续。

原文地址:https://www.cnblogs.com/dudududu/p/8487750.html

时间: 2024-07-31 14:31:30

Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较的相关文章

spring基于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:aop="http://www.springframework.org/schema/

Spring学习之旅(四)Spring工作原理再探

上篇博文对Spring的工作原理做了个大概的介绍,想看的同学请出门左转.今天详细说几点. (一)Spring IoC容器及其实例化与使用 Spring IoC容器负责Bean的实例化.配置和组装工作有两个接口:BeanFactory和ApplicationContext.其中ApplicationContext继承于BeanFactory,对企业级应用开发提供了更多的支持.在实际应用中都是用该接口. 1)实例化Spring容器(主要有四种) 1.ClassPathXmlApplicationCo

Spring学习(5)---Bean的定义及作用域的注解实现

Bean管理的注解实现 Classpath扫描与组件管理 类的自动检测与注册Bean <context:annotation-config/> @Component,@Repository,@Service,@Controller @Required @Autowired @Qualifier @Resource (一) Classpath扫描与组件管理 从Spring3.0开始,Spring javaConfig项目提供了很多特性,包括使用java而不是xml定义bean,比如:@Confi

Spring Aop实例之AspectJ注解配置

http://blog.csdn.net/xiaoxian8023/article/details/17285809 上篇博文<Spring Aop实例之xml配置>中,讲解了xml配置方式,今天来说说AspectJ注解方式去配置spring aop. 依旧采用的jdk代理,接口和实现类代码请参考上篇博文.主要是将Aspect类分享一下: [java] view plaincopy package com.tgb.aop; import org.aspectj.lang.JoinPoint;

Spring核心学习(4)从XML中读取BeanDefinition-将代码变成配置

前导:开始学习Spring核心思想,通过一个山寨精简版Spring代码结合学习. 内容:1. BeanDefinitionReader-配置读取者. 2. XmlBeanDefinitionReader-从XML中读取配置. 3. Resource-定位资源文件.这次将Bean的配置信息都放到了XML里,所以这里会有一个XML文件的读取,我们通过XmlBeanDefinitionReader将XML中的信息组装好成BeanDefinition放到一个Map里,这样我们就能真正做到像Spring那

Spring 学习笔记(七)—— 切入点表达式

为了能够灵活定义切入点位置,Spring AOP提供了多种切入点指示符. execution———用来匹配执行方法的连接点 语法结构:   execution(   方法修饰符  方法返回值  方法所属类 匹配方法名 (  方法中的形参表 )  方法申明抛出的异常  ) 其中红色字体的部分时不能省略的,各部分都支持通配符 “*” 来匹配全部. 比较特殊的为形参表部分,其支持两种通配符 "*":代表一个任意类型的参数: “..”:代表零个或多个任意类型的参数. 例如: ()匹配一个无参方

Java框架spring 学习笔记(二十):事务管理(注解管理)

注解管理的方式要比xml配置方式要简单很多 只需在配置文件中添加事务注解 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:tx="http://www.springframework.org/schema/tx" 4 xmlns:aop=&

框架 day36 Spring3 入门,DI依赖注入,装配bean基于xml/注解, 整合Junit4,配置约束自动提示

1 什么是spring 1.1官网 spring.io 1.2介绍 Spring的核心是控制反转(IoC)和面向切面(AOP). IoC(Inverse of Control 反转控制) AOP(Aspect Oriented Programming 面向切面编程为内核) 简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架. *轻量级:依赖其他内容较小,使用资源消耗也少.对比:EJB 重量级 *分层:经典三层体系架构,spring 提供解决方案

spring基于xml的声明式事务控制

配置文件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:aop="http://www.springframework