基于Spring aop 和JAVA注解方式添加日志

2 篇文章 0 订阅
2 篇文章 0 订阅
首先需要传入日志记录的具体操作名称,我们可以用java的注解功能来带入参数,代码如下:

/**
 * 类的方法描述注解
 * @author LuoYu
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Log {

    /** 要执行的操作类型比如:add操作 **/
    public String operationType() default "";
  
    /** 要执行的具体操作比如:【添加仓库】 **/
    public String operationName() default "";
  
}

 注解类编写好之后,就要考虑spring我切面的问题目了,首先我们要创建一个切点,也就是需要插入的代码块,代码如下:

/**
 * 通过Spring AOP来添加系统日志
 * @author LuoYu
 */
public class LogAspect extends BaseAction{

    private static final long serialVersionUID = -5063868902693772455L;

    private Log logger = LogFactory.getLog(LogAspect.class);
  
    @SuppressWarnings( { "rawtypes", "unchecked" } )
    public void doSystemLog(JoinPoint point) throws Throwable {
        Object[] param = point.getArgs();
        Method method = null;
        String methodName = point.getSignature().getName();
        if (!(methodName.startsWith("set") || methodName.startsWith("get")||methodName.startsWith("query"))){
            Class targetClass = point.getTarget().getClass();
            method = targetClass.getMethod(methodName, param[0].getClass());
            if (method != null) {
                boolean hasAnnotation = method.isAnnotationPresent(com.tlj.pcxt.common.logaop.Log.class);
                if (hasAnnotation) {
                    com.tlj.pcxt.common.logaop.Log annotation = method.getAnnotation(com.tlj.pcxt.common.logaop.Log.class);
                    String methodDescp = annotation.operationType()+annotation.operationName();
                    if (logger.isDebugEnabled()) {
                        logger.debug("Action method:" + method.getName() + " Description:" + methodDescp);
                    }
                    User appUser=(User) this.getHttpServletRequest().getSession().getAttribute("user");
                    if(appUser!=null){
                        try{
                            com.tlj.pcxt.entity.admin.Log logInfo=new com.tlj.pcxt.entity.admin.Log();
                            logInfo.setIp(this.getHttpServletRequest().getRemoteAddr());
                            logInfo.setSubmitUser(appUser);
                            logInfo.setContent(annotation.operationType()+","+appUser.getUserName()+
                                    "执行【"+annotation.operationName()+"】操作,影响数据的ID集合为["+getID(param[0])+"]");
                            this.logService.save(logInfo);
                        }catch(Exception ex){
                            logger.error(ex.getMessage());
                        }
                    }
                }
            }
        }
    }
  
    /**
     * 通过java反射来从传入的参数object里取出我们需要记录的id,name等属性,
     * 此处我取出的是id
     *@author 罗宇
     *@date 2013-4-11
     *@param obj
     *@return
     *@return String
     */
    public String getID(Object obj){
        if(obj instanceof String){
            return obj.toString();
        }
        PropertyDescriptor pd = null;
        Method method = null;
        String v = "";
        try{
            pd = new PropertyDescriptor("id", obj.getClass());
            method = pd.getReadMethod();
            v = String.valueOf(method.invoke(obj));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return v;
    }
}

 切入代码编写好之后,需要在applicatioContext.xml里配置切入规则,也就是说要在哪些方法执行的时候来切入上面编写的代码:配置如下:

<aop:aspectj-autoproxy/>
    <bean id="logAspect" class="com.tlj.pcxt.common.logaop.LogAspect"/>  
     <aop:config>
        <aop:aspect ref="logAspect">
            <aop:pointcut id="logPointCut" expression="
                   (execution(* com.tlj.pcxt.service.*.*Impl.add*(..)))
                or (execution(* com.tlj.pcxt.service.*.*Impl.update*(..)))
                or (execution(* com.tlj.pcxt.service.*.*Impl.delete*(..)))
            "/>
            <aop:after pointcut-ref="logPointCut" method="doSystemLog"/>
        </aop:aspect>
    </aop:config>

在此我配置的时在方法执行之后插入代码块

<aop:after pointcut-ref="logPointCut" method="doSystemLog"/>

并且是在所有以add,update,delete开头的方法才执行,其余的方法将不再匹配。

调用方法如下,

  @Log(operationType="add操作:",operationName="添加仓库房间")
    public void addWareHouseRoom(WareHouseRoom wareHouseRoom) throws ServiceException {
        try{
            this.getWareHouseRoomDao().save(wareHouseRoom);
        }catch (Exception e) {
            throw new ServiceException(e);
        }
    }

是在方法头前添加上面自定义的@Log注解,传入相关日志信息

另外,在LogAspect的doSystemLog方法里的

Object[] param = point.getArgs();

就是取出所匹配方法传入的参数,我们记录日志所需要的相关参数就是从这个对象里取出来的,并且在该方法下面的代码会检查所匹配的方法是否有注解@log,如果没有,会直接跳出该方法,不做任何处理.

如果是拦截action,包路径不能具体到方法,如果想获取参数值,可以

Object[] args = point.getArgs();// 获取传入参数
  System.out.println("目标参数列表:");

  if (args != null) {
   for (Object obj : args) {
    System.out.println(obj + ",");
   }
   System.out.println("执行结果是:" + returnObj);
  }  

且配置文件修改为同时添加cglib-nodep.jar:

<aop:aspectj-autoproxy/>
    <bean id="logAspect" class="edu.cqwu.util.LogAspect">
     <property name="sysLogDao" ref="sysLogDao"/>
    </bean>
    <aop:config proxy-target-class="true">
        <aop:aspect ref="logAspect">
            <aop:pointcut id="logPointCut" expression="
                (execution(* edu.cqwu.util.action.*.*(..)))
                or (execution(* edu.cqwu.wx.action.*.*(..)))
                or (execution(* edu.cqwu.yq.action.*.*(..)))
            "/>
            <aop:after-returning returning="returnObj" pointcut-ref="logPointCut" method="doSystemLog"/>
        </aop:aspect>
    </aop:config>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值