spring aop logs operation log and error log

Introduction to AOP

The concept of aop is not unfamiliar, we will briefly introduce several concepts in aop in combination with the following figure. 
write picture description here 
AOP: Abbreviation for Aspect-Oriented Programming 
JoinPoint: The point to be cut in, for example, the addUser method in our figure is a JoinPoint. 
Pointcut: Determines a set of Joinpoints that meet the conditions in the system. 
Aspect: It is our aspect, such as our log class, which contains operation logs, exception logs, etc., then this object is an aspect. 
Advice: How to cut, whether to execute before or after the execution of business logic, etc.

aop implementation

Having said so much, let's combine the above diagrams and noun explanations with the code to talk about their meaning and how to execute them.

environment

write picture description here

business class

A class implements an interface addUser

public interface UserManager {
    public void addUser(String username,String password);
}
public class UserManagerImpl implements UserManager {

    @Override
    public void addUser(String username, String password) {
        int a = 1/0;
        System.out.println("addUser");

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Note: The addUser here is our JoinPoint, which is the cut point, the method we want to cut into.

Aspect

/**
 * 定义aspect,将横切点模块化
 * 
 * @author mengh
 * 
 */

public class SecurityHandler {

    /**
     * 操作日志
     * 
     * @param joinPoint
     */
    public void checkSecurity(JoinPoint joinPoint) {
        System.out.println(joinPoint.getTarget().getClass() + "类"
                + joinPoint.getSignature().getName());

    }

    /**
     * 异常日志
     * 
     * @param joinPoint
     */

    public void doThrowing(JoinPoint jp, Throwable ex) {
        System.out.println(jp.getTarget().getClass() + "类的方法的" + jp.getSignature().getName() + "可以记录程序运行时候抛出的异常信息: " + ex.getMessage());
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

Note: The SecurityHandler here is our aspect, which contains the operations we want to cut into. 
JoinPoint in the parameters of the method contains the attributes of our cut-in method, including information such as class, method name, field name, and so on.

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <!-- 启用acpect对注解的支持 -->
    <!-- <aop:aspectj-autoproxy></aop:aspectj-autoproxy> -->
    <bean id="userManager" class="com.tgb.spring.UserManagerImpl" />
    <bean id="securityHandler" class="com.tgb.spring.SecurityHandler" />
    <aop:config>
        <aop:aspect id="securityAspect" ref="securityHandler">
            <!-- <aop:pointcut id="addAddMethod" expression="execution(* add*(..))"/> -->

            <!-- 操作日志的处理 -->
            <aop:pointcut id="addAddMethod" expression="execution(* com.tgb.spring.*.add*(..)) ||execution(* com.tgb.spring.*.del*(..))"/>
            <aop:before method="checkSecurity" pointcut-ref="addAddMethod"/>
            <aop:after method="checkSecurity" pointcut-ref="addAddMethod"/>

            <!-- 异常日志的处理 -->
            <!-- <aop:pointcut id="addExceptionMethod" expression="execution(* com.tgb.spring.*.*(..)) || args(name)"/> --> 
             <aop:after-throwing method="doThrowing" pointcut="execution(* com.tgb.spring.*.add*(..))" throwing="ex"/> 

        </aop:aspect>
    </aop:config>
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

Note: aop:before in the code is what we call advice. Everyone literally means to tell spring how I want to cut, whether to execute it before the method is executed or after it is executed and so on. 
aop:pointcut is to tell spring the rules we want to cut in. For example, here all the added methods must execute the operation log. 
method: is the method name corresponding to our aspect class.

client

public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserManager userManager = (UserManager)beanFactory.getBean("userManager");
        userManager.addUser("zhangssan", "wangwu");

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

result

write picture description here
The red box is our operation class, which tells when to operate what class and what method. Personally, I feel that the actual meaning of this class is to be larger when testing performance, but the error handling in our blue box makes our program go wrong. , we can also clearly find where our program went wrong, and if we add time, we can find the time of the error. 

But if we don't use it, is it that if our program needs to add a log method, we need to add error handling and operation log to all classes, so if we want to delete this operation log, it doesn't matter If it is used, then we have to delete it bit by bit.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325451488&siteId=291194637