日期:2014-05-18 浏览次数:20635 次
package com.zjlolife.action;
import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;
import com.zjlolife.domain.Item;
import com.zjlolife.domain.User;
import com.zjlolife.service.UserService;
public class UserAction {
@Resource
private UserService userService;
private User user;
private Item item;
public User getUser() {
System.out.println("getUser");
return user;
}
public void setUser(User user) {
System.out.println("setUer");
this.user = user;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
public String login() {
User loginUser = userService.login(user);
if(loginUser!=null) {
ServletActionContext.getRequest().getSession().setAttribute("user", loginUser);
return "success";
}
return "login";
}
public String addItem() {
userService.addItem(item);
return "success";
}
}
package com.zjlolife.util;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Aspect
public class LogInterceptor {
@Pointcut("execution (* com.zjlolife.action.UserAction.*(..))")
private void anyMethod() {};
@Pointcut("execution (* com.zjlolife.action.UserAction.*et*(..))")
private void noInterceptorMethod() {};
@Pointcut("anyMethod()&&!noInterceptorMethod()")
private void InterceptorMethod() {};
@Around("InterceptorMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
LoggerUtils.setLogger(LogInterceptor.class);
//Logger logger = LoggerFactory.getLogger(LogInterceptor.class);
System.out.println("intercepyot");
Class className = pjp.getTarget().getClass();
Object[] args = pjp.getArgs();
String methodName = pjp.getSignature().getName();
Object result = null;
try {
result = pjp.proceed();
}
catch(Throwable t) {
//如果出现异常就记录异常日志
LoggerUtils.log(className, args, methodName,t);
throw new Throwable(t);
}
//如果操作正常记录日志
LoggerUtils.log(className, args, methodName);
return result;
}
}