温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

切入业务日志

发布时间:2020-06-28 23:15:38 来源:网络 阅读:485 作者:isiah_zhou 栏目:开发技术

创建接口BizAnnotation.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)  
@Target({ElementType.METHOD})
public @interface BizAnnotation {

    //操作模块
    String moduleName();  
    //操作
    String option(); 
    
}

-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------

添加业务日志DAO接口bizLoggerDao,此处未贴代码。

添加业务日志DAO接口bizLoggerDao实现类bizLoggerDaoImpl,此处未贴代码。

-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------

标记业务接口StudentService.java

import java.util.List;
import com.shenzhen.management.pojo.Student;
public interface StudentService {
public List<Student> getAllStudents();
@BizAnnotation(moduleName="Student Management",option="Add Student")
public void addStudent(Student student);
}

-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------

添加业务接口StudentService.java实现类StudentServiceImpl.java,此处未贴代码。

-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------

创建业务日志类BizLogger.java

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import com.opensymphony.xwork2.ActionContext;
import com.shenzhen.management.dao.BizLoggerDao;
import com.shenzhen.management.service.BizAnnotation;

public class BizLogger {


private BizLoggerDao bizLoggerDao;


        public void log(JoinPoint joinPoint, Object returnObj) {
        
        //方法名
        String methodName = joinPoint.getSignature().getName();
        //参数
        Object[] parameters = joinPoint.getArgs();
        //返回值
        Object returnValue = returnObj;
        //获取模块,操作
        Method method = ((MethodSignature)joinPoint.getSignature()).getMethod();
        BizAnnotation bizAnnotation = method.getAnnotation(BizAnnotation.class);
        String moduleName = bizAnnotation.moduleName();
        String option = bizAnnotation.option();  
        //获取用户ID
        String userId = getUserId();
        //获取用户IP
        String ip = getIP();
        //执行插入
        bizLoggerDao.saveLog(选择需要保存的数据作为参数);
    }
    
    public String getTime()
    {
    Date now = new Date(); 
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    String time = dateFormat.format( now ); 
    return time;
    }
    
    public String getUserId()
    {
    Map session =  ActionContext.getContext().getSession(); 
    String userId = (String)session.get("userId");
    return userId;
    }
    
    public String getIP() { 
    
    HttpServletRequest request =  org.apache.struts2.ServletActionContext.getRequest();
    
        String ip = request.getHeader("x-forwarded-for"); 
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
            ip = request.getHeader("Proxy-Client-IP"); 
        } 
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
            ip = request.getHeader("WL-Proxy-Client-IP"); 
        } 
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
            ip = request.getRemoteAddr(); 
        } 
        return ip; 
    }


public BizLoggerDao getBizLoggerDao() {
return bizLoggerDao;
}


public void setBizLoggerDao(BizLoggerDao bizLoggerDao) {
this.bizLoggerDao = bizLoggerDao;

}

-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------

配置applicationContext.xml

    <bean id = "bizLoggerDao" class="com.shenzhen.management.dao.impl.BizLoggerDaoImpl">
      <property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
    </bean>
    <bean id="bizLogger" class="com.shenzhen.management.util.log.BizLogger">
      <property name="bizLoggerDao" ref="bizLoggerDao" />
    </bean>
<aop:config>
 <aop:pointcut id="bizLogPointcut" expression="execution(* com.shenzhen.management.service.*.*(..))"/>
      <aop:aspect id="bizLogAspect" ref="bizLogger">
        <aop:after-returning method="log" returning="returnObj" pointcut-ref="bizLogPointcut"/>
      </aop:aspect>
    </aop:config>


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI