后置通知

看代码:

package logan.study.aop.impl;

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 logan.study.aop.impl;

import org.springframework.stereotype.Component;

@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override
public int add(int i, int j) {
// TODO Auto-generated method stub
int result = i + j;
return result;
} @Override
public int sub(int i, int j) {
// TODO Auto-generated method stub
int result = i - j;
return result;
} @Override
public int mul(int i, int j) {
// TODO Auto-generated method stub
int result = i * j;
return result;
} @Override
public int div(int i, int j) {
// TODO Auto-generated method stub
int result = i / j;
return result;
} }
package logan.study.aop.impl;

import java.util.Arrays;
import java.util.List; 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; //把这个类声明为一个切面:需要把该类放入到IOC容器中,在声明为一个切面
@Aspect
@Component
public class LoggingAspect {
//声明该方法时一个前置通知:在目标方法开始之前执行
@Before("execution(public int logan.study.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);
} //后置通知,在目标方法执行之后执行(无论该方法是否出现异常)
//在后置通知中,还不能访问目标方法执行的结果,执行结果在返回通知中可以访问
@After("execution(public int logan.study.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 with "+args);
} }
<?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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <context:component-scan base-package="logan.study.aop.impl"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
package logan.study.aop.impl;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub
//1.创建Spring的IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器里面获取bean实例
ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
//使用Bean
int result = arithmeticCalculator.add(3, 6);
System.out.println(result); result = arithmeticCalculator.div(3, 6);
System.out.println(result); } }

输出结果:

五月 27, 2017 5:46:24 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Sat May 27 17:46:24 CST 2017]; root of context hierarchy
五月 27, 2017 5:46:24 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
The method add begins with [3, 6]
The method add ends with [3, 6]
9
The method div begins with [3, 6]
The method div ends with [3, 6]
0

最新文章

  1. (转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
  2. REST API (from IBM)
  3. windows下安装PhpDocumentor(phpdoc)笔记
  4. chrome 下载插件包及离线安装
  5. javascript 把字符串转换为对象
  6. sql语句延时执行或者是指定时间执行
  7. HW4.27
  8. UML进行Linux内核调试
  9. C#中委托和事件
  10. [置顶] 阅读Oracle官方文档指南
  11. $MarkDown$ 中使用$ \LaTeX$ 数学式
  12. .Net Core 部署在win10 的IIS上注意问题。
  13. Java并发之ReentrantLock
  14. 分享一个好用的tmux配置文件
  15. vue条件与循环
  16. php 数据库备份还原
  17. mysql数据库主从同步读写分离(一)主从同步
  18. Oracle 关于WKT构造SDO_GEOMETRY的问题。
  19. BZOJ 3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机 后缀自动机 字符串
  20. 行人检测4(LBP特征)

热门文章

  1. 利用python进行数据分析之pandas入门
  2. 安装anaconda及pytorch
  3. 九度OJ 1051:数字阶梯求和 (大数运算)
  4. Versions 崩溃(Mac升级OS X Yonsemite 10.10)
  5. 【windows】更改最大动态端口数
  6. JETSON TK1 ~ 控制GPIO
  7. elasticsearch 简单聚合查询示例
  8. [egret+pomelo]实时对战游戏杂记(5)
  9. Linux桥接网络配置
  10. 算法(Algorithms)第4版 练习 1.3.27 1.3.28