AOP日志&&Spring访问静态图片

配置文件:application_context.xml

     <!-- aop注解   proxy-target-class="true" 切入的类可以是代理类  -->    
       <aop:aspectj-autoproxy proxy-target-class="true"/>

     <!--切面-->
     <bean id="systemLogAspect" class="com.lty.common.log.SystemLogAspect"></bean>

 

java 代码:

package com.lty.common.log;


import javax.annotation.Resource;

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.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.lty.common.redis.RedisHelper;
import com.lty.common.util.Constants;
import com.lty.thailand.entity.BaseUserLog;
import com.lty.thailand.service.impl.BaseUserLogService; 

/**

 * @desc 切点类
 */

@Aspect
@Component
public class SystemLogAspect {

    //注入Service用于把日志保存数据库 
    @Resource  //这里我用resource注解,一般用的是@Autowired,他们的区别如有时间我会在后面的博客中来写
    private BaseUserLogService baseUserLogService; 
//   
    private  static  final Logger logger = LoggerFactory.getLogger(SystemLogAspect. class); 

    @Pointcut("execution (* com.lty.travel.controller.ManagerController.*(..))") 
    public void controllerPointcut() {} 
   
      @Around("controllerPointcut()")  
        public Object controllerAround(ProceedingJoinPoint joinPoint)  throws Throwable {         // 执行的类      
          String exeType = null;         // 执行的方法        
          String exeMethod = null;
          Object retObj = null ;
           long start = System.currentTimeMillis();      
             try {             // 获取接口的请求参数         
                MethodSignature signature = (MethodSignature) joinPoint.getSignature();            
                exeType = signature.getDeclaringType().getCanonicalName();            
                exeMethod = signature.getMethod().getName();
                BaseUserLog baseUserLog = null ;
                String sqlId = null ;
                boolean needInsert  = false;
                if (joinPoint.getArgs() !=  null && joinPoint.getArgs().length > 0) { 
                    for ( int i = 0; i < joinPoint.getArgs().length; i++) {
                     baseUserLog = new BaseUserLog();
                     JSONObject requestParam = JSONObject.parseObject((String)joinPoint.getArgs()[i]);
                     String userId = requestParam.getString("userId");
                     if(!StringUtils.isEmpty(userId)){
                      needInsert = true ;                   
                      baseUserLog.setUserId(userId);
                      sqlId = requestParam.getString("sqlId");
                      baseUserLog.setOperType(sqlId);
                       }
                   } 
               }
                try{
                    retObj = joinPoint.proceed();                  
                    if("getCertification".equals(sqlId)||"getCertificationNew".equals(sqlId)){
                     JSONObject requestParam = JSONObject.parseObject((String)retObj);
                     String userIdss = requestParam.getString("data");
                     JSONObject requestParam22 = JSONObject.parseObject(userIdss);
                     baseUserLog.setUserId(requestParam22.getString("userId"));
                    }                   
                    baseUserLog.setStatus(0);
                 }catch(Exception e){
                  baseUserLog.setStatus(1);
                 }
               
                if(needInsert){
                 baseUserLog.setExecuteTime(System.currentTimeMillis() - start);
                  if(!"quitApp".equals(sqlId)){
                   RedisHelper.setWithExpireTime(Constants.USER_KEY+""+baseUserLog.getUserId(), baseUserLog.getUserId(), 30*60);
                     }
                   baseUserLogService.insertBaseUserLog(baseUserLog);
                 }
                } catch (Throwable ex) {                 
                } finally {          
                 logger.info("[{}.{} end,cost {} ms]", new Object[] {   exeType, exeMethod, (System.currentTimeMillis() - start)           
                   });        
               }                    
               return retObj;   
        }
 }

 

访问静态图片

配置文件:application_context.xml

 <mvc:default-servlet-handler/> 

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
 <property name="prefix" value="/WEB-INF/jsp/"/> 
    <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类逻辑  -> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 
 </bean>

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值