下面是配置文件aop-servlet.xml,使用的版本是spring3.1
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- <span style="color:#ff0000"> </span>http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
- ">
- <aop:aspectj-autoproxy />
- <bean id="logAdvice" class="com.cloudData.aop.LogAdvice"></bean>
- </beans>
- package com.cloudData.aop;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.AfterThrowing;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.Pointcut;
- /**
- * @author LiuKe
- * @version 1.0 2013-10-21
- * @show 日志切面
- */
- @Aspect
- public class LogAdvice{
- @Pointcut("execution(* *.handleRequest(..))")
- public void log(){}
- @Before("log()")
- public void LogRequest(JoinPoint joinPoint)
- {
- Object obj[] = joinPoint.getArgs();
- for(Object o :obj){
- System.out.println("----------------"+o+"----------------");
- }
- // System.out.println("========checkSecurity=="+joinPoint.getSignature().getName());//这个是获得方法名
- // System.out.println("记录了一个请求");
- }
- @After("log()")
- public void LogResponse(JoinPoint joinPoint)
- {
- }
- @AfterThrowing("log()")
- public void LogThrowing(JoinPoint joinPoint)
- {
- }
- }
- @Pointcut("execution(* *.handleRequest(..))")
- public void log(){}
指明了log()方法代表的切点,可以理解为log()方法是一个钩子,他的方法体为空即可,它钩到@Pointcut定义的切点方法,在后面的@Before等配置中,直接配置“log()”即可
@Before("log()"),@After("log()"),@AfterThrowing("log()")分别表示在* *.handleRequest(..)之前调用,* *.handleRequest(..)之后调用,
* *.handleRequest(..)抛出异常调用。
JoinPoint代表切点,joinpoint实例中可以获取切点方法的方法名,参数等信息。
最后,在LogRequest(JoingPoint joinPoint)等方法中,添加日志代码即可。
下面贴出几种常用的记录日志的方法,可自行选择一种加入到LogRequest LogResponse 或LogThrowing中,下面提供的几种记录日志方法的区别,读者可自己测试体会
Log4jTest.java
- package com.test.log4jtest;
- import java.io.FileOutputStream;
- import org.apache.log4j.ConsoleAppender;
- import org.apache.log4j.FileAppender;
- import org.apache.log4j.HTMLLayout;
- import org.apache.log4j.Level;
- import org.apache.log4j.Logger;
- import org.apache.log4j.PatternLayout;
- import org.apache.log4j.SimpleLayout;
- import org.apache.log4j.WriterAppender;
- public class Log4jTest {
- static Logger logger = Logger.getLogger(Log4jTest.class);
- public static void main(String args[]) {
- // output1();//SimpleLayout和 FileAppender
- // output2();//HTMLLayout和 WriterAppender
- output3();//PatternLayout和 ConsoleAppender
- }
- public static void output1()
- {
- SimpleLayout layout = new SimpleLayout();
- FileAppender appender = null;
- try {
- appender = new FileAppender(layout,"output1.txt",false);
- } catch(Exception e) {}
- logger.addAppender(appender);
- logger.setLevel((Level) Level.DEBUG);
- logger.debug("Here is some DEBUG");
- logger.info("Here is some INFO");
- logger.warn("Here is some WARN");
- logger.error("Here is some ERROR");
- logger.fatal("Here is some FATAL");
- }
- public static void output2()
- {
- HTMLLayout layout = new HTMLLayout();
- WriterAppender appender = null;
- try {
- FileOutputStream output = new FileOutputStream("output2.html");
- appender = new WriterAppender(layout,output);
- } catch(Exception e) {}
- logger.addAppender(appender);
- logger.setLevel((Level) Level.DEBUG);
- logger.debug("Here is some DEBUG");
- logger.info("Here is some INFO");
- logger.warn("Here is some WARN");
- logger.error("Here is some ERROR");
- logger.fatal("Here is some FATAL");
- }
- public static void output3()
- {
- // Note, %n is newline
- String pattern = "Milliseconds since program start: %r %n";
- pattern += "Classname of caller: %C %n";
- pattern += "Date in ISO8601 format: %d{ISO8601} %n";
- pattern += "Location of log event: %l %n";
- pattern += "Message: %m %n %n";
- PatternLayout layout = new PatternLayout(pattern);
- ConsoleAppender appender = new ConsoleAppender(layout);
- logger.addAppender(appender);
- logger.setLevel((Level) Level.DEBUG);
- logger.debug("Here is some DEBUG");
- logger.info("Here is some INFO");
- logger.warn("Here is some WARN");
- logger.error("Here is some ERROR");
- logger.fatal("Here is some FATAL");
- }
- }