切入业务日志-创新互联
创建接口BizAnnotation.java
创新互联建站-专业网站定制、快速模板网站建设、高性价比辽阳县网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式辽阳县网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖辽阳县地区。费用合理售后完善,十多年实体公司更值得信赖。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
@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
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
文章题目:切入业务日志-创新互联
分享链接:http://scyanting.com/article/peioe.html