实现两个整数的加减乘除,并在每个计算前后打印出日志。

ArithmeticCalculator.java:

package spring.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); }

ArithmeticCalculatorImpl.java:

package spring.aop.impl;

import org.springframework.stereotype.Component;

@Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator { public int add(int i, int j) {
int result = i+j;
return result;
} public int sub(int i, int j) {
int result = i-j;
return result;
} public int mul(int i, int j) {
int result = i*j;
return result;
} public int div(int i, int j) {
int result = i/j;
return result;
} }

LoggingAspect.java:

package spring.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; //把这个类声明为一个切面:1.把该类放入到IOC容器中,2.再声明一个切面 @Aspect
@Component
public class LoggingAspect { //声明该方法是一个前置通知:在目标方法之前执行
@Before("execution( public int spring.aop.impl.*.*(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"+args);
} //后置通知:在目标方法执行后执行(无论是否发生异常),的通知。
//在后置通知中不能访问目标方法的执行结果
@After("execution( public int spring.aop.impl.*.*(int,int) )")
public void afterMethod(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" ends");
} }

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: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="spring.aop.impl" /> <!-- 使AspectJ注解起作用,自动为匹配的类生成代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

测试:

package spring.aop.impl.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.aop.impl.ArithmeticCalculator; public class Main {
public static void main(String[] args) { //1.创建spring的IOC容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从容器中获取bean的实例
ArithmeticCalculator arithmeticCalculator=(ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
//3.使用bean
int result = arithmeticCalculator.add(5, 8);
System.out.println("result:"+result); result = arithmeticCalculator.div(5, 8);
System.out.println("result:"+result);
}
}

输出:

The method add begins[5, 8]
The method add ends
result:13
The method div begins[5, 8]
The method div ends
result:0

最新文章

  1. zookeeper清除事物日志
  2. C语言新文法
  3. [_CN] Eclipse精要与高级开发技术 note
  4. php设计模式 适配器模式
  5. MySQL线上执行大事务或锁表操作
  6. bjfu1235 两圆公共面积
  7. MyBatis之四:调用存储过程含分页、输入输出参数
  8. UIButton关于setFont方法过时的解决方法
  9. 【转】querystring传递中文出现乱码的问题
  10. 关于看似简单的eclipse中tomcat小猫图标消失的问题解决
  11. JavaScripts学习日记——ECMAscript
  12. css 简单 返回顶部 代码及注释说明
  13. 关于Android开发中导出jar包后的资源使用问题解决
  14. Vue2.0+Node.js+MongoDB全栈打造商城系统 免费下载
  15. 2017-10-5模拟赛T2 小Z爱排序(sorting.*)
  16. ATM开学测试(未完成)
  17. axure rp ----专业的快速原型设计工具
  18. logback.xml配置示例
  19. (一)SpringMVC
  20. 使用socket BPF/Linux内核工程导论——网络:Filter(LSF、BPF、eBPF)

热门文章

  1. 邱老师玩游戏(树形DP) UESTC - 1136
  2. HUST 1354 - Rubiks (DP)
  3. MogoDB 分片键
  4. jPage.js分页
  5. h5 localStorage本地存储
  6. 一篇搞定spring Jpa操作数据库
  7. 我的Android进阶之旅------>Android横竖屏切换总结
  8. docker daemon.json 配置
  9. django的模板系统过滤器笔记
  10. More about Parameter Passing in Python(Mainly about list)