博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从头认识Spring-3.1 简单的AOP日志实现-某方法之前的前后记录日志
阅读量:7155 次
发布时间:2019-06-29

本文共 5397 字,大约阅读时间需要 17 分钟。

这一章节我们引入简单的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;	}}
烤炉类:

package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_1;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_1;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_1;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.....");	}}
我们定义了三个方法,头两个是调用方法前运行的,后面一个是调用方法后运行的

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

package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_1;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic 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_1;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();	}}

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_1" /> <aop:config> <aop:aspect ref="log"> <aop:pointcut expression="(execution(* com.raylee.my_new_spring.my_new_spring.ch03.topic_1_1.Chief.*(..)))" id="chiefPointCut" /> <aop:before method="washOven" pointcut-ref="chiefPointCut" /> <aop:before method="prepare" pointcut-ref="chiefPointCut" /> <aop:after method="after" pointcut-ref="chiefPointCut" /> </aop:aspect> </aop:config> </beans>

解释一下配置文件的东西:

(1)我们使用<aop:config/>标签定义aop

(2)然后定义通过<aop:aspect>切面

(3)再通过<aop:pointcut/>定义切入点

(4)最后通过<aop:before/><aop:after/>等定义切点的前后方法

在这里解释一下expression里面的内容

execution(* com.raylee.my_new_spring.my_new_spring.ch03.topic_1_1.Chief.*(..))

(1)第一颗星表示返回随意类型

(2)com.raylee.my_new_spring.my_new_spring.ch03.topic_1_1代表包名

(3)Chief代表那个类

(4)*(..)代表随意方法,里面的..表示不论什么參数

(5)当然我们也能够简单点,全是用*这个东西来标注,只是一般不这样做,由于每个切点相应的应该是不同的方法

測试输出:

washOven,logging.....

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

总结:这一章节主要介绍一个简单的AOP日志实现。

文件夹: 

 

我的github:

文件夹: 

 

我的github:

转载地址:http://nvegl.baihongyu.com/

你可能感兴趣的文章
七、自定义函数 变量的引用传值
查看>>
windows服务器无法远程连接问题分析
查看>>
2010下半年网络规划设计师上午试卷、标准参考答案及分析(2)
查看>>
Fedora系统升级
查看>>
Memcached安装与启用
查看>>
linux mysql表名大小写
查看>>
判断站点访问的终端类型(移动端还是pc端)的方法(转)
查看>>
Scala2.11.7学习笔记(六)Scala特质介绍
查看>>
浏览器缓存图解
查看>>
lvm卷同名解决方法
查看>>
软件项目开发环境构建之三:JIRA7.2.3安装
查看>>
Wiz十大基本使用技巧 | 为知笔记产品博客
查看>>
Linux kernel的定制与微型linux系统实现
查看>>
我的友情链接
查看>>
django安装了pymysql还报MySQLdb module: No module named
查看>>
百度运维部成长分享—没有透明的天花板!
查看>>
CA的搭建与申请
查看>>
我的友情链接
查看>>
WebViewJavascriptBridge 原理分析
查看>>
KVM虚拟化技术之网卡流量聚合-附脚本
查看>>