从头认识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;
} }
烤炉类:
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
从头认识Spring-3.1 简单的AOP日志实现-某方法之前的前后记录日志的更多相关文章
- 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展添加检查订单功能,以便记录并检測输入的參数
这一章节我们讨论一下扩展添加检查订单功能,以便记录并检測输入的參数. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03 ...
- 从头认识Spring-3.4 简单的AOP日志实现-扩展添加检查订单功能,以便记录并检測输入的參数
这一章节我们再上一个章节的基础上加上一个检查订单功能 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1 ...
- php 简单通用的日志记录方法
使用file_put_contents 方法来实现简单便捷的日志记录功能 方法1: // use \r\n for new line on windows, just \n on linux func ...
- Spring学习记录(十二)---AOP理解和基于注解配置
Spring核心之二:AOP(Aspect Oriented Programming) --- 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...
- Spring Security4.X 简单实例介绍
简介 本例子采用的是SpringMVC.SpringSecurity和Spring整合的简单使用 使用gradle搭建的项目(gradle比maven更加便捷),可以自行了解 web.xml配置 &l ...
- Spring框架IOC容器和AOP解析
主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置 一.S ...
- Spring的注解方式实现AOP
Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...
- 使用Spring的注解方式实现AOP
Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...
- Spring笔记——使用Spring进行面向切面(AOP)编程
要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间: =================== Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种: 1. 基于XM ...
随机推荐
- upload 上传按钮组件 iview
<!-- * @description 导入Excel * @fileName importExcel.vue * @author 彭成刚 * @date // :: * @version V1 ...
- 如何卸载eclipse中的pydev
在Eclipse中依次点击菜单"Help"->"About Eclipse". 点击"Installation Details"按钮, ...
- ios之UITextfield (2)
UItextField通常用于外部数据输入,以实现人机交互.下面以一个简单的登陆界面来讲解UItextField的详细使用. //用来显示“用户名”的label UILabel* label1 = [ ...
- N皇后递归
问题: n皇后问题:输入整数n, 要求n个国际象棋的皇后,摆在 n*n的棋盘上,互相不能攻击,输出全部方案. #include <iostream> using namespace std ...
- wap学习记录
针对移动端浏览器: 大部分的浏览器内核都是webkit内核,对h5和c3的支持非常好 库 国内比较流行的框架 : 妹子UI bootstrap中解决ie8以及以下的h5标签和媒体查询兼容问题的两个库分 ...
- CSS3---关于背景
1.background-origin:设置元素背景图片的原始起始位置. background-origin : border-box | padding-box | content-box; ...
- 如何学好C和C++
酷壳上的两篇文章,转载至此,学好C和C++. 我相信,这可能是很多朋友的问题,我以前也有这样的感觉,编程编到一定的时候,发现能力到了瓶颈,既不深,也不扎实,半吊子.比如:你长期地使用Java和.NET ...
- python re 正则表达式
元字符和其含义 . 匹配除换行符以外的任意字符 \ 转义字符,使后一个字符改变原来的意思 \w 匹配字母.数字.下划线:[A-Za-z0-9_] \W 匹配特殊字符:[^A-Za-z0-9_] \s ...
- JavaScript CSS 等前端推荐
推荐两个网站,JAVASCRIPT与前端相关,两边都可以玩,蛮方便的,然后全通后你拿个全英文版本的证书提升逼格. https://www.freecodecamp.org/ https://www.f ...
- luogu1463 [HAOI2007]反素数
以下证明来自算法竞赛进阶指南 引理一: 答案就是 \([1,n]\) 之间约数个数最多的最小的数. 证明: 记 \(m\) 是 \([1,n]\) 之间约数个数最多的最小的数.则 \(\forall ...