Spring AOP实现日志服务

pom.xml需要的jar

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.4</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.2.4.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.4.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.2.4.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.4.RELEASE</version>
</dependency>
<dependency><groupId>cglib</groupId><artifactId>cglib-nodep</artifactId><version>3.2.0</version>
</dependency>
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.8</version>
</dependency>
<dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId><version>1.0</version>
</dependency>
<dependency><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId><version>3.2.2</version>
</dependency>
<dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version>
</dependency>

1.Before Advice

Before Advice会在目标方法执行之前被调用,可以通过实现org.springframework.aop.MethodBeforeAdvice接口实现。

LogBeforeAdvice.java

package com.blog.csdn.net.unix21;import java.lang.reflect.Method;
import java.util.logging.Level;
import org.springframework.aop.MethodBeforeAdvice;
import java.util.logging.Logger;/**** @author http://blog.csdn.net/unix21/*/
public class LogBeforeAdvice implements MethodBeforeAdvice {private Logger logger=Logger.getLogger(this.getClass().getName());public void before(Method method,Object[] args,Object target) throws Throwable{logger.log(Level.INFO,"日志信息>>>method starts..."+method);}
}

beans-config.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="   http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.0.xsd  http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<bean id="LogBeforeAdvice" class="blog.csdn.net.unix21.LogBeforeAdvice"/>
<bean id="HelloSpeaker" class="blog.csdn.net.unix21.HelloSpeake"/>
<bean id="HelloProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="proxyInterfaces" value="blog.csdn.net.unix21.IHello"/><property name="target" ref="HelloSpeaker"/><property name="interceptorNames"><list><value>LogBeforeAdvice</value></list></property>
</bean>
</beans>

接口

IHello.java

package blog.csdn.net.unix21;public interface IHello {
public void hello(String name);
}

HelloSpeaker.java

package blog.csdn.net.unix21;public class HelloSpeaker implements IHello {public void hello(String name){System.out.println("Hello, "+name);}
}

测试Before Advice

  ApplicationContext context=new ClassPathXmlApplicationContext("beans-config.xml");IHello h=(IHello)context.getBean("helloProxy");h.hello("blog.csdn.net.unix21")

运行成功使用AOP的方式打印日志信息:

参考:《Spring 2.0技术手册》

Spring 3.1编写AOP时需要导入的倚赖jar包汇总

菜鸟学SSH(七)——Spring jar包详解

2.After Advice

After Advice会在目标方法执行之后被调用,可以通过实现org.springframework.aop.AfterReturningAdvice接口来实现

import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.aop.AfterReturningAdvice;public class LogAfterAdvice  implements AfterReturningAdvice  {
private Logger logger=Logger.getLogger(this.getClass().getName());public void afterReturning(Object object, Method method, Object[] args, Object target) throws Throwable {logger.log(Level.INFO,"日志信息<<<method ends..."+method);}
}

修改beans-config.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="   http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.0.xsd  http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<bean id="LogBeforeAdvice" class="blog.csdn.net.unix21.LogBeforeAdvice"/>
<bean id="HelloSpeaker" class="blog.csdn.net.unix21.HelloSpeake"/>
<bean id="HelloProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="proxyInterfaces" value="blog.csdn.net.unix21.IHello"/><property name="target" ref="HelloSpeaker"/><property name="interceptorNames"><list><value>LogBeforeAdvice</value><value>LogAfterAdvice</value></list></property>
</bean>
</beans>

成功的执行了LogAfterAdvice

Spring AOP与IOC相关推荐

  1. Spring AOP与IOC以及自定义注解

    Spring AOP实现日志服务 pom.xml需要的jar <dependency><groupId>org.apache.commons</groupId>&l ...

  2. spring aop原理_Spring知识点总结!已整理成142页离线文档(源码笔记+思维导图)...

    写在前面 由于Spring家族的东西很多,一次性写完也不太现实.所以这一次先更新Spring[最核心]的知识点:AOP和IOC 无论是入门还是面试,理解AOP和IOC都是非常重要的.在面试的时候,我没 ...

  3. Spring AOP Capability and goals

    5.1.AOP概念 让我们首先定义一些中心AOP概念和术语.这些术语不是特定于Spring的.不幸的是,AOP术语不是特别直观.但是,如果Spring使用自己的术语,那将更加令人困惑. 方面:跨越多个 ...

  4. Spring AOP capabilities and goals

    Spring AOP是用纯Java实现的. 不需要特殊的编译过程. Spring AOP不需要控制类加载器层次结构,因此适合在Servlet容器或应用程序服务器中使用. Spring AOP目前仅支持 ...

  5. Spring AOP知识详解

    本文来详细说下spring中的aop内容 文章目录 Spring AOP概述 Spring AOP一代 Pointcut Jointpoint Advice Advisor 织入 Spring AOP ...

  6. 3.2 Spring AOP的设计与实现

    2019独角兽企业重金招聘Python工程师标准>>> JVM的动态代理特性 在Spring AOP实现中,使用的核心技术是动态代理,这实际上是JDK的一个特性(JDK1.3以上的版 ...

  7. Spring 5 中文解析之核心篇-Spring AOP编程

    技术交流群: 面向切面的编程(AOP)通过提供另一种思考程序结构的方式来补充面向对像的编程(OOP).OOP中模块化的关键单元是类,而在AOP中模块化是切面.切面使关注点(例如事务管理)的模块化可以跨 ...

  8. Spring AOP(一)——基础概念

    前文的一些内容更多是针对Spring容器内部的一些特性的描述,接下来一个专题将描述Spring AOP的一些信息,配置细节等等. 介绍 面向切面编程(AOP)是一种新的针对程序结构的思路,它补足了面向 ...

  9. Spring技术内幕(3)Spring AOP的实现

    本章内容 Spring AOP概述.Spring AOP的设计与实现.建立AopProxy代理对象.Spring AOP拦截器调用的实现.Spring AOP的高级特性. 3.1 Spring AOP ...

最新文章

  1. 屏幕按压力度android,android手机 N 所支持的压感技术
  2. 机器学习(十二)——机器学习中的矩阵方法(2)特征值和奇异值
  3. laravel框架中引入Workerman
  4. npcap loopback adapter是什么意思_抖音限流是什么意思? 抖音为什么突然限流?
  5. python学习第19天
  6. 机器学习笔记(十)——Logistic Function AND Softmax Function
  7. 输出图片任意点的像素坐标(结合IRFANVIEW使用)
  8. 二叉树的BFS及DFS
  9. 今年一个偶然的创业,就改变了我的人生
  10. JavaScript——模块化的历史进程梳理
  11. 【数据挖掘】电商数据合集
  12. 一文读懂: 什么是用户故事?What is User Stories?
  13. php weka,使用Weka进行数据挖掘
  14. php清除手机浏览器缓存,js清除浏览器缓存的几种方法
  15. python websockets 网络聊天室V1
  16. 【html】【微信小程序】将图片压缩,文件上传的方法
  17. 问道手游服务器配置文件,问道手游脚本视频教程
  18. 读史蒂芬·利维《黑客:计算机革命的英雄》
  19. shell 编程计算器
  20. 5分钟搞懂计算机的各种时间(GMT、UTC、CST)

热门文章

  1. html display 显示,CSS display显示
  2. java process exit_Java Process.exitValue()中值的含义是什么?
  3. linux怎么创建用户教程,在Linux中如何手动创建一个用户
  4. java aqs源码_Java-AQS源码详解(细节很多!)
  5. 使用Java对轨迹进行抽稀,并生成mvt(Map Vector Tile)瓦片
  6. 机器学习中的数学基础:(1)实际应用中矩阵特征值与特征向量的几何意义
  7. 如何设计四象限电压转换电路?
  8. 力扣(LeetCode)刷题,简单题(第24期)
  9. STM32 GPIO的原理、特性、选型和配置
  10. keras中conv2d,conv2dTranspose的Padding详细介绍