I want use annotation and aspect to make an logging function:
annotation:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LogInfo {
String operationName();
}
Aspect:
@Aspect
public class LogAspect {
@Resource
private LogDao logDao;
@Pointcut(value="@annotation(logInfo)",argNames="l ogInfo")
public void methodWithLogInfo(LogInfo logInfo) {
}
@Around("methodWithLogInfo(logInfo)")
public Object writeLog(ProceedingJoinPoint point,LogInfo logInfo) throws Throwable {
HttpServletRequest request = null;
Object[] args = point.getArgs();
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof HttpServletRequest) {
request = (HttpServletRequest) args[i];
}
}
CmsUser currUser = CmsUtils.getUser(request);
Set<CmsRole> roles=currUser.getRoles();
String usertype="";
for(CmsRole role:roles){
usertype=role.getName()+" ";
}
Sys_log log=new Sys_log();
log.setHandle(logInfo.operationName());
log.setIntime(new Date());
log.setUserid(currUser.getId());
log.setUsertype(usertype);
logDao.save(log);
return point.proceed();
}
}
using palce:
@RequestMapping(value = "/login.do", method = RequestMethod.POST)
@LogInfo(operationName = "用户登录")
public String submit(String username, String password, String captcha,
String processUrl, String returnUrl, String message,
HttpServletRequest request, HttpServletResponse response,
ModelMap model) {
}
application-context.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:unis="http://www.springframework.org/schema/unis" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schem...ng-jee-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-lazy-init="true">
<aop:aspectj-autoproxy/>
</beans>
when I invoke public String submit() method ,nothing happened,some one help me to applied aspect to the function!tks!


Reply With Quote

