日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

切入业务日志

發(fā)布時間:2023/12/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 切入业务日志 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

創(chuàng)建接口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();?
? ??
}

-----------------------------------------------------------默默無聞的分割線-----------------------------------------------------------

添加業(yè)務(wù)日志DAO接口bizLoggerDao,此處未貼代碼。

添加業(yè)務(wù)日志DAO接口bizLoggerDao實現(xiàn)類bizLoggerDaoImpl,此處未貼代碼。

-----------------------------------------------------------默默無聞的分割線-----------------------------------------------------------

標(biāo)記業(yè)務(wù)接口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);
}

-----------------------------------------------------------默默無聞的分割線-----------------------------------------------------------

添加業(yè)務(wù)接口StudentService.java實現(xiàn)類StudentServiceImpl.java,此處未貼代碼。

-----------------------------------------------------------默默無聞的分割線-----------------------------------------------------------

創(chuàng)建業(yè)務(wù)日志類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();
? ? ? ? //參數(shù)
? ? ? ? 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();
? ? ? ? //執(zhí)行插入
? ? ? ? bizLoggerDao.saveLog(選擇需要保存的數(shù)據(jù)作為參數(shù));
? ? }
? ??
? ? 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>


轉(zhuǎn)載于:https://blog.51cto.com/isiah/1766697

總結(jié)

以上是生活随笔為你收集整理的切入业务日志的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。