利用aop的方式打印日志。
蛮好用的。
废话不多说,直接上代码。
1 Log注解定义
//Log.java
package com.sigh.test;
import java.lang.annotation.*;
/**
* Created by sigh on 2015/6/25.
*/
/**
* 拦截器定义
* @see LogAspect
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD})
public @interface Log {
}
2 拦截器定义
//LogAspect.java
package com.sigh.test;
import net.paoding.rose.scanning.context.RoseAppContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
/**
* Created by sigh on 2015/6/25.
*/
@Service
@Aspect
public class LogAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);
@Pointcut("@annotation(Log)")
public void logPointcut() {
}
@Around("logPointcut()")
public Object doSurround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object[] objects = proceedingJoinPoint.getArgs();
String name = proceedingJoinPoint.getSignature().getName();
long startTime = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
LOGGER.info("class: {}, function name: {}", proceedingJoinPoint.getTarget().getClass().getName(), name);
LOGGER.info("in args: <{}>, out args: <{}>", objects, result);
LOGGER.info("execute time: {}", System.currentTimeMillis() - startTime);
return result;
}
public static void main(String[] args) {
//ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/spring-config.xml");
ApplicationContext applicationContext = new RoseAppContext().getApplicationContext();
First first = (First) applicationContext.getBean("first");
Second second = (Second) applicationContext.getBean("second");
Third third = (Third) applicationContext.getBean("third");
first.run(3);
first.report();
second.doWork(1, 2, true);
second.display();
//third.logTest(new Object[] {first, second});
third.logTest(null);
}
}
3 示例测试类
//First.java
package com.sigh.test;
import org.springframework.stereotype.Service;
/**
* Created by sigh on 2015/6/9.
*/
@Service
public class First {
@Log
public boolean run(int x) {
System.out.println("first " + x);
return true;
}
@Log
public void report() {
System.out.println("report first");
}
}
//Second.java
package com.sigh.test;
import org.springframework.stereotype.Service;
/**
* Created by sigh on 2015/6/9.
*/
@Service
public class Second {
@Log
public long doWork(int a, long b, boolean c) {
System.out.println("second " + a + " " + b + " " + c);
return 3;
}
@Log
public void display() {
System.out.println("display second");
}
}
//Third.java
package com.sigh.test;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* Created by sigh on 2015/6/26.
*/
@Service
public class Third {
@Log
public List<Object> logTest(Object[] objects) {
List<Object> list = new ArrayList<Object>();
for (Object o : objects) {
list.add(o);
}
return list;
}
}
4 applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.sigh.test">
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true" />
</beans>
**<aop:aspectj-autoproxy proxy-target-class="true" /> **
这行很重要,没有的话无法拦截,具体是jdk代理和cglib代理的区别问题,如下:
(1) jdk代理实现的具体过程:
a. 获取指定类上的所有接口列表
b. 确定要生成的代理类的类名,默认为com.sun.proxy.$ProxyXXXX
c. 根据需要实现的接口信息,在代码中动态创建该Proxy类的字节码
d. 将对应的字节码转换为相应的class对象
e. 创建InvocationHandler实例handler,用来处理Proxy所有方法调用
f. Proxy的class对象以创建的handler对象为参数,实例化一个Proxy对象
(2) cglib代理的具体过程:
a. 查找指定类上的所有非final的public类型的方法定义
b. 将这些方法的定义转换为字节码
c. 将组成的字节码转换成相应的代理的class对象
d. 实现MethodIntereceptor接口,用来处理对代理类上所有方法的请求
(3) 两者的区别:
a. jdk使用接口+组合的方式实现,而cglib使用继承的方式实现,因此对于没有接口的类来说,jdk代理不可用
b. 通常来说,jdk代理的所有功能cglib都具有,反之不然
基于以上原理,可以解释上述问题。
a. 当没有增加<aop:aspectj-autoproxy proxy-target-class="true" />时,aop不生效——因为aop默认使用jdk代理实现,而被拦截的类不具有接口。因此需要使用cglib代理
b. 当方法非public时,aop不生效——因为cglib代理只代理非final的public类型方法
ps: 1. mvn配置起来很恶心,还有包的冲突问题
ps: 2. 如果不把目标方法设置为public,使用注解拦截会有问题,调了半个下午
网友评论