2008-04-09
Spring AOP 进行统一日志处理
关键字: aop
java代码:
spring配置文件:
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
public class GenericLoggerBean {
private static Logger logger = LogManager.getLogger(GenericLoggerBean.class);
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println(joinPoint.getTarget().getClass());
logger.warn("Beginning method : " + joinPoint.getTarget().getClass() + "." + joinPoint.getSignature().getName()+ "()");
long startTime = System.currentTimeMillis();
try{
Object result = joinPoint.proceed();
return result;
}catch(Exception e){
logger.warn( joinPoint.getTarget().getClass() + "." + joinPoint.getSignature().getName() + "() invoke error" );
logger.warn("error info ["+e.getMessage()+"]");
}finally{
logger.warn("Ending method : " + joinPoint.getTarget().getClass() + "." + joinPoint.getSignature().getName() + "()");
logger.warn("Method invocation time : " + (System.currentTimeMillis() - startTime) + " ms.");
}
return null;
}
}
spring配置文件:
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<aop:config>
<aop:pointcut id="logger"
expression="execution( public * com.potato..*.*(..)) and !execution( * com.potato.core.aop.GenericLogger.*(..))" />
<aop:aspect id="loggerAspect" ref="genericLoggerBean">
<aop:around pointcut-ref="logger" method="invoke" />
</aop:aspect>
</aop:config>
<bean id="genericLoggerBean"
class="com.potato.core.aop.GenericLoggerBean">
</bean>
</beans>
发表评论
- 浏览: 13821 次

- 详细资料
搜索本博客
我的相册
ttttt
共 7 张
共 7 张
链接
最新评论
-
Java实现按照要求的尺寸对 ...
只能缩小不能放大啊!
-- by likehibernate -
tomcat 6 el表达式的问题
tomcat6 中对5.0中这样的判断 ${not empty command ...
-- by hf200012 -
tomcat 6 el表达式的问题
这个是Tomcat6 的bug:https://issues.apache.or ...
-- by codeutil -
Java实现按照要求的尺寸对 ...
透明的png图片缩放后的图片就不透明了! 怎么解决啊?
-- by form_rr -
[以解决]请教iBatis批量 ...
问题以解决,是因为#oknos#这个地方的#换成$就可以,因为Ibatis认为这 ...
-- by hf200012






评论排行榜