1. 程式人生 > >從頭認識Spring-3.4 簡單的AOP日誌實現-擴展添加檢查訂單功能,以便記錄並檢測輸入的參數

從頭認識Spring-3.4 簡單的AOP日誌實現-擴展添加檢查訂單功能,以便記錄並檢測輸入的參數

pack logging exe app 基礎上 config round statistic was

這一章節我們再上一個章節的基礎上加上一個檢查訂單功能

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日誌實現,擴展添加檢查訂單功能。以便記錄並檢測輸入的參數。


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

?

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


從頭認識Spring-3.4 簡單的AOP日誌實現-擴展添加檢查訂單功能,以便記錄並檢測輸入的參數