从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展添加检查订单功能,以便记录并检測输入的參数
这一章节我们讨论一下扩展添加检查订单功能,以便记录并检測输入的參数。
1.domain
蛋糕类:
package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_8;
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_8;
public class Oven {
private String name = "";
@Override
public 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_8;
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_8; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class Log { @Pointcut(value = "execution(* com.raylee.my_new_spring.my_new_spring.ch03.topic_1_8.Chief.*(..))")
public void chiefPointCut() {
} public void washOven() {
System.out.println("washOven,logging.....");
} @Before("chiefPointCut()")
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 {
@Bean
public Chief jack() {
Chief chief = new Chief();
chief.setName("jack");
chief.setOven(oven());
chief.setCake(cake());
return chief;
} @Bean
public Oven oven() {
Oven oven = new Oven();
oven.setName("big oven");
return oven;
} @Bean
public Cake cake() {
Cake cake = new Cake();
cake.setName("blueberryCheeseCake");
return cake;
} @Bean
public Log log() {
return new Log();
} }
2.測试类:
package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_8; 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_8/ApplicationContext-test.xml" })
public class ChiefTest { @Autowired
private ApplicationContext applicationContext; @Test
public 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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
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.xsd"> <context:component-scan
base-package="com.raylee.my_new_spring.my_new_spring.ch03.topic_1_8" />
<aop:aspectj-autoproxy /> </beans>
測试输出:
blueberryCheeseCake
jack make blueberryCheeseCake
总结:这一章节主要介绍一个简单的AOP日志实现,扩展添加检查订单功能。以便记录并检測输入的參数。
从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展添加检查订单功能,以便记录并检測输入的參数的更多相关文章
- 从头认识Spring-3.4 简单的AOP日志实现-扩展添加检查订单功能,以便记录并检測输入的參数
这一章节我们再上一个章节的基础上加上一个检查订单功能 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1 ...
- 从头认识Spring-3.1 简单的AOP日志实现-某方法之前的前后记录日志
这一章节我们引入简单的AOP日志实现. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_1; pub ...
- 【Spring】的【Bean】管理(注解)【四个相同功能的注解】
[Spring]的[Bean]管理(注解)[四个相同功能的注解] 注解:代码里面特殊的标记,使用注解也可以完成一些相关的功能. 注解写法:@注解名称(属性名称=属性值) 注解使用在类.方法.属性上面 ...
- 自己实现简单的AOP(三) 实现增强四项基本功能
前面的两篇随笔,都是只是个铺垫,真正实现增强四项基本功能的重头戏,在本篇随笔中, 本文将通过AOP实现如下的四个基本功能: /// <para>1.自动管理数据库连接[可选]</pa ...
- [ SSH框架 ] Spring框架学习之三(AOP开发和注解的使用)
一.Spring 使用 AspectJ 进行 AOP 的开发:注解的方式 1.1 引入相关的jar包 1.2 引入spring的配置文件 <?xml version="1.0" ...
- Spring MVC 基于URL的映射规则(注解版)
好几天没有跟进Spring MVC的学习了,之前看了点源码都忘的差不多了.这次就跟着之前的问题,继续总结下Spring MVC中的小知识. 关于SpringMVC的小demo可以参考这里! url-p ...
- Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版
一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...
- Spring MVC 基于Method的映射规则(注解版)
在Restful风格的web开发中,根据不同的请求方法使用相应的控制器处理逻辑成为核心需求,下面就看看如何在Spring MVC中识别不同的请求方法. 请求方法 在Http中,请求的方法有很多种,最常 ...
- Spring Boot Mybatis简单使用
Spring Boot Mybatis简单使用 步骤说明 build.gradle:依赖添加 application.properties:配置添加 代码编写 测试 build.gradle:依赖添加 ...
随机推荐
- 网页截图工具CutyCapt
网页截图工具CutyCapt CuteCapt是Kali Linux提供的一款网页截图工具.该工具运行在命令行中,可以将WebKit引擎解析的网页保存为图片.它保存的文件支持矢量图和位图两大类型, ...
- 【线段树】Gym - 100507C - Zhenya moves from parents
线段树每个结点维护两个值,分别是这个区间的 负债 和 余钱. 按时间顺序从前往后看的时候,显然负债是单调不减的. 按时间顺序从后往前看的时候,显然余钱也是单调不减的,因为之前如果有余钱,可能会增加现在 ...
- Echarts无数据时只显示文字不显示动画
只需要在option中加入如下代码即可: noDataLoadingOption: { text: '暂无数据', ...
- Asp.Net MVC part45 过滤器、模板页
过滤器 使用方式自定义类继承自相应的类或接口,重写方法,作为特性使用在控制器类中重写方法 特性方式的使用注意:如果继承自接口需要让类实现FilterAttribute,才可以作为特性使用使用方式1:作 ...
- 敏捷开发中的sprint是什么意思_百度知道
敏捷开发中的sprint是什么意思_百度知道 敏捷开发中的sprint是什么意思 未成年RB21 | 浏览 4208 次 推荐于2016-02-27 15:19:02 最佳 ...
- js之对象(经典)
一.对象的定义: 对象是JavaScript的一个基本数据类型,是一种复合值,它将很多值(原始值或者其他对象)聚合在一起,可通过名字访问这些值.即属性的无序集合. 二.对象的创建(多种方法) 1.对象 ...
- 使用ab.exe监测100个并发/100次请求情况下同步/异步访问数据库的性能差异
ab.exe介绍 ab.exe是apache server的一个组件,用于监测并发请求,并显示监测数据 具体使用及下载地址请参考:http://www.cnblogs.com/gossip/p/439 ...
- easyui combobox 的取值问题
easy-combobox 取值问题 例子:<select id="cc" class="easyui-combobox" name="cc&q ...
- Linux tftp配置
TFTP是用来下载远程文件的最简单网络协议,它其于UDP协议而实现. xinetd(eXtended InterNET services daemon)是新一代的网络守护进程服务程序,又叫超级INTE ...
- flask_admin model官方文档学习
文档地址:http://www.minzhulou.com/docs/flask-admin/api/mod_model.html model在flask_admin算是比较重要的部分,根据文档稍微的 ...