这一章节我们再上一个章节的基础上加上一个检查订单功能

1.domain

蛋糕类:

package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_4;public class Cake {private String name = "";public String getName() {return name;}public void setName(String name) {this.name = name;}}

烤炉类:

package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_4;public class Oven {private String name = "";@Overridepublic String toString() {return name;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

厨师类:

package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_4;public class Chief {private static int index = 0;public static int getIndex() {return index;}public static void setIndex(int index) {Chief.index = index;}private Cake cake = null;private final int id = index++;private String name = "";private Oven oven = null;public Cake getCake() {return cake;}public int getId() {return id;}public String getName() {return name;}public Oven getOven() {return oven;}public void setCake(Cake cake) {this.cake = cake;}public void setName(String name) {this.name = name;}public void setOven(Oven oven) {this.oven = oven;}public void makeOneCake(Cake cake) {System.out.println(getName() + " make " + cake.getName());}}

日志类:

package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_4;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;public class Log {public void washOven() {System.out.println("washOven,logging.....");}public void checkOrder(JoinPoint joinpoint) {for (Object item : joinpoint.getArgs()) {if (item instanceof Cake) {Cake cake = (Cake) item;System.out.println(cake.getName());}}}public void prepare() {System.out.println("prepare,logging.....");}public void after() {System.out.println("after someting to do,logging.....");}public void around(ProceedingJoinPoint joinPoint) throws Throwable {washOven();prepare();long startTime = System.currentTimeMillis();joinPoint.proceed();long endTime = System.currentTimeMillis();System.out.println("use time:" + (endTime - startTime));after();}}

上面扩展了一个检測订单的功能。以便记录并检測输入的參数。

配置类:(我们这里使用基于java的配置)

package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_4;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SpringBeans {@Beanpublic Chief jack() {Chief chief = new Chief();chief.setName("jack");chief.setOven(oven());chief.setCake(cake());return chief;}@Beanpublic Oven oven() {Oven oven = new Oven();oven.setName("big oven");return oven;}@Beanpublic Cake cake() {Cake cake = new Cake();cake.setName("blueberryCheeseCake");return cake;}@Beanpublic Log log() {return new Log();}}

在日志类这里我们须要注意的是:

我们上面引入了joinpoint这个接口。然后从接口里面得到想要的參数,再通过转型从而得到对应的输入參数。

2.測试类:

package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_4;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/com/raylee/my_new_spring/my_new_spring/ch03/topic_1_4/ApplicationContext-test.xml" })
public class ChiefTest {@Autowiredprivate ApplicationContext applicationContext;@Testpublic void testChief() {Chief jack = (Chief) applicationContext.getBean(Chief.class);Cake cake = applicationContext.getBean(Cake.class);cake.setName("blueberryCheeseCake");jack.makeOneCake(cake);}
}

3.配置文件(重点)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><context:component-scanbase-package="com.raylee.my_new_spring.my_new_spring.ch03.topic_1_3" /><aop:config><aop:aspect ref="log"><aop:pointcutexpression="execution(* com.raylee.my_new_spring.my_new_spring.ch03.topic_1_3.Chief.*(..))"id="chiefPointCut" /><aop:before method="checkOrder" pointcut-ref="chiefPointCut"/></aop:aspect></aop:config></beans>

配置文件这里。我们不须要特定的cake參数,直接就是把类放到expression里面就可以。

測试输出:

blueberryCheeseCake
jack make blueberryCheeseCake

总结:这一章节主要介绍一个简单的AOP日志实现,扩展添加检查订单功能。以便记录并检測输入的參数。

文件夹:http://blog.csdn.net/raylee2007/article/details/50611627

我的github:https://github.com/raylee2015/my_new_spring

转载于:https://www.cnblogs.com/llguanli/p/8444613.html

从头认识Spring-3.4 简单的AOP日志实现-扩展添加检查订单功能,以便记录并检測输入的參数...相关推荐

  1. Spring中的简单实现AOP小例子

    先说一下相应的知识作铺垫 Spring的AOP术语:          1)连接点(JointPoint):目标对象的每个方法     2)切入点(PontCut):切入了服务代码的连接点     3 ...

  2. (转)使用Spring的注解方式实现AOP的细节

    http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...

  3. 【Spring】面向切面编程AOP

    AOP基础 什么是AOP [废话解释]在软件业,AOP全称Aspect Oriented Programming 即:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AO ...

  4. Spring (二) OOP V.S AOP

    介绍 这是两种不同的编程思想就好比初中数学中学习的横纵坐标,一种是横向的一种是纵向,OOP是代表X轴而AOP代表Y轴,如下图: 数学几乎可以解释生活中所有的现象,无论是物体运动还是静止,也可以通过数学 ...

  5. JAVA基础加强(张孝祥)_类加载器、分析代理类的作用与原理及AOP概念、分析JVM动态生成的类、实现类似Spring的可配置的AOP框架...

    1.类加载器 ·简要介绍什么是类加载器,和类加载器的作用 ·Java虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个类负责加载特定位置的类:BootStrap,ExtClassLoader ...

  6. Spring 原理初探——IoC、AOP

    前言 众所周知, 现在的 Spring 框架已经成为构建企业级 Java 应用事实上的标准了,众多的企业项目都构建在 Spring 项目及其子项目之上,特别是 Java Web 项目. Spring ...

  7. 图文结合分析Spring的面向切面编程--AOP

    Spring还可以这么学–AOP 上一篇文章Spring还可以这么学–IoC(控制反转) / DI(依赖注入)理解 1. 什么是AOP? AOP(Aspect Oriented Programming ...

  8. Spring笔记——使用Spring进行面向切面(AOP)编程

    要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间: =================== Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种: 1. 基于XM ...

  9. Spring 源码分析(三) —— AOP(二)Spring AOP 整体架构

    2019独角兽企业重金招聘Python工程师标准>>> Spring AOP 架构         先是生成代理对象,然后是拦截器的作用,最后是编织的具体实现.这是AOP实现的三个步 ...

最新文章

  1. Django入门之开发环境搭建1.1
  2. jquery清空div内容_jQuery常用功能
  3. JVM之垃圾收集器回收种类
  4. 电影与爆米花(模拟)
  5. 神结合!一招玩转K8s和微服务治理
  6. 数据结构-哈希与映射
  7. opencv java水平投影_使用OpenCv中Mat进行水平投影与垂直投影并实现字符切分
  8. aws终止实例后还收费吗_「技术选型」AWS 和 AZURE的全面比较
  9. [转]详细解说:简单CSS3实现炫酷读者墙
  10. Spring MVC 常用注解
  11. G415,到了徐州,车厢空了
  12. git grep 全字匹配_git log --grep 筛选(转)
  13. AssertionError: Override list has odd length: [‘\r‘]; it must be a list of pairs
  14. SEGGER System View - J-Link Library not found
  15. 惠普m180n故障码04_惠普m180n打印机驱动(解决m180n打印机连接问题)V1.0 免费版
  16. python RTL自动生成_实例16:用Python自动生成Excel档每日领料单
  17. 不一样的xss payload
  18. 蓝牙鼠标windows linux,解决Ubuntu 18.04与Windows 10双系统蓝牙鼠标连接的问题
  19. html5源码笔记(三)【爱创课堂专业前端培训】
  20. vrchat模型保存_轻松简单自己上传VRChat的Avatar

热门文章

  1. RedisTemplate存数据时指定过期时间
  2. Android开发笔记(一百一十二)开发工具
  3. Android之控件与布局,结构知识点,基础完结
  4. 12、testng.xml指定运行测试包、测试类、测试方法
  5. 第8周课堂测试3(课上未完成)
  6. 《指针的编程艺术(第二版)》一3.8 改错题
  7. php图片处理之本地图片转base64格式上传
  8. Fix Elementary Boot Screen (plymouth) After Installing Nvidia Drivers
  9. docker nginx部署web应用_实战docker,编写Dockerfile定制tomcat镜像,实现web应用在线部署...
  10. Linux下用户组、文件权限、更改目录下所有文件权限