这一章节我们引入简单的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; @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_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日志实现。

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

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

从头认识Spring-3.1 简单的AOP日志实现-某方法之前的前后记录日志的更多相关文章

  1. 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展添加检查订单功能,以便记录并检測输入的參数

    这一章节我们讨论一下扩展添加检查订单功能,以便记录并检測输入的參数. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03 ...

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

    这一章节我们再上一个章节的基础上加上一个检查订单功能 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1 ...

  3. php 简单通用的日志记录方法

    使用file_put_contents 方法来实现简单便捷的日志记录功能 方法1: // use \r\n for new line on windows, just \n on linux func ...

  4. Spring学习记录(十二)---AOP理解和基于注解配置

    Spring核心之二:AOP(Aspect Oriented Programming) --- 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...

  5. Spring Security4.X 简单实例介绍

    简介 本例子采用的是SpringMVC.SpringSecurity和Spring整合的简单使用 使用gradle搭建的项目(gradle比maven更加便捷),可以自行了解 web.xml配置 &l ...

  6. Spring框架IOC容器和AOP解析

    主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置  一.S ...

  7. Spring的注解方式实现AOP

    Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...

  8. 使用Spring的注解方式实现AOP

    Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...

  9. Spring笔记——使用Spring进行面向切面(AOP)编程

    要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间: =================== Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种: 1. 基于XM ...

随机推荐

  1. javascript.json snippets vscode 注释

    vscode vue js里面的注释 javascript.json { // Place your global snippets here. Each snippet is defined und ...

  2. ES6(vue)对象词法扩展

    ES6 允许声明在对象字面量时使用简写语法,来初始化属性变量和函数的定义方法,并且允许在对象属性中进行计算操作: function getCar(make, model, value) { retur ...

  3. G7或变G6+1?特朗普七国峰会箱友军开炮

    今日导读 G7 峰会刚召开完毕,德国总理默克尔发的一张照片就迅速火遍全球.照片中她身体前倾,像是在质问特朗普,而后者则双手交叉胸前,我自岿然不动,这张火药味十足的照片不禁让人脑补当时剑拔弩张的气氛.到 ...

  4. SQL Sever中多列拼接成一列值为NULL

    查询出数据 SELECT a.ID AS KYMain_ID , ',' + a.Leader + ',' AS KYMain_Leader , ), b.TaskLeader) FROM TB_KY ...

  5. selenium Select下拉框

    先来认识一下下拉框,以百度的“高级设置”为例 介绍两种方法来处理下拉框:使用click事件,使用Select方法 使用click事件 上述下拉框的源代码如下: 虽然我们可以在html源文件中看到sel ...

  6. 每日命令:(14)tune2fs

    tune2fs简介 tune2fs是调整和查看ext2/ext3文件系统的文件系统参数,Windows下面如果出现意外断电死机情况,下次开机一般都会出现系统自检.Linux系统下面也有文件系统自检,而 ...

  7. python读取excel学习(1)

    #coding=gbk #coding=utf-8 import xlrd table = xlrd.open_workbook(r'E:\test.xlsx') #sheet = table.she ...

  8. rails使用bootstrap

    在Gemfile文件中添加'bootstrap-sass',再运行bundle install gem 'bootstrap-sass' 在config/application.rb添加一行代码,让b ...

  9. LeetCode(51) N-Queens

    题目 The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two quee ...

  10. python基础——6(字符编码,文件操作)

    今日内容: 1.字符编码: 人识别的语言与机器识别的语言转化的媒介 *****     2.字符与字节: 字符占多少字节,字符串转化 ***     3.文件操作: 操作硬盘中的一块区域:读写操作  ...