Spring的AOP-----HelloWord

这里就一个计算器开发为例
1搭建环境-搭配好Spring的AOP开发环境
导入以下这些包:

2建立好核心处理模块的类
ArithmeticCalculator:

package com.jeremy.spring.AspectJ;

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);
}

ArithmeticCalculatorImp

package com.jeremy.spring.AspectJ;

import org.springframework.stereotype.Component;

@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

    @Override
    public int add(int i, int j) {
        int result =i+j;
        System.out.println(this.getClass()+"     "+this.hashCode());
        return result;
    }

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

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

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

}

3建立切面类,并在里面配置切面信息

package com.jeremy.spring.AspectJ;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * AOP 的 helloWorld
 * 1. 加入 jar 包
 * com.springsource.net.sf.cglib-2.2.0.jar
 * com.springsource.org.aopalliance-1.0.0.jar
 * com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
 * spring-aspects-4.0.0.RELEASE.jar
 *
 * 2. 在 Spring 的配置文件中加入 aop 的命名空间。
 *
 * 3. 基于注解的方式来使用 AOP
 * 3.1 在配置文件中配置自动扫描的包: <context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan>
 * 3.2 加入使 AspjectJ 注解起作用的配置: <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
 * 为匹配的类自动生成动态代理对象.
 *
 * 4. 编写切面类:
 * 4.1 一个一般的 Java 类
 * 4.2 在其中添加要额外实现的功能.
 *
 * 5. 配置切面
 * 5.1 切面必须是 IOC 中的 bean: 实际添加了 @Component 注解
 * 5.2 声明是一个切面: 添加 @Aspect
 * 5.3 声明通知: 即额外加入功能对应的方法.
 * 5.3.1 前置通知: @Before("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(int, int))")
 * @Before 表示在目标方法执行之前执行 @Before 标记的方法的方法体.
 * @Before 里面的是切入点表达式:
 *
 * 6. 在通知中访问连接细节: 可以在通知方法中添加 JoinPoint 类型的参数, 从中可以访问到方法的签名和方法的参数.
 *
 * 7. @After 表示后置通知: 在方法执行之后执行的代码.
 */

//通过添加 @Aspect 注解声明一个 bean 是一个切面!
@Aspect
@Component
public class LoggingAspect {

    @Before("execution(public int com.jeremy.spring.AspectJ.ArithmeticCalculator.*(int, int))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object [] args = joinPoint.getArgs();

        System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
    }

    @After("execution(* com.jeremy.spring.AspectJ.*.*(..))")
    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " ends");
    }

}

4配置SPring的xml文件

3基于注解的方式来使用 AOP
 3.1 在配置文件中配置自动扫描的包: <context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan>
 3.2 加入使 AspjectJ 注解起作用的配置: <aop:aspectj-autoproxy></aop:aspectj-autoproxy>为匹配的类自动生成动态代理对象.

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-- 自动扫描的包 -->
    <context:component-scan base-package="com.jeremy.spring.AspectJ"></context:component-scan>
    <!-- 使 AspectJ 的注解起作用 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>
时间: 2024-08-30 06:10:30

Spring的AOP-----HelloWord的相关文章

8 -- 深入使用Spring -- 4... Spring的AOP

8.4 Spring的AOP AOP(Aspect Orient Programming),也就是面向切面编程,最为面向对象编程的一种补充. AOP和OOP互为补充,面向对象编程将程序分解成各个层次的对象,而面向切面编程将程序运行过程分解成各个切面.可以这样理解:面向对象编程是从静态角度考虑程序结构,而面向切面编程则是从动态角度考虑程序运行过程. 8.4.1 为什么需要AOP 8.4.2 使用AspectJ实现AOP 1.下载和安装AspectJ 2.AspectJ使用入门 8.4.3 AOP的

Spring 之 AOP

面向方面的编程,即 AOP,是一种编程技术,它允许程序员对横切关注点或横切典型的职责分界线的行为(例如日志和事务管理)进行模块化.AOP 的核心构造是方面, 它将那些影响多个类的行为封装到可重用的模块中. 通常情况下,对于AOP,我们有两种方式来实现. 使用DynamicProxy实现AOP 下面是一个简单的示例,首先定义业务对象: 1 public interface UserDao { 2 3 void save(); 4 } 5 6 public class UserDaoImpl imp

Spring实现AOP的4种方式(转)

转自:http://blog.csdn.net/udbnny/article/details/5870076 Spring实现AOP的4种方式 先了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个“时机”,这些“时机”就是连接点,例如方法被调用时.异常被抛出时等等.3.切入点(Pointcut)通知定义了切面要发生的“故事”和时间,那么切入点就定义了“故事”发生的

Java反射—模拟Spring的Aop

1.    大概流程 上篇文章已经结合Java反射解释了SpringAop的原理,这里我们简单模拟以下Spring的Aop实现.大体流程如下: ?  创建一个properties配置文件模拟Spring配置文件. ?  创建一个增强接口与一个实现类模拟Spring的Advice. ?  创建一个生成代理的工厂类,并在InvocationHandler类的invoke方法中织入增强方法(即aop). ?  创建一个生成Bean的工厂类(类似IOC工厂,只创建bean,没有依赖注入的功能),生成Be

Spring实现AOP的4种方式

来自:http://blog.csdn.net/udbnny/article/details/5870076 先了解AOP的相关术语: 1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个“时机”,这些“时机”就是连接点,例如方法被调用时.异常被抛出时等等.3.切入点(Pointcut)通知定义了切面要发生的“故事”和时间,那么切入点就定义了“故事”发生的地点,例如某个类或方法的名称,S

spring的AOP个人理解和使用

1什么是AOP:AOP是面向切面编程,也就是说面向某个功能模块编程,典型的应用就是Spring的声明式事务, Spring的AOP事务解析: 在以前的事务管理是要融合在逻辑代码中的,在逻辑代码中决定事务是否提交或者回滚,这样很容易造成代码难以维护,代码冗余 但是使用spring的声明式事务后,只需要在数据库处理方法上注解事务,就可以对操作进行管理,事务的设置和逻辑代码分开,容易维护2AOP有什么作用 :面向切面编程,例如某个功能点,我们只需抽取横切关注点,然后让需要处理这些功能点的方法来使用代理

第二十八天 月出惊山鸟 —Spring的AOP

6月13日,阴转细雨."人闲桂花落,夜静春山空.月出惊山鸟,时鸣春涧中." 不管在面向过程还是在面向对象里,神奇的"纯"字,似乎永远都充满了无限的可能性.除了函数之所调用.类之所封装,在程序员文化里,对于"纯粹"的感知和定义,既起自于代码,又超越了代码.也就是说,能够真真切切地感觉到纯净的,不仅是我们的每一个Bean和每一个Class,还包括每个Coder的心. 然而,客户的需求是千变万化和千奇百怪的,Spring在为Coder在应对和处理各自不

Spring框架第五篇之Spring与AOP

一.AOP概述 AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运行过程. AOP底层就是采用动态代理模式实现的,采用了两种代理:JDK的动态代理与CGLIB的动态代理. 面向切面编程,就是将交叉业务逻辑封装成切面,利用AOP容器的功能将切面织入到主业务逻辑中.所谓交叉业务逻辑是指,通用的.与主业务逻辑无关的代码.如安全检查.事务.日志等. 若不是用AOP,则会出

Spring的AOP详解

Spring的AOP详解 一.AOP基础 1.1AOP是什么 考虑这样一个问题:需要对系统中的某些业务做日志记录,比如支付系统中的支付业务需要记录支付相关日志,对于支付系统可能相当复杂,比如可能有自己的支付系统,也可能引入第三方支付平台,面对这样的支付系统该如何解决呢? 传统解决方案 1.日志部分定义公共类LogUtils,定义logPayBegin方法用于记录支付开始日志, logPayEnd用于记录支付结果 logPayBegin(long userId,long money) logPay

spring之aop概念和配置

面向切面的一些概念: 简单说: 连接点就一些方法,在这些方法基础上需要额外的一些业务需求处理. 切入点就是方法所代表的功能点组合起来的功能需求. 通知就是那些额外的操作. 织入就是使用代理实现整个切入的过程. 引入就是已有功能代码不变的基础上,添加新属性和方法. spring使用aop首先xml添加命名空间实例; 并且要在xml 配置中添加<aop:aspectj-autoproxy/>标签,当然对象交给spring管理也要配置bean 环绕通知可以替换上面通知效果: 最终通知在例外通知前执行