基于注解的aop实现用户操作日志管理

来源:互联网 发布:超图软件最新消息 编辑:程序博客网 时间:2024/06/08 10:32

AOP是spring的核心之一。用AOP来实现用户操作日志管理非常合适。
学东西应该先具体再抽象。下面直接上代码

  • pom.xml
<!-- 面向切面的包 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>${spring-version}</version>        </dependency>        <dependency>            <groupId>org.aspectj</groupId>            <artifactId>aspectjrt</artifactId>            <version>1.7.4</version>        </dependency>        <dependency>            <groupId>org.aspectj</groupId>            <artifactId>aspectjweaver</artifactId>            <version>1.7.4</version>        </dependency>
  • SpringServlet.xml
    <!-- aop 切面 -->    <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->    <context:component-scan base-package="com.kylin.aep.aop"></context:component-scan>    <!--  强制使用cglib代理,如果不设置,将默认使用jdk的代理,但是jdk的代理是基于接口的 -->    <aop:aspectj-autoproxy proxy-target-class="true" />
  • ControllerAspect.java
@Aspect@Component/*You may register aspect classes as regular beans in your Spring XML configuration, or autodetect them through classpath scanning - just like any other Spring-managed bean. However, note that the @Aspect annotation is not sufficient for autodetection in the classpath: For that purpose, you need to add a separate @Component annotation (or alternatively a custom stereotype annotation that qualifies, as per the rules of Spring's component scanner).*/public class ControllerAspect {    @Autowired    @Qualifier("SystemManageServiceImpl")    SystemManageService systemManageServiceImpl;    private static final Logger log = Logger.getLogger(ControllerAspect.class);    // 方法执行之前被调用执行    /* 定义切入点        (配置在com.mycomp.myproj.controller包下所有后缀为Controller的类的所有方法在调用之前都会被拦截)       用户的所有操作,最终都是页面请求controller中的方法。       controller中方法执行之前会执行下面的方法,在这个时候写入日志。    */    @Before("execution(* com.mycomp.myproj.controller.*Controller.*(..))")    public void permissionCheck(JoinPoint point) throws Throwable {        // 获取调用的controller的类名        String className = point.getSignature().getDeclaringType().getSimpleName();        // 获取调用的controller类中的方法名        String methodName = point.getSignature().getName();        String operation = className + "." + methodName;        //前端调用的方法和传入的参照都写入log        log.info("控制器" + className + "." + methodName + "入参:" + Arrays.toString(point.getArgs()));        // 获取方法中的参数        Object[] args = point.getArgs();        if (args.length > 0 && args[args.length - 1] instanceof HttpSession) {            //参数列表的最后一个是httpsession            HttpSession session = (HttpSession) args[args.length - 1];            //从session中获取当前登录用户            User user = (User) session.getAttribute("user");            if (user != null) {                //操作日志写入日志表                //后面写入数据库之前会对operation做一个中文转义,毕竟是给用户看的。                systemManageServiceImpl.saveSystemLog(user, operation, 0);            }            else {                log.info("未知用户进行" + operation + "操作!");            }        }    }}

这样,页面每一次请求都会自动记录一次日志。后面写业务代码的时候就不用关心用户操作日志了。
ps:当然,其他的errer和debug信息,该写log还是要写,方便后面查错。

原创粉丝点击