从头认识Spring-3.6 简单的AOP日志实现(注解版)-需要记录方法的运行时间

这一章节我们把前面的第二个章节使用注解的形式来展示。

1.domain

蛋糕类:

package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_6;

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_6;

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_6;

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_6;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Log {

	@Pointcut("execution(* com.raylee.my_new_spring.my_new_spring.ch03.topic_1_6.Chief.*(..))")
	public void makeOneCake() {
	}

	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.....");
	}

	@Around(value = "makeOneCake()")
	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标签


配置类:(我们这里使用基于java的配置)

package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_6;

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_6;

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_6/ApplicationContext-test.xml" })
public class ChiefTest {

	@Autowired
	private ApplicationContext applicationContext;

	@Test
	public void testChief() {
		Chief jack = (Chief) applicationContext.getBean(Chief.class);
		jack.makeOneCake();
	}
}


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_6" />
	<aop:aspectj-autoproxy />


</beans>



测试输出:

washOven,logging.....
prepare,logging.....
jack make blueberryCheeseCake
use time:31
after someting to do,logging.....


总结:这一章节主要介绍使用注解来记录方法的运行时间。


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值