从头认识Spring-3.3 简单的AOP日志实现-增加检查订单功能
来源:互联网 发布:淘宝粉星便利店靠谱么 编辑:程序博客网 时间:2024/05/01 13:36
这一章节我们再上一个章节的基础上加上一个检查订单功能
1.domain
蛋糕类:
package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_3;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_3;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_3;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_3;import org.aspectj.lang.ProceedingJoinPoint;public class Log {public void washOven() {System.out.println("washOven,logging.....");}public void checkOrder(Cake cake) {System.out.println("is blueberryCheeseCake?" + cake.getName().equals("blueberryCheeseCake"));}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_3;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic 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();}}
2.测试类:
package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_3;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_3/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.xsd http://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.xsd http://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.*(..)) and args(cake)"id="chiefPointCut" /><aop:before method="checkOrder" pointcut-ref="chiefPointCut"arg-names="cake" /></aop:aspect></aop:config></beans>
在配置文件里面,我们需要注意的就是:
(1)expression表达式,我们不但定义了普通的表达式,而且还特意标注了args这个参数标签
(2)我们在执行方法记录的时候,我们把上面的cake标签当作参数名称注入到方法里面去
测试输出:
is blueberryCheeseCake?true
jack make blueberryCheeseCake
总结:这一章节主要介绍一个简单的AOP日志实现。
目录:http://blog.csdn.net/raylee2007/article/details/50611627
我的github:https://github.com/raylee2015/my_new_spring
0 0
- 从头认识Spring-3.3 简单的AOP日志实现-增加检查订单功能
- 从头认识Spring-3.7 简单的AOP日志实现(注解版)-增加检查订单功能
- 从头认识Spring-3.4 简单的AOP日志实现-扩展增加检查订单功能,以便记录并检测输入的参数
- 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展增加检查订单功能,以便记录并检测输入的参数
- 从头认识Spring-3.1 简单的AOP日志实现-某方法之前的前后记录日志
- 从头认识Spring-3.2 简单的AOP日志实现-需要记录方法的运行时间
- 从头认识Spring-3.2 简单的AOP日志实现-需要记录方法的运行时间
- 从头认识Spring-3.5 简单的AOP日志实现(注解版)-某方法之前的前后记录日志
- 从头认识Spring-3.6 简单的AOP日志实现(注解版)-需要记录方法的运行时间
- 关于Spring Aop,日志功能简单的实现
- 从头认识Spring-1.8 怎样利用AOP记录日志?
- spring aop实现日志功能
- 不使用spring的aop功能实现日志输出
- Spring AOP 实现写事件日志功能
- 简单认识 Spring AOP
- Spring Aop实现简单日志记录
- spring AOP的 操作日志记录功能
- 【Spring】aop的简单实现
- 【C语言】文件定位读取
- oracle rac理解和用途扩展(转)
- neuq oj 1052: 谭浩强C语言(第三版)习题6.8 C语言
- 解密回声消除技术之二(应用篇)
- poj3693Maximum repetition substring【后缀数组+RMQ求重复最多连续子串】
- 从头认识Spring-3.3 简单的AOP日志实现-增加检查订单功能
- TCP/IP、Http、Socket的区别
- PAT--1013 Battle Over Cities(并查集)
- 利用python求解八数码难题
- 《MVC + EF+ WCF 》——批量删除数据
- HDU1075 What Are You Talking About
- Windows基础-使用XAudio2播放音频(本质是WASAPI)
- 基于百度API的开源自动翻译.srt文件软件的实现的几个技术细节总结附源代码
- elasticsearch zen discovery