转载自:http://blog.csdn.net/raylee2007/article/details/50710168
上一章节我们只是在做蛋糕的前后记录了一下日志,这个不够,我们需要记录做蛋糕需要的时间,这里就需要引入<aop:around/>标签。
1.domain
蛋糕类:
- package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_2;
- 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_2;
- 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_2;
- 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() {
- System.out.println(getName() + " make " + getCake().getName());
- }
- }
上面的几个类我们只是简单的定义了一些属性,只是在厨师类那里加上了一个简单的方法。
日志类:
- package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_2;
- import org.aspectj.lang.ProceedingJoinPoint;
- public class Log {
- public void washOven() {
- System.out.println("washOven,logging.....");
- }
- 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();
- }
- }
由于需要记录做蛋糕的时间,我们这里引入around方法
在arond方法这里,我们需要关注的就是ProceedingJoinPoint ,我们看到里面有一句joinPoint.proceed();,其实就是代表厨师类里面makeOneCake这个方法的执行。
配置类:(我们这里使用基于java的配置)
- package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_2;
- 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_2;
- 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_1/ApplicationContext-test.xml" })
- public class ChiefTest {
- @Autowired
- private ApplicationContext applicationContext;
- @Test
- public void testChief() {
- Chief jack = (Chief) applicationContext.getBean(Chief.class);
- jack.makeOneCake();
- }
- }
- <?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_2" />
- <aop:config>
- <aop:aspect ref="log">
- <aop:pointcut
- expression="(execution(* com.raylee.my_new_spring.my_new_spring.ch03.topic_1_2.Chief.*(..)))"
- id="chiefPointCut" />
- <aop:around method="around" />
- </aop:aspect>
- </aop:config>
- </beans>
在配置文件里面,我们引入了around 的标签,他代表的意义是,围绕着某个方法的执行。
测试输出:
washOven,logging.....
prepare,logging.....
jack make blueberryCheeseCake
use time:31
after someting to do,logging.....
总结:这一章节主要介绍一个简单的AOP日志实现。