Spring AOP实现日志服务
pom.xml需要的jar
代码语言:javascript复制<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
代码语言:javascript复制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配置
代码语言:javascript复制<?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.xsd
http://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
代码语言:javascript复制package blog.csdn.net.unix21;
public interface IHello {
public void hello(String name);
}
HelloSpeaker.java
代码语言:javascript复制package blog.csdn.net.unix21;
public class HelloSpeaker implements IHello {
public void hello(String name){
System.out.println("Hello, " name);
}
}
测试Before Advice
代码语言:javascript复制 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接口来实现
代码语言:javascript复制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配置
代码语言:javascript复制<?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.xsd
http://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
本文由来源 21aspnet,由 system_mush 整理编辑,其版权均为 21aspnet 所有,文章内容系作者个人观点,不代表 Java架构师必看 对观点赞同或支持。如需转载,请注明文章来源。