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

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 = ""; @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_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 {
@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();
} }

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

我们上面引入了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 { @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_3" />
<aop:config>
<aop:aspect ref="log">
<aop:pointcut
expression="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日志实现,扩展添加检查订单功能。以便记录并检測输入的參数。

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

  1. 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展添加检查订单功能,以便记录并检測输入的參数

    这一章节我们讨论一下扩展添加检查订单功能,以便记录并检測输入的參数. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03 ...

  2. 从头认识Spring-3.1 简单的AOP日志实现-某方法之前的前后记录日志

    这一章节我们引入简单的AOP日志实现. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_1; pub ...

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

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

  4. Spring Security4.X 简单实例介绍

    简介 本例子采用的是SpringMVC.SpringSecurity和Spring整合的简单使用 使用gradle搭建的项目(gradle比maven更加便捷),可以自行了解 web.xml配置 &l ...

  5. Spring框架IOC容器和AOP解析

    主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置  一.S ...

  6. Spring的注解方式实现AOP

    Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...

  7. 使用Spring的注解方式实现AOP

    Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...

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

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

  9. Spring (二) OOP V.S AOP

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

随机推荐

  1. Mysql存储过程包括事务,且传入sql数据运行

    有这样一个需求.要求在mysql存储过程中使用到事务,并且运行的是动态的sql语句 代码例如以下: BEGIN DECLARE in_data TEXT; /** 标记是否出错 */ DECLARE ...

  2. 安卓系统底层C语言算法之測试參数是几个long型的算法

    #include <stdio.h> #define BITS_PER_LONG (sizeof(unsigned long) * 8) //求一个数x是几个long的长度 #define ...

  3. Spring经常使用属性的注入及属性编辑器

    对于对象的注入,我们使用ref方式,能够指定注入的对象.以下看下对于基本类型的注入.以及当spring无法转换基本类型进行注入时,怎样编写一个相似转换器的东西来完毕注入. 一.基本类型的注入 以下写一 ...

  4. android WebViewClient和WebChromeClient

    一.Android之WebViewClient与WebChromeClient的区别 ANDROID应用开发的时候可能会用到WEBVIEW这个组件,使用过程中可能会接触到WEBVIEWCLIENT与W ...

  5. Swift - 判断是否有某功能访问权限,没有则提示,并自动跳转到设置页

    由于 iOS 系统的安全限制,App 如果需要访问设备的通讯录.麦克风. 相册. 相机.地理位置等时,需要请求用户是否允许访问.   有时用户不小心点了“不允许”,后面可能就不知道要去哪里再开启这个权 ...

  6. [HDU 5542] The Battle of Chibi

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5542 [算法] 树状数组优化DP [代码] #include<bits/stdc++.h&g ...

  7. Android WebView访问网站携带登录认证Cookies和动态自定义的cookies

    最近项目几个页面要复用微信程序的网页.但是需要调用微网站登录接口,返回Cookies,webview访问需要的网页的时候携带. 并且还需要几个其他的动态改变的cookie,目的是根据这几个动态自定义c ...

  8. WCF(四)windows服务寄宿

    WCF常用的寄宿方式除了IIS寄宿外,还有一种方式是寄宿到windows服务中,跟随系统启动而启动. 1.在项目中选择“添加”--“新建windows服务” 2.打开系统生成的设计界面的代码,引用“u ...

  9. Codeforces Round #284 (Div. 2) A

    解题思路:给出 n个电影的精彩时段(a[i],b[i]),和每次可以跳过的时间x,问要看完所有的精彩时刻,至少需要看多长时间的电影. 因为要时间最少,所有除了精彩时刻的电影则能跳过就跳过(用取余来算) ...

  10. svn: E155017: Checksum mismatch while updating 校验错误的解决方法

    [10.3.53.53:/EMRCV5]# svn up svn: E155004: Working copy '/EMRCV5' locked. svn: E155004: '/EMRCV5' is ...