Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create an administrator background operation log monitoring which is completely decoupled from Servlet-api

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to create a complete decoupling and Servlet-api administrator background operation log monitoring, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

In the background of daily development system, it is necessary to monitor the operation of the administrator. If you use the technical system of Spring, it might as well be a good way to use AOP section programming + custom annotations, but there will also be some holes when using this system. For example, this system is completely dependent on the WEB environment, if it is separated from the WEB environment, there will be a situation where ServletRequestAttributes is null. So how to solve this problem.

First, quickly build a running environment for Spring. For more information on jar, please see the following figure:

Javax.servlet

Servlet-api

2.5

Provided

Javax.servlet

Jstl

1.2

Org.springframework

Spring-core

4.3.2.RELEASE

Org.springframework

Spring-context

4.3.2.RELEASE

Org.springframework

Spring-context-support

4.3.2.RELEASE

Org.springframework

Spring-beans

4.3.2.RELEASE

Org.springframework

Spring-aop

4.3.2.RELEASE

Org.springframework

Spring-expression

4.3.2.RELEASE

Org.springframework

Spring-aspects

4.3.2.RELEASE

Org.springframework

Spring-tx

4.3.2.RELEASE

Org.springframework

Spring-web

4.3.2.RELEASE

Org.springframework

Spring-jdbc

4.3.2.RELEASE

Org.springframework

Spring-webmvc

4.3.2.RELEASE

Org.mybatis

Mybatis

3.2.5

Org.mybatis

Mybatis-spring

1.3.2

Mysql

Mysql-connector-java

5.1.38

Com.alibaba

Druid

1.1.10

Com.alibaba

Fastjson

1.2.47

Commons-fileupload

Commons-fileupload

1.3

Com.fasterxml.jackson.core

Jackson-databind

2.9.5

Log4j

Log4j

1.2.16

Junit

Junit

4.12

Test

First, give a simple explanation of the previous development steps, and first develop a custom annotation:

Import java.lang.annotation.ElementType

Import java.lang.annotation.Retention

Import java.lang.annotation.RetentionPolicy

Import java.lang.annotation.Target

/ / use in the method

@ Target ({ElementType.METHOD})

/ / once the class is running,

@ Retention (RetentionPolicy.RUNTIME)

Public @ interface LogAnnotation {

Public String value ()

}

Develop surround notification (general practice, modify below)

Import org.aopalliance.intercept.MethodInterceptor

Import org.aopalliance.intercept.MethodInvocation

Import org.springframework.web.context.request.RequestContextHolder

Import org.springframework.web.context.request.ServletRequestAttributes

Import javax.servlet.http.HttpSession

Import java.lang.reflect.Method

Import java.util.Date

/ *

*

* this is a surround notice

* need to implement

* * /

Public class Around implements MethodInterceptor {

@ Override

Public Object invoke (MethodInvocation mi) throws Throwable {

/ *

* 1. Who-

* 2. When is the new Date ()

* 3. What is it?-method name.

* 4. Whether it is successful or not?-Yes.

* * /

ServletRequestAttributes s = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes ()

/ / get the HttpRequst object, which is strongly coupled to Servlet.

HttpSession session = s.getRequest () .getSession ()

/ / get the data stored in Session

String adminName = (String) session.getAttribute ("admin")

/ / time

Date date = new Date ()

/ / what is it?

Method method = mi.getMethod ()

/ / get the reflection of the class object

LogAnnotation annotation = method.getAnnotation (LogAnnotation.class)

/ / call the method through the instance obtained through reflection

String name = annotation.value ()

/ / whether it is successful

Boolean flag = false

Object proceed = null

Try {

Proceed = mi.proceed ()

Flag=true

} catch (Exception a) {

A.printStackTrace ()

}

/ / No insert operation is done here

System.out.println (did the adminName+ "administrator execute" + name+ "in" + date+ "succeed?" + flag)

Return proceed

}

}

The modification of the above implementation (weak coupling with Servlet) makes use of the DI feature of Spring to realize the automatic injection of operating objects.

Import org.aopalliance.intercept.MethodInterceptor

Import org.aopalliance.intercept.MethodInvocation

Import java.lang.reflect.Method

Import java.util.Date

/ *

* this is a surround notice

* need to implement

* * /

Public class Around implements MethodInterceptor {

/ / declare the Operand as a member variable

Private String adminName

/ / set the public access method to achieve future data injection

Public void setAdminName (String adminName) {

This.adminName = adminName

}

@ Override

Public Object invoke (MethodInvocation mi) throws Throwable {

Date date = new Date ()

/ / what is it?

Method method = mi.getMethod ()

/ / get the reflection of the class object

LogAnnotation annotation = method.getAnnotation (LogAnnotation.class)

/ / call the method through the instance obtained through reflection

String name = annotation.value ()

/ / whether it is successful

Boolean flag = false

Object proceed = null

Try {

Proceed = mi.proceed ()

Flag=true

} catch (Exception a) {

A.printStackTrace ()

}

System.out.println (did the adminName+ "administrator execute" + name+ "in" + date+ "succeed?" + flag)

Return proceed

}

}

The above set the operation data as member variables. In the future, I can add a filter in the controller layer and the business layer to realize the injection of operation data.

3. Use Spring to manage notification classes

Development of filter

Import com.baizhi.aop.Around

Import org.springframework.web.context.WebApplicationContext

Import org.springframework.web.context.support.WebApplicationContextUtils

Import javax.servlet.*

Import javax.servlet.http.HttpServletRequest

Import javax.servlet.http.HttpSession

Import java.io.IOException

Public class LogFilter implements Filter {

@ Override

Public void init (FilterConfig filterConfig) throws ServletException {

}

@ Override

Public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

FilterChain.doFilter (servletRequest, servletResponse)

HttpServletRequest res = (HttpServletRequest) servletRequest

HttpSession session = res.getSession ()

String admin = (String) session.getAttribute ("admin")

ServletContext servletContext = session.getServletContext ()

/ / use this web utility class to get the WebApplicationContext object

WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext (servletContext)

/ / you can get the initialized Bean through the WebApplicationContext class

Around around = (Around) webApplicationContext.getBean ("around")

/ / inject data through set method

Around.setAdminName (admin)

}

@ Override

Public void destroy () {

}

}

5. Finally, it is configured in web.xml.

Log

Com.baizhi.filter.LogFilter

Log

/ *

To get the test done, the advantage of this is that when testing a module, you can only focus on the name of String type, and you don't have to consider the problem of obtaining session and null value of web.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report