【log】AOP配置日志
2013-04-13 22:35 Loull 阅读(347) 评论(0) 编辑 收藏 举报直接代码
QueryService.java接口
public interface QueryService { TbDiseases getTbDiseasesById(int id) throws ServerException; }
QueryServiceImpl实现
public class QueryServiceImpl implements QueryService { @Autowired private TbDiseaseDao tbDiseaseDao; @Override public TbDiseases getTbDiseasesById(int id) throws ServerException { return tbDiseaseDao.read(id); } }
ILogService.java接口
public interface ILogService { public void log(); public void logArg(JoinPoint point); /** * 有参和返回值的方法 * @param point * @param returnObj */ public void logArgAndReturn(JoinPoint point,Object returnObj); }
LogServiceImpl实现
public class LogServiceImpl implements ILogService { Logger logger = Logger.getLogger(LogServiceImpl.class); @Override public void log() { System.out.println("*************Log*******************"); } @Override public void logArg(JoinPoint point) { //此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象 Object[] args = point.getArgs(); String methodName = point.getSignature().getDeclaringTypeName(); logger.info("Method:" + methodName + " " + point.getSignature().getName()); String params = ""; if (args != null) { for (Object obj : args) { params += obj.toString() + ", "; } } logger.info("param size:" + args.length + " === " + params); } @Override public void logArgAndReturn(JoinPoint point, Object returnObj) { //此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象 logger.info("Result: " + returnObj); } }
配置
<bean id="queryService" class="org.ccnt.med.servimpl.QueryServiceImpl"/> <bean id="logService" class="org.ccnt.med.servimpl.LogServiceImpl"/> <!-- AOP log --> <aop:config> <aop:pointcut expression="execution (* org.ccnt.med.servimpl.QueryServiceImpl.*(..))" id="servicePointcut"/> <aop:aspect id="serviceAspect" ref="logService"> <!-- <aop:before method="log" pointcut-ref="servicePointcut"/>--> <aop:after method="logArg" pointcut-ref="servicePointcut"/> <aop:after-returning method="logArgAndReturn" returning="returnObj" pointcut-ref="servicePointcut"/> </aop:aspect> </aop:config>