从头认识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:依赖添加 ...
随机推荐
- HDU 1166 敌兵布阵 <线段树 单点修改 区间查询>
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 【树链剖分】【dfs序】【线段树】bzoj2836 魔法树
这道题告诉我们:树链剖分的重标号就是dfs序. #include<cstdio> #include<algorithm> using namespace std; #defin ...
- STL之vector2
描述 依次输入n个整数,每次输入时检查该值是否已经出现在vector中,如果存在则不插入,否则将其插入到开头位置. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() ...
- 【读书笔记】Elasticsearch集成Hadoop最佳实践
前言 本文记录[Elasticsearch集成Hadoop最佳实战]读书笔记 本书总计209页,共7章节,计划时间:20180712-20180717 (每天至少40页) 本文代码地址: https: ...
- 杂谈PID控制算法——第二篇:调·三个量
上面一篇文章讲了一下PID算法中的三个常量大致的在PID算法中起的一个作用,但在实际的使用中,究竟应该如何调节(或者用更加专业的话说是整定)PID控制算法的三个.首先可以将KP,KI,KD三个常量全部 ...
- NDK 调用对象属性
JNIEXPORT jbyteArrayJNICALL Java_com_lanhetech_iso8583_nativeLib_Iso8583NativeLib_pubPack8583 (JNIEn ...
- FFmpeg学习起步 —— 环境搭建
下面是我搭建FFmpeg学习环境的步骤. 一.在Ubuntu下 从http://www.ffmpeg.org/download.html下载最新的FFmpeg版本,我的版本是ffmpeg-2.7.2. ...
- [SpringMVC+redis]自定义aop注解实现控制器访问次数限制
原文:http://www.cnblogs.com/xiaoyangjia/p/3762150.html?utm_source=tuicool 我们需要根据IP去限制用户单位时间的访问次数,防止刷手机 ...
- 支付宝签名验证实现-Delphi版
支付宝签名验证实现-Delphi版 首先介结下支付宝签名验证流程: 一 支付宝密钥生成 支付宝提供秘钥生成工具https://docs.open.alipay.com/291/105971/ 用此下 ...
- 【Echarts】百度Echarts的使用入门+两个简单的小例子+心得
Echarts对于展示结果,有一个很好的表达方式. 1.首先,在官网将js下载到本地,引用到页面上 这里是在开发环境,所以下载最后源代码这个 managerResult.jsp <%@ page ...