1.加入jar包

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

commons-logging-1.1.3.jar

spring-aop-4.1.0.RELEASE.jar

spring-aspects-4.1.0.RELEASE.jar

spring-beans-4.1.0.RELEASE.jar

spring-context-4.1.0.RELEASE.jar

spring-core-4.1.0.RELEASE.jar

spring-expression-4.1.0.RELEASE.jar

2.在配置文件中加入AOP的命名空间

3.基于注解的方式

①在配置文件中加入如下配置

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

②把横切关注点的代码抽象到切面的类中

切面首先是一个IOC中的bean,即加入@Component注解

切面还需要加入@Aspect注解

③在类中声明各种通知

@Before 前置通知,在方法执行之前执行

@After 后置通知,在方法执行之后执行

@AfterRunning 返回通知,在方法返回结果之后执行

@AfterThrowing 异常通知,在方法抛出异常之后执行

@Around 环绕通知,围绕着方法执行

③在方法中声明一个类型为JoinPoint的参数就可以访问链接细节

ArithmeticCalculator接口

package com.spring.aop.impl;

public interface ArithmeticCalculator {
public int add(int i, int j); public int sub(int i, int j); public int mul(int i, int j); public int div(int i, int j);
}

  接口实现类 ArithmeticCalculatorImpl.java

package com.spring.aop.impl;

import org.springframework.stereotype.Component;

@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator{ @Override
public int add(int i, int j) {
int result = i + j;
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;
} }

  切面类 LoggingAspect.java

package com.spring.aop.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; //把这个类声明为一个切面:需要把该类放入到IOC容器中。再声明为一个切面.
@Aspect
@Component
public class LoggingAspect { /**
*前置通知,在目标方法开始之前执行。
*@Before("execution(public int com.spring.aop.impl.ArithmeticCalculator.add(int, int))")这样写可以指定特定的方法。
* @param joinpoint
*/
@Before("execution(public int com.spring.aop.impl.ArithmeticCalculator.*(int, int))")
public void beforeMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
List<Object>args = Arrays.asList(joinpoint.getArgs());
System.out.println("前置通知:The method "+ methodName +" begins with " + args);
} /**
*后置通知,在目标方法执行之后开始执行,无论目标方法是否抛出异常。
*在后置通知中不能访问目标方法执行的结果。
* @param joinpoint
*/
@After("execution(public int com.spring.aop.impl.ArithmeticCalculator.*(int, int))")
public void afterMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
//List<Object>args = Arrays.asList(joinpoint.getArgs()); 后置通知方法中可以获取到参数
System.out.println("后置通知:The method "+ methodName +" ends ");
} /**
*返回通知,在方法正常结束之后执行。
*可以访问到方法的返回值。
* @param joinpoint
* @param result 目标方法的返回值
*/
@AfterReturning(value="execution(public int com.spring.aop.impl.ArithmeticCalculator.*(..))", returning="result")
public void afterReturnning(JoinPoint joinpoint, Object result) {
String methodName = joinpoint.getSignature().getName();
System.out.println("返回通知:The method "+ methodName +" ends with " + result);
} /**
*异常通知。目标方法出现异常的时候执行,可以访问到异常对象,可以指定在出现特定异常时才执行。
*假如把参数写成NullPointerException则只在出现空指针异常的时候执行。
* @param joinpoint
* @param e
*/
@AfterThrowing(value="execution(public int com.spring.aop.impl.ArithmeticCalculator.*(..))", throwing="e")
public void afterThrowing(JoinPoint joinpoint, Exception e) {
String methodName = joinpoint.getSignature().getName();
System.out.println("返回通知:The method "+ methodName +" occurs exception " + e);
} /**
* 环绕通知类似于动态代理的全过程,ProceedingJoinPoint类型的参数可以决定是否执行目标方法。
* @param point 环绕通知需要携带ProceedingJoinPoint类型的参数。
* @return 目标方法的返回值。必须有返回值。
*/
/*不常用
@Around("execution(public int com.spring.aop.impl.ArithmeticCalculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint point) {
Object result = null;
String methodName = point.getSignature().getName();
try {
//前置通知
System.out.println("The method "+ methodName +" begins with " + Arrays.asList(point.getArgs()));
//执行目标方法
result = point.proceed();
//翻译通知
System.out.println("The method "+ methodName +" ends with " + result);
} catch (Throwable e) {
//异常通知
System.out.println("The method "+ methodName +" occurs exception " + e);
throw new RuntimeException(e);
}
//后置通知
System.out.println("The method "+ methodName +" ends");
return result;
}
*/
}

  applicationContext.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"
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.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 配置自动扫描包 -->
<context:component-scan base-package="com.spring.aop.impl"></context:component-scan>
<!-- 使AspectJ注解起作用:自动为匹配的类生产代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

  Main.java

package com.spring.aop.impl;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
//创建spring IOC容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//从IOC容器中获取bean实例
ArithmeticCalculator arithmeticCalculator = applicationContext.getBean(ArithmeticCalculator.class);
int result = arithmeticCalculator.add(4, 6);
System.out.println(result);
result = arithmeticCalculator.sub(4, 6);
System.out.println(result);
System.out.println(result);
result = arithmeticCalculator.mul(4, 6);
System.out.println(result);
System.out.println(result);
result = arithmeticCalculator.div(4, 0);
System.out.println(result);
}
}

  

源码

http://yunpan.cn/cgsy2s5Qf8KAY  提取码 e202

Spring4学习笔记-AOP的更多相关文章

  1. Spring4学习笔记-AOP(基于配置文件的方式)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://shamrock.blog.51cto.com/2079212/1557743 引 ...

  2. Spring4学习笔记1-HelloWorld与IOC和DI概述

    1.安装环境 1.1安装eclipse,jdk 1.1安装Spring tool suite(非必要项) 2.spring HelloWorld 2.1 需要的jar包(spring官网下载:http ...

  3. 学习笔记: AOP面向切面编程和C#多种实现

    AOP:面向切面编程   编程思想 OOP:一切皆对象,对象交互组成功能,功能叠加组成模块,模块叠加组成系统      类--砖头     系统--房子      类--细胞     系统--人    ...

  4. spring4学习笔记

    IOC public class IServiceImpl implements IService { public void IserviceImpl(){} @Override public vo ...

  5. spring学习笔记-AOP

    1.aop:aspect oriented programming 面向切面编程 2.aop在spring中的作用:   提供声明式服务(声明式事务) 允许用户实现自定义切面 3.aop:在不改变原有 ...

  6. Spring的入门学习笔记 (AOP概念及操作+AspectJ)

    AOP概念 1.aop:面向切面(方面)编程,扩展功能不通过源代码实现 2.采用横向抽取机制,取代了传统的纵向继承重复代码 AOP原理 假设现有 public class User{ //添加用户方法 ...

  7. 学习笔记——AOP

    以下纯属个人刚了解点皮毛,一知半解情况下的心得体会: ==================================================================== AOP( ...

  8. Spring4学习笔记 - Bean的生命周期

    1 Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1)通过构造器或工厂方法创建 Bean 实例 2)为 Bean 的属性设置值和对其他 Bean 的引用 3)调用 Bean 的初 ...

  9. Spring4学习笔记 - SpEL表达式

随机推荐

  1. iOS 蓝牙开发(四)BabyBluetooth蓝牙库介绍(转)

    转载自:http://www.cocoachina.com/ios/20151106/14072.html 原文作者:刘彦玮 BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBlue ...

  2. BZOJ 2080: [Poi2010]Railway 双栈排序

    2080: [Poi2010]Railway Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 140  Solved: 35[Submit][Statu ...

  3. js-JavaScript高级程序设计学习笔记2

    第四章 变量.作用域和内存问题 1.ES变量包含两种不同数据类型的值--基本类型值(5种基本数据类型)和引用类型值(保存在内存中的对象,所有引用类型值都是Object的实例) 2.只能给引用类型值动态 ...

  4. 【BZOJ-2115】Xor 线性基 + DFS

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2142  Solved: 893[Submit][Status] ...

  5. CMD.EXE中dir超长字符串缓冲区溢出原理学习

    最近看逍遥的<网络渗透攻击与安防修炼>讲到CMD命令窗口的dir传超长字符串溢出的例子.自己实验了一下,的确会产生程序崩溃,但是具体什么原理没太详细说,这里做一下原理探究,权当学习笔记了. ...

  6. [Objective-C 面试简要笔记]

    Obj-C: 1.消息机制 [shape draw]  向该对象发送消息,该对象查找并运行此函数 差不多就是shape.draw(); 2.中缀语法 [textThing setStringValue ...

  7. [IOS 实现TabBar在Push后的隐藏 以及 两级Tabbar的切换]

    翻了好多网页都没找到资料,自己试了下终于成功了,遂分享一下. 1.实现TabBar在Push后的隐藏 假如结构是这样 NavController->A->B,我们想要实现在A里有Tabba ...

  8. pycharm 启动后一直更新index的问题

    这个谷歌一下就知道了,stackoveflow上就有几个解决方案,试试哪个好使就可以了. 详情见http://stackoverflow.com/questions/29030682/pycharm- ...

  9. 用DOS命令配置服务开机自启动

    2016-08-19 15:01 Create 使用命令  sc  config 参考博客:http://blog.csdn.net/it1988888/article/details/7992626 ...

  10. hdu 2014 青年歌手大奖赛_评委会打分

    题意: 输入N个数,去掉最大和最小的数,求剩余的数的平均数. 解法: 找到分别最大和最小的数,然后从总和中减去他们,再求平均数(不要排序): 1: #include<stdlib.h> 2 ...