从头认识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 ...
随机推荐
- hard fault 学习记录
使用 segger 的 hard fault 的源文件后,当调试时,发生硬件错误的时候,可以查看 HardFaultRegs 中的内容,并对比 segger_HardFaultHandler.c 中的 ...
- 【php】查看扩展的版本
php --ri [扩展名称] 例如: php --ri mongodb mongodb MongoDB support => enabled MongoDB extension version ...
- Python3使用PyMySQL操作数据库
1. 安装PyMySQL pip install PyMySQL 关于PyMySQL的详细内容可以查看官方文档 Github 2. 创建表 在某个数据库内,使用以下指令建表 CREATE TABLE ...
- Mysql中max函数取得的值不是最大
①问题:遇到一个很有意思的问题,这里记录一下, 就是在使用max函数的时候发现取得的最大值其实不是最大值. 比如: 某一列中有10000000,和9999999, 其最大值应该是10000000但是查 ...
- POJ 2349 Arctic Network(贪心 最小生成树)
题意: 给定n个点, 要求修p-1条路使其连通, 但是现在有s个卫星, 每两个卫星可以免费构成连通(意思是不需要修路了), 问修的路最长距离是多少. 分析: s个卫星可以代替s-1条路, 所以只要求最 ...
- 配置工程文件dll编译后copy路径
放到工程文件的最后面的配置节点: 下面的配置节点中生成路径换成实际的相对路径就可以了 修改:Prject.csproj 文件里面的配置节点 project配置节点里面的最后面 <Target ...
- 【转】windows下nginx+mono+fastCGI部署asp.net网站
原文链接:http://www.cnblogs.com/amityat/archive/2011/08/23/2150153.html 1,什么是nginx 简介Nginx ("engine ...
- 大数据学习——hive数仓DML和DDL操作
1 创建一个分区表 create table t_partition001(ip string,duration int) partitioned by(country string) row for ...
- window查看哪些端口被占用命令
管理员方式运行cmd netstat -n
- 80. Hibernate 5.0命名策略使用naming-strategy 不起作用【从零开始学Spring Boot】
[原创文章,转载请注明出处] 事情的起因:一不小心从1.3.3升级到了1.4.0版本,结果就碰到了各种悲催的事情了,好吧,Hibernate5.0的新特性就是其中一个坑,我们会发现我们配置的namin ...