从头认识Spring-3.5 简单的AOP日志实现(注解版)-某方法之前的前后记录日志

这一章节我们使用注解来简单实现AOP日志。

1.domain

蛋糕类:

package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_5;

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

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

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

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Log {

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

	@Before("makeOneCake()")
	public void washOven() {
		System.out.println("washOven,logging.....");
	}

	@Before("makeOneCake()")
	public void prepare() {
		System.out.println("prepare,logging.....");
	}

	@After("makeOneCake()")
	public void after() {
		System.out.println("after someting to do,logging.....");
	}

}

日志类需要注意的地方:

(1)使用@Aspect标签,标注一个切面

(2)方法public void makeOneCake() {},其实只是一个标签,可以理解成xml里面的切面id

(3)@[email protected]就是切点里面的方法前后顺序

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

package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_5;

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

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

</beans>

解释一下配置文件的东西:通过<aop:aspectj-autoproxy />就可以知道注册了的切面信息。

测试输出:

washOven,logging.....
prepare,logging.....
jack make blueberryCheeseCake
after someting to do,logging.....

总结:这一章节主要使用注解来简单实现AOP日志。

目录:http://blog.csdn.net/raylee2007/article/details/50611627

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

时间: 2024-12-29 00:38:30

从头认识Spring-3.5 简单的AOP日志实现(注解版)-某方法之前的前后记录日志的相关文章

从头认识Spring-3.3 简单的AOP日志实现-增加检查订单功能

这一章节我们再上一个章节的基础上加上一个检查订单功能 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_3; public class Cake { private String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; }

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

这一章节我们引入简单的AOP日志实现. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_1; public class Cake { private String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; } } 烤炉类:

从头认识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 =

从头认识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 =

从头认识Spring-3.7 简单的AOP日志实现(注解版)-增加检查订单功能

这一章节我们使用注解来实现检查订单功能 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_7; public class Cake { private String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; } } 烤炉类:

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

这一章节我们再上一个章节的基础上加上一个检查订单功能 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; }

Spring中AspectJ的实现AOP(XML+注解)

AOP相关术语如下: 1. Joinpoint(连接点) -- 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点 2. Pointcut(切入点) -- 所谓切入点是指我们要对哪些Joinpoint进行拦截的定义 3. Advice(通知/增强) -- 所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能) 4. Introduction(引介) -- 引

spring mvc+hibernate 实现事务管理(全注解版)

为了方便项目变大配置文件变多,用注解代替 *.hbm.xml,<bean id="*dao" class="">,另外用反省实现dao操作,省去每个类一个dao,此处参考了鸵鸟的例子. 实现功能跟http://blog.csdn.net/waiwai4701/article/details/38270721这个项目是一样的,controller和页面就不再写 首先,jar包支持,为了方便jar包管理采用maven技术,服务器没有用tomcat用的jett

spring学习3_通过注解简单实现AOP

在上一篇中通过XML配置演示了Spring实际进行AOP的过程,这里简单介绍一下通过注解实现这一功能的过程. 1.Spring配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins