从头认识Spring-1.8 怎样利用AOP记录日志?
来源:互联网 发布:淘宝店上传图片的大小 编辑:程序博客网 时间:2024/05/22 10:38
由于之前有网友提出Aop的使用的问题,我们这一章节来讨论一下利用AOP记录日志。
我们下面将以用户登录为例子
1.domain
User类:
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8;public class User {private String name = "";private int id = 0;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}}
用户这里我们比较简单,只记录id和name
登录实现类:
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8;public class LoginServiceImpl {public void login(User user) {if (user != null) {System.out.println(user.getName() + " login");}}}
登录实现类主要打印一下登录的信息。
日志类:
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8;import org.aspectj.lang.JoinPoint;public class Log {public void before() {System.out.println("before login);}public void after(JoinPoint joinpoint) {Object[] objects = joinpoint.getArgs();for (int i = 0; i < objects.length; i++) {User user = (User) objects[i];System.out.println("save login log:" + user.getName() + " login");}}}
日志这里我们需要记录两个东西,一个在登录这个动作之前做的,一个是登录日志之后记录登录日志的操作,这里需要有一个注意的地方:
就是这里面的方法,可以带参数或者无参
而我们的数据流将可以通过JoinPoint这个类来获得
2.测试类:
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8;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/ch01/topic_1_8/ApplicationContext-test.xml" })public class LoginTest {@Autowiredprivate ApplicationContext applicationContext;@Testpublic void testLogin() {LoginServiceImpl loginServiceImpl = applicationContext.getBean(LoginServiceImpl.class);User user = applicationContext.getBean(User.class);loginServiceImpl.login(user);}}
只是简单的get那个Bean出来,然后登录一下。
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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsdhttp://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"><bean id="user"class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8.User"><property name="id" value="1" /><property name="name" value="jack" /></bean><bean id="loginServiceImpl"class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8.LoginServiceImpl" /><bean id="log"class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8.Log" /><aop:config><aop:aspect ref="log"><aop:pointcutexpression="execution(* com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8.LoginServiceImpl.*(..))"id="logPointcut" /><aop:before method="before" pointcut-ref="logPointcut" /><aop:after method="after" pointcut-ref="logPointcut" /></aop:aspect></aop:config></beans>
配置文件跟之前的aop那一章节的基本一样。
测试输出:
before login
jack login
save login log:jack login
总结:这一章节主要介绍怎样利用AOP记录日志,以及里面需要注意的JoinPoint。
目录:http://blog.csdn.net/raylee2007/article/details/50611627
我的github:https://github.com/raylee2015/my_new_spring
0 0
- 从头认识Spring-1.8 怎样利用AOP记录日志?
- 从头认识Spring-3.1 简单的AOP日志实现-某方法之前的前后记录日志
- 从头认识Spring-3.2 简单的AOP日志实现-需要记录方法的运行时间
- 从头认识Spring-3.2 简单的AOP日志实现-需要记录方法的运行时间
- 从头认识Spring-3.5 简单的AOP日志实现(注解版)-某方法之前的前后记录日志
- 从头认识Spring-3.6 简单的AOP日志实现(注解版)-需要记录方法的运行时间
- 利用Spring的AOP进行日志记录
- 从头认识Spring-3.4 简单的AOP日志实现-扩展增加检查订单功能,以便记录并检测输入的参数
- 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展增加检查订单功能,以便记录并检测输入的参数
- 从头认识Spring-3.3 简单的AOP日志实现-增加检查订单功能
- 从头认识Spring-3.7 简单的AOP日志实现(注解版)-增加检查订单功能
- spring aop 日志记录
- Spring AOP记录日志!
- Spring AOP记录日志! .
- spring aop记录日志
- Spring AOP 记录日志
- Spring AOP日志记录
- Spring AOP 记录日志
- 在对话框上创建视图的方法总结
- 173Binary Search Tree Iterator
- eclipse 选项设置
- java hdu2188 巴特博弈
- AVFoundation之语音合成器
- 从头认识Spring-1.8 怎样利用AOP记录日志?
- NOIP2010:接水问题
- tornado实现的简单异步调用的例子
- setTimeout和setInterval的使用区别
- 快速掌握一个语言最常用的50%
- Linux学习:第九周
- 初学最短路算法
- leetcode 136. Single Number
- 高效阅读法