How to use Spring's DebugInterceptor


Publication Date:January 19, 2021



INFOMARTION > How to use Spring's DebugInterceptor

summary

This is about how to use Spring's DebugInterceptor. I understood the concept from the following Spring website, but it was difficult to understand how to do it concretely from the website alone.

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-api

Table of Contents

  1. What is DebugInterceptor?
  2. mounting
  3. summary

1. What is DebugInterceptor?

DebugInterceptor is a library that outputs logs by AOP. Specifically, the following libraries (jar files) are used. Version 5.1.4.

https://mvnrepository.com/artifact/org.springframework/spring-aop/5.1.4.RELEASE

The specific Java file is "org.springframework.aop.interceptor.DebugInterceptor.

To briefly explain AOP, AOP is a technique for injecting processing into instances managed on Spring's DI container. The official name for AOP is Aspect Oriented Programming.

In this case, we would like to inject DebugInterceptor to the instance being generated by @Component.

1-1. implementation environment

I would like to describe the Jar and version used, as there may be some differences depending on the environment.

Version of spring aopspring-aop-5.1.4.RELEASE.jar
Version of spring beansspring-beans-5.1.4.RELEASE.jar
Log output library versionslf4j-api-1.7.25.jar
logback-core-1.2.3.jar

1-2. Work required

The required tasks are as follows

  • Create an instance with @Component
  • Define AOP in applicationContext.xml
  • Change log output settings

2. mounting

Now, we would like to actually implement it.

2-1. Create an instance with @Component

Prepare a class with "@Component" declared as follows. Since we only want to see the AOP, only "return" is executed.

TestAop.java


@Component
public class TestAop{
    public void testMethod() {

        return;

    }

}

2-2. Define AOP in applicationContext.xml

Set up AOP to inject the process. The red will be the part related to the AOP.

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:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
~omission~
    <!-- Spring AOP Testing. -->
    <bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor" />
    <aop:config>
        <aop:advisor advice-ref="debugInterceptor"
            pointcut="execution(* com.example.TestAop.testMethod(..))"  />
    </aop:config>
</beans>

The "pointcut" is written as follows

pointcut="execution(* Package Name.class name.method name(..))"

In the above example, it would be as follows

・Package Name:com.example

・class name:TestAop

・method name:testMethod

If you wish to apply it to all methods, the following will apply.

・pointcut="execution(* com.example.TestAop.*(..))"

2-3. Change log output settings

Since the level of log output listed in DebugInterceptor is "TRACE", change the log level only in DebugInterceptor. The red areas are the ones that need to be set. The other settings can be entered arbitrarily.

logback.xml


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

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern><![CDATA[date:%d{yyyy-MM-dd HH:mm:ss}\tthread:%thread\tX-Track:%X{X-Track}\tlevel:%-5level\tlogger:%-48logger{48}\tmessage:%msg%n]]></pattern>
        </encoder>
    </appender>

    <appender name="APPLICATION_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${app.log.dir:-log}/todo-application.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${app.log.dir:-log}/todo-application-%d{yyyyMMdd}.log</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder>
            <charset>UTF-8</charset>
            <pattern><![CDATA[date:%d{yyyy-MM-dd HH:mm:ss}\tthread:%thread\tX-Track:%X{X-Track}\tlevel:%-5level\tlogger:%-48logger{48}\tmessage:%msg%n]]></pattern>
        </encoder>
    </appender>

    <logger name="org.springframework.aop.interceptor" level="trace">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="APPLICATION_LOG_FILE" />
    </logger>

    <root level="warn">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="APPLICATION_LOG_FILE" />
    </root>

</configuration>

I looked at some other people's blogs when creating this file, and some of them stated that the output should be at log level DEBUG, but in the case of "spring-aop-5.1.4.RELEASE.jar", it seems that the output is not output unless the log level TRACE is used.

If the configuration should be working well, but for some reason the logs are not outputting, try changing the log level to TRACE.

2-4. result

When testMethod of TestAop.java is called with the above settings completed, the following log should be output. The log is long, so I deleted a few lines, but there should be two lines for "Entering (start of processing)" and "Exiting (end of processing)," for a total of four lines of output.

date:2021-01-18 15:26:35	thread:http-nio-8081-exec-3	X-Track:84b8ab0aa6504352a815ce85167a1981	level:TRACE	logger:o.s.aop.interceptor.DebugInterceptor	message:Entering ReflectiveMethodInvocation 
date:2021-01-18 15:26:35	thread:http-nio-8081-exec-3	X-Track:84b8ab0aa6504352a815ce85167a1981	level:TRACE	logger:o.s.aop.interceptor.DebugInterceptor	message:Entering ReflectiveMethodInvocation 
date:2021-01-18 15:26:40	thread:http-nio-8081-exec-3	X-Track:84b8ab0aa6504352a815ce85167a1981	level:TRACE	logger:o.s.aop.interceptor.DebugInterceptor	message:Exiting ReflectiveMethodInvocation 
date:2021-01-18 15:26:40	thread:http-nio-8081-exec-3	X-Track:84b8ab0aa6504352a815ce85167a1981	level:TRACE	logger:o.s.aop.interceptor.DebugInterceptor	message:Exiting ReflectiveMethodInvocation 

3. summary

If you are not familiar with the use of DebugInterceptor, please refer to it.

Thank you for taking the time to read this to the end.




■INFORMATION

Please click here to go to the top page of INFORMATION.


■PROFILE

Please click here to view the profile.


■For inquiries, please contact

For inquiries about the article, please contact us here.