新建maven项目,pom.xml中jar包如下:
jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
web.xml:
applicationContext.xml:
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
设置日志注解:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface BusinessOption {
String moduleName() default "";
String menuName() default "";
}
HelloWorld:
package cn.jzj;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller("/Hello")
@RequestMapping("/Hello")
@Configuration
public class Helloworld {
@Autowired
private UserMapper userMapper;
@BusinessOption(moduleName="的说法都是", menuName="谁不是")
@RequestMapping(value="/hello")
public String helloWorld() {
return "success";
}
}
UserMapper.java:
import java.util.List;
public interface UserMapper {
void save(User user);
void update(User user);
void delete(int id);
User findById(int id);
List
}
UserMapper.xml:
insert into user_log(name,desc1)
values(#{name},#{desc})
update user_log
set name = #{name},
desc1 = #{desc},
where id = #{id}
delete from user_log
where id = #{id}
LogIntercept:
package cn.jzj;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import cn.jzj.UserMapper;
@Component
public class LogInterceptor extends HandlerInterceptorAdapter {
@Autowired
private UserMapper userMapper;
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
super.afterCompletion(request, response, handler, ex);
}
@Override
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// TODO Auto-generated method stub
super.afterConcurrentHandlingStarted(request, response, handler);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
HandlerMethod hm = (HandlerMethod) handler;//将其强转过来
BusinessOption businessOption = hm.getMethodAnnotation(BusinessOption.class);
String s=String.format("操作了【%s】模块下【%s】功能", businessOption.moduleName(), businessOption.menuName());
User user=new User();
user.setName("jzj");
user.setDesc(s);
userMapper.save(user);
super.postHandle(request, response, handler, modelAndView);
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// TODO Auto-generated method stub
return super.preHandle(request, response, handler);
}
}