SSH框架系列:Spring AOP应用记录日志Demo
- /*
- * $filename: Logging.java,v $
- * $Date: 2013-12-10 $
- * Copyright (C) ZhengHaibo, Inc. All rights reserved.
- * This software is Made by Zhenghaibo.
- */
- package edu.njupt.zhb;
- /*
- *@author: ZhengHaibo
- *web: http://blog.csdn.net/nuptboyzhb
- *mail: zhb931706659@126.com
- *2013-12-10 Nanjing,njupt,China
- */
- public class Logging {
- public void beforeAdvice(){
- System.out.println("Logging:before... ");
- }
- public void afterAdvice(){
- System.out.println("Logging:after... ");
- }
- /**
- *
- * @param retVal 函数的返回值
- */
- public void afterReturningAdvice(Object retVal){
- if(retVal==null){
- return;
- }
- System.out.println("Logging:return :"+retVal.toString());
- }
- public void afterThrowingAdvice(IllegalArgumentException ex){
- System.out.println("Logging:exception:"+ex.toString());
- }
- }
- /*
- * $filename: Student.java,v $
- * $Date: 2013-12-10 $
- * Copyright (C) ZhengHaibo, Inc. All rights reserved.
- * This software is Made by Zhenghaibo.
- */
- package edu.njupt.zhb;
- /*
- *@author: ZhengHaibo
- *web: http://blog.csdn.net/nuptboyzhb
- *mail: zhb931706659@126.com
- *2013-12-10 Nanjing,njupt,China
- */
- public class Student {
- private String id;
- private String name;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void printThrowException(){
- System.out.println("Exception in Student.class...");
- throw new IllegalArgumentException("Exception from Student...");
- }
- public void print(String say){
- System.out.println("Say:"+say+",Name = "+name);
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <!-- 切面类 -->
- <bean id="myLogging" class="edu.njupt.zhb.Logging"></bean>
- <!-- AOP配置 -->
- <aop:config>
- <aop:aspect id="logStudent" ref="myLogging">
- <!-- pointcut配置 -->
- <aop:pointcut id="allMethod" expression="execution(* edu.njupt.zhb.*.*(..))"/>
- <aop:before pointcut-ref="allMethod" method="beforeAdvice"/>
- <aop:after pointcut-ref="allMethod" method="afterAdvice"/>
- <aop:after-returning pointcut-ref="allMethod" returning="retVal" method="afterReturningAdvice"/>
- <aop:after-throwing pointcut-ref="allMethod" throwing="ex" method="afterThrowingAdvice"/>
- </aop:aspect>
- </aop:config>
- <bean id="student" class="edu.njupt.zhb.Student">
- <property name="id" value="1012010638"></property>
- <property name="name" value="Haibo Zheng"></property>
- </bean>
- </beans>
- package edu.njupt.zhb;
- /*
- * $filename: TestMain.java,v $
- * $Date: 2013-12-10 $
- * Copyright (C) ZhengHaibo, Inc. All rights reserved.
- * This software is Made by Zhenghaibo.
- */
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /*
- *@author: ZhengHaibo
- *web: http://blog.csdn.net/nuptboyzhb
- *mail: zhb931706659@126.com
- *2013-12-10 Nanjing,njupt,China
- */
- public class TestMain {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- Student student = (Student)context.getBean("student");
- System.out.println("-------------");
- student.getId();
- System.out.println("-------------");
- student.getName();
- System.out.println("-------------");
- student.print("Hi,I am a student");
- System.out.println("-------------");
- try{
- student.printThrowException();
- }catch (Exception e) {
- // TODO: handle exception
- System.out.println(e.getMessage());
- }
- }
- }
2.5运行结果:
- 2013-12-10 18:32:54 org.springframework.context.support.AbstractApplicationContext prepareRefresh
- 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@145e044: display name [org.springframework.context.support.ClassPathXmlApplicationContext@145e044]; startup date [Tue Dec 10 18:32:54 CST 2013]; root of context hierarchy
- 2013-12-10 18:32:54 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
- 信息: Loading XML bean definitions from class path resource [applicationContext.xml]
- 2013-12-10 18:32:54 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
- 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@145e044]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1f4cbee
- 2013-12-10 18:32:54 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
- 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1f4cbee: defining beans [myLogging,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,allMethod,student]; root of factory hierarchy
- -------------
- Logging:before...
- Logging:after...
- Logging:return :1012010638
- -------------
- Logging:before...
- Logging:after...
- Logging:return :Haibo Zheng
- -------------
- Logging:before...
- Say:Hi,I am a student,Name = Haibo Zheng
- Logging:after...
- -------------
- Logging:before...
- Exception in Student.class...
- Logging:after...
- Logging:exception:java.lang.IllegalArgumentException: Exception from Student...
- Exception from Student...
上述的方法只是在执行函数前,加一些自己的逻辑。如果完成更加复杂的功能,我们可能需要知道函数的名称以及函数的参数及其值等等信息。此时,我们只需要修改Logging类即可,可以修改为如下:(主要是使用了Spring aop中的JointPoint)
- /*
- * $filename: Logging.java,v $
- * $Date: 2013-12-10 $
- * Copyright (C) ZhengHaibo, Inc. All rights reserved.
- * This software is Made by Zhenghaibo.
- */
- package edu.njupt.zhb;
- import org.aspectj.lang.JoinPoint;
- /*
- *@author: ZhengHaibo
- *web: http://blog.csdn.net/nuptboyzhb
- *mail: zhb931706659@126.com
- *2013-12-10 Nanjing,njupt,China
- */
- public class Logging {
- public void beforeAdvice(JoinPoint jointPoint){
- Object methodArgs[] = jointPoint.getArgs();//获取切入点函数的参数
- for(Object arg:methodArgs){
- System.out.println("Logging:args type="+arg.getClass().getName());
- System.out.println("Logging:args value="+arg);
- }
- System.out.println("Logging:ClassName="+jointPoint.getTarget().getClass().getName());
- System.out.println("Logging:MethodName="+jointPoint.getSignature().getName());
- System.out.println("Logging:before... ");
- }
- public void afterAdvice(){
- System.out.println("Logging:after... ");
- }
- /**
- *
- * @param retVal 函数的返回值
- */
- public void afterReturningAdvice(Object retVal){
- if(retVal==null){
- return;
- }
- System.out.println("Logging:return :"+retVal.toString());
- }
- public void afterThrowingAdvice(IllegalArgumentException ex){
- System.out.println("Logging:exception:"+ex.toString());
- }
- }
此时的运行结果为:
- 2013-12-10 19:44:07 org.springframework.context.support.AbstractApplicationContext prepareRefresh
- 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@145e044: display name [org.springframework.context.support.ClassPathXmlApplicationContext@145e044]; startup date [Tue Dec 10 19:44:07 CST 2013]; root of context hierarchy
- 2013-12-10 19:44:07 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
- 信息: Loading XML bean definitions from class path resource [applicationContext.xml]
- 2013-12-10 19:44:07 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
- 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@145e044]: org.springframework.beans.factory.support.DefaultListableBeanFactory@787d6a
- 2013-12-10 19:44:07 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
- 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@787d6a: defining beans [myLogging,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,allMethod,student]; root of factory hierarchy
- -------------
- Logging:ClassName=edu.njupt.zhb.Student
- Logging:MethodName=getId
- Logging:before...
- Logging:after...
- Logging:return :1012010638
- -------------
- Logging:ClassName=edu.njupt.zhb.Student
- Logging:MethodName=getName
- Logging:before...
- Logging:after...
- Logging:return :Haibo Zheng
- -------------
- Logging:args type=java.lang.String
- Logging:args value=Hi,I am a student
- Logging:args type=java.lang.Integer
- Logging:args value=20
- Logging:ClassName=edu.njupt.zhb.Student
- Logging:MethodName=print
- Logging:before...
- Say:Hi,I am a student,Name = Haibo Zheng,age = 20
- Logging:after...
- -------------
- Logging:ClassName=edu.njupt.zhb.Student
- Logging:MethodName=printThrowException
- Logging:before...
- Exception in Student.class...
- Logging:after...
- Logging:exception:java.lang.IllegalArgumentException: Exception from Student...
- Exception from Student...
SSH框架系列:Spring AOP应用记录日志Demo的更多相关文章
- Maven环境下搭建SSH框架之Spring整合Hibernate
© 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Spring:4.3.8.RELEASE Hibernate:5.1.7.Final MySQL:5.7.17 注意:其他版本在某些特性 ...
- Spring框架系列之AOP思想
微信公众号:compassblog 欢迎关注.转发,互相学习,共同进步! 有任何问题,请后台留言联系! 1.AOP概述 (1).什么是 AOP AOP 为 Aspect Oriented Progra ...
- SSH框架系列:Spring配置多个数据源
分类: [java]2013-12-09 16:59 1247人阅读 评论(0) 收藏 举报 1.问题的引入 对于普通的SSH框架而言,一般配置一个数据源,一个SessionFactory,一个事务管 ...
- SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean
分类: [java]2013-12-09 16:29 1020人阅读 评论(0) 收藏 举报 1.简介 在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路 ...
- Eclipse搭建SSH框架(Struts2+Spring+Hibernate)
见识少的我经过一天多的研究才知道,在MyEclipse中搭好的框架的配置文件和jar包是通用的.接下来——亮剑! 工具:Eclipse+Tomcat+Mysql 一.先在Eclipse中配置好Tomc ...
- java框架篇---spring AOP 实现原理
什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入 ...
- SSH框架中spring的原理
在ssh项目中,是有明确分工的,spring的作用就相当于将struts和hibernate连接起来,是将两个没有关系的框架的特性,方法,action都放在spring的配置文件中使他们建立关系.取他 ...
- Spring AOP应用实例demo
AOP(Aspect-Oriented Programming.面向方面编程).能够说是OOP(Object-OrientedPrograming.面向对象编程)的补充和完好.OOP引入封装.继承和多 ...
- SSH框架实现仿淘宝购物demo
还记得六月份实习的时候,曾经做过一个电商的项目,项目里面需要实现类似淘宝购物车的移动端的demo,随着项目的进行,再一次跟购物车碰面,但是今天呢,不是移动端的需求,如何使用SSH框架实现类似淘宝的购物 ...
随机推荐
- Python学习第二十七课——写一个和Django框架的自己的框架
MyWeb框架: from wsgiref.simple_server import make_server def application(environ, start_response): pri ...
- 【Python矩阵及其基础操作】【numpy matrix】
一.矩阵生成 1.numpy.matrix: import numpy as np x = np.matrix([ [1, 2, 3],[4, 5, 6] ]) y = np.matrix( [1, ...
- 详解python的字符编码问题
一:什么是编码 将明文转换为计算机可以识别的编码文本称为“编码”.反之从计算机可识别的编码文本转回为明文为“解码”. 那么什么是明文呢,首先我们从一段信息说起,消息以人们可以理解,易懂的表示存在,我们 ...
- vector 踩过的坑
今天,做LeetCode上的一道题,198题:Rob House,在纸上画了画,发现了重复的结构,就使用了递归的方式实现的 #include<iostream> #include<v ...
- 「ZJOI2011」最小割
「ZJOI2011」最小割 传送门 建出最小割树,然后暴力计算任意两点之间最小割即可. 多组数据记得初始化. 参考代码: #include <algorithm> #include < ...
- Java入门笔记 02-数组
介绍: Java的数组既可以存储基本类型的数据,也可以存储引用类型的数据,但是要求所有的数组元素具有相同的数据类型.另外,Java数组也是一种数据类型,其本身就是一种引用类型. 一.数组的定义: 数据 ...
- Swift3.0-基础知识
本文对Swift做一个从OC的角度的基础知识简单概要. Swift OC 说明 let.var const 在OC中不用const声明的常量,都认为是变量 Float.Double CGFloat ...
- mongodb插入性能
转自 https://blog.csdn.net/asdfsadfasdfsa/article/details/60872180 MongoDB与MySQL的插入.查询性能测试 7.1 平均 ...
- PTA的Python练习题(十)
从 第3章-22 输出大写英文字母 开始 1. a=str(input()) b='' for i in a: if 'A'<=i<='Z' and i not in b: b=b+i i ...
- 2019 深信服 下棋(DFS+回溯)
链接:https://www.nowcoder.com/questionTerminal/a0feb0696e2043a5b3b0779fa861b64a?f=discussion来源:牛客网 8x8 ...