admin管理员组

文章数量:1311051

I have 2 applications each packaged as WAR. Let's call the applications as:

  • WAR 1: demo
  • WAR 2: interceptor

My goal is for the interceptor application to intercept and log a message whenever a specific method in the demo application is about to be executed (@Before advice) and after it was executed (@After advice).

demo app


MyController.java

package com.fredan.demo.controller;

import .springframework.beans.factory.annotation.Autowired;
import .springframework.http.ResponseEntity;
import .springframework.web.bind.annotation.GetMapping;
import .springframework.web.bind.annotation.RestController;
import com.fredan.demo.dto.StatusDto;
import com.fredan.demo.service.MyService;

@RestController
public class MyController {
    @Autowired
    private MyService myService;

    @GetMapping("/counter")
    public ResponseEntity<StatusDto> executeCounter() {
        myService.executeCounter();
        StatusDto dto = new StatusDto();
        dto.setStatus("success");
        return ResponseEntity.ok(dto);
    }
}

MyServiceImpl.java

package com.fredan.demo.service.impl;

import .springframework.stereotype.Service;
import com.fredan.demo.service.MyService;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class MyServiceImpl implements MyService {
    @Override
    public void executeCounter() {
        log.info("=============================== inside MyServiceImpl.executeCounter - Start =============================");
        for (int counter = 1; counter <= 10; counter++) {
            log.info("Current number: {}", counter);
        }
        log.info("=============================== inside MyServiceImpl.executeCounter - End =============================");
    }
}

interceptor app


Application.java

package com.fredan.interceptor;

import java.lang.instrument.Instrumentation;
import .springframework.boot.SpringApplication;
import .springframework.boot.autoconfigure.SpringBootApplication;
import .springframework.boot.web.support.SpringBootServletInitializer;
import .springframework.instrument.InstrumentationSavingAgent;
import net.bytebuddy.agent.ByteBuddyAgent;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        Instrumentation instrumentation = ByteBuddyAgent.install();
        InstrumentationSavingAgent.premain("", instrumentation);
        SpringApplication.run(Application.class, args);
    }
}

MyServiceAspect.java

package com.fredan.interceptor.aspect;

import .aspectj.lang.JoinPoint;
import .aspectj.lang.annotation.After;
import .aspectj.lang.annotation.Aspect;
import .aspectj.lang.annotation.Before;
import .aspectj.lang.annotation.Pointcut;
import lombok.extern.slf4j.Slf4j;

@Aspect
@Slf4j
public class MyServiceAspect {
    @Pointcut("execution(public void com.fredan.demo.service.impl.MyServiceImpl.executeCounter())")
    public void executeCounterPointcut() {
        // this is a method that defines a pointcut of an aspect
    }

    @Before("executeCounterPointcut()")
    public void beforeMethod(JoinPoint jp) {
        log.info("Executing MyServiceAspect.beforeMethod() BEFORE calling " + jp.getSignature().getDeclaringTypeName() + "." + jp.getSignature().getName());
    }

    @After("executeCounterPointcut()")
    public void afterMethod(JoinPoint jp) {
        log.info("Executing MyServiceAspect.afterMethod() AFTER calling " + jp.getSignature().getDeclaringTypeName() + "." + jp.getSignature().getName());
    }
}

AopConfig.java

package com.fredan.interceptor.config;

import .springframework.context.annotation.Configuration;
import .springframework.context.annotation.EnableLoadTimeWeaving;
import .springframework.context.annotation.aspectj.EnableSpringConfigured;

@Configuration
@EnableSpringConfigured
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
public class AopConfig {}

src/main/resources/META-INF/aop.xml

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" ".dtd">
<aspectj>
    <aspects>
        <aspect name="com.fredan.interceptor.aspect.MyServiceAspect" />

        <weaver options="-verbose -debug -showWeaveInfo">
            <include within="com.fredan.interceptor.aspect..*" />
            <dump within="com.fredan.interceptor.aspect..*" beforeandafter="true" />

            <include within="com.fredan.demo.service..*" />
            <dump within="com.fredan.demo.service..*" beforeandafter="true" />
        </weaver>
    </aspects>
</aspectj>

pom.xml

<project xmlns=".0.0"
    xmlns:xsi=";
    xsi:schemaLocation=".0.0 .0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.fredan.interceptor</groupId>
        <artifactId>interceptor-parent</artifactId>
        <version>${revision}</version>
    </parent>

    <artifactId>interceptor</artifactId>
    <packaging>war</packaging>
    <name>Interceptor WAR</name>

    <dependencies>
        <dependency>
            <groupId>com.fredan.demo</groupId>
            <artifactId>demo</artifactId>
            <classifier>classes</classifier>
        </dependency>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
        </dependency>
        <dependency>
            <groupId>.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>
        <dependency>
            <groupId>.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy-agent</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

I am NOT USING javaagent for the JVM since I followed this code how-to-use-load-time-weaving-without-javaagent

The issue right now is that no aspects are woven as I'm not seeing "debug weaving" on the log. Some lines in the log below are omitted for brevity.

2025-02-01 16:17:41 [ INFO] my-interceptor - DefaultContextLoadTimeWeaver:82 - Found Spring's JVM agent for instrumentation
[AppClassLoader@764c12b6] info AspectJ Weaver Version 1.9.22.1 built on Friday May 10, 2024 at 23:23:16 PDT
[AppClassLoader@764c12b6] info register classloader sun.misc.Launcher$AppClassLoader@764c12b6
[AppClassLoader@764c12b6] info using configuration /C:/projects/interceptor-parent/interceptor/target/classes/META-INF/aop.xml
[AppClassLoader@764c12b6] info using configuration file:/C:/Users/langfred/.m2/repository//springframework/spring-aspects/4.3.25.RELEASE/spring-aspects-4.3.25.RELEASE.jar!/META-INF/aop.xml
[AppClassLoader@764c12b6] info register aspect com.fredan.interceptor.aspect.MyServiceAspect
[AppClassLoader@764c12b6] info register aspect .springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect
[AppClassLoader@764c12b6] info register aspect .springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect
[AppClassLoader@764c12b6] info register aspect .springframework.transaction.aspectj.AnnotationTransactionAspect
[AppClassLoader@764c12b6] error can't determine superclass of missing type .springframework.transaction.interceptor.TransactionAspectSupport
 [Xlint:cantFindType]
[AppClassLoader@764c12b6] info register aspect .springframework.transaction.aspectj.JtaAnnotationTransactionAspect
[AppClassLoader@764c12b6] info deactivating aspect '.springframework.transaction.aspectj.JtaAnnotationTransactionAspect' as it requires type 'javax.transaction.Transactional' which cannot be found on the classpath
[AppClassLoader@764c12b6] info register aspect .springframework.cache.aspectj.AnnotationCacheAspect
[AppClassLoader@764c12b6] info register aspect .springframework.cache.aspectj.JCacheCacheAspect
[AppClassLoader@764c12b6] info deactivating aspect '.springframework.cache.aspectj.JCacheCacheAspect' as it requires type '.springframework.cache.jcache.interceptor.JCacheAspectSupport' which cannot be found on the classpath
[AppClassLoader@764c12b6] info deactivating aspect '.springframework.cache.aspectj.JCacheCacheAspect' as it requires type 'javax.cache.annotation.CacheResult' which cannot be found on the classpath
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration$$FastClassBySpringCGLIB$$a7cec5b4'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration$$EnhancerBySpringCGLIB$$1dfdd984$$FastClassBySpringCGLIB$$be80dce7'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration$$FastClassBySpringCGLIB$$28b34ea5'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration$$EnhancerBySpringCGLIB$$62e6f395$$FastClassBySpringCGLIB$$367044a'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$$FastClassBySpringCGLIB$$ee97f15b'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$$EnhancerBySpringCGLIB$$92a5cf0b$$FastClassBySpringCGLIB$$49757e60'
[AppClassLoader@764c12b6] debug not weaving 'com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver'
[AppClassLoader@764c12b6] debug not weaving 'com.fasterxml.jackson.core.Version'
[AppClassLoader@764c12b6] debug not weaving '.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement'
...
[AppClassLoader@764c12b6] debug not weaving '.springframework.beans.ExtendedBeanInfo$PropertyDescriptorComparator'
[AppClassLoader@764c12b6] debug not weaving '.springframework.beans.PropertyDescriptorUtils'
[AppClassLoader@764c12b6] debug not weaving '.springframework.beans.ExtendedBeanInfo$1'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.jackson.JsonComponent'
[AppClassLoader@764c12b6] debug not weaving 'com.fasterxml.jackson.databind.ser.FilterProvider'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration$$FastClassBySpringCGLIB$$596316ac'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration$$EnhancerBySpringCGLIB$$b09a317c$$FastClassBySpringCGLIB$$89c759cd'
...
[AppClassLoader@764c12b6] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified
[AppClassLoader@764c12b6] debug cannot weave 'javax.validation.ConstraintViolation'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.web.NonRecursivePropertyPlaceholderHelper'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration$$FastClassBySpringCGLIB$$7b26c9e9'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration$$EnhancerBySpringCGLIB$$9b4e2359$$FastClassBySpringCGLIB$$5caedacf'
[AppClassLoader@764c12b6] debug not weaving '.springframework.context.support.MessageSourceAccessor'
[AppClassLoader@764c12b6] debug not weaving '.springframework.http.ResponseEntity'
[AppClassLoader@764c12b6] debug not weaving '.springframework.http.HttpEntity'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.bind.annotation.ResponseBody'
[AppClassLoader@764c12b6] debug not weaving 'com.sun.proxy.$Proxy75'
...
2025-02-01 16:17:41 [ INFO] my-interceptor - RequestMappingHandlerAdapter:557 - Looking for @ControllerAdvice: .springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6236eb5f: startup date [Sat Feb 01 16:17:40 SGT 2025]; root of context hierarchy
...
[AppClassLoader@764c12b6] debug not weaving 'com.sun.proxy.$Proxy77'
...
2025-02-01 16:17:42 [ INFO] my-interceptor - RequestMappingHandlerMapping:544 - Mapped "{[/error]}" onto public .springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> .springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistration'
2025-02-01 16:17:42 [ INFO] my-interceptor - RequestMappingHandlerMapping:544 - Mapped "{[/error],produces=[text/html]}" onto public .springframework.web.servlet.ModelAndView .springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.resource.ResourceTransformerChain'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.resource.PathResourceResolver'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.resource.AbstractResourceResolver'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.resource.ResourceResolver'
[AppClassLoader@764c12b6] debug not weaving '.springframework.http.converter.ResourceRegionHttpMessageConverter'
2025-02-01 16:17:42 [ INFO] my-interceptor - SimpleUrlHandlerMapping:364 - Mapped URL path [/webjars/**] onto handler of type [class .springframework.web.servlet.resource.ResourceHttpRequestHandler]
2025-02-01 16:17:42 [ INFO] my-interceptor - SimpleUrlHandlerMapping:364 - Mapped URL path [/**] onto handler of type [class .springframework.web.servlet.resource.ResourceHttpRequestHandler]
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.util.UriComponentsBuilder'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.handler.HandlerExceptionResolverComposite'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.config.annotation.UrlBasedViewResolverRegistration'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.config.annotation.ViewResolverRegistry$GroovyMarkupRegistration'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.config.annotation.ViewResolverRegistry$FreeMarkerRegistration'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.config.annotation.ViewResolverRegistry$ScriptRegistration'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.config.annotation.ViewResolverRegistry$TilesRegistration'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.config.annotation.ViewResolverRegistry$VelocityRegistration'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.view.ViewResolverComposite'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter$FaviconConfiguration$$FastClassBySpringCGLIB$$6a2f79af'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter$FaviconConfiguration$$EnhancerBySpringCGLIB$$8d23dddf$$FastClassBySpringCGLIB$$68223559'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.servlet.resource.ResourceTransformer'
2025-02-01 16:17:42 [ INFO] my-interceptor - SimpleUrlHandlerMapping:364 - Mapped URL path [/**/favicon.ico] onto handler of type [class .springframework.web.servlet.resource.ResourceHttpRequestHandler]
...
[AppClassLoader@764c12b6] debug not weaving 'com.sun.proxy.$Proxy78'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.multipart.MultipartHttpServletRequest'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.multipart.MultipartRequest'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.web.client.RestTemplateCustomizer'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.web.WebClientAutoConfiguration$RestTemplateConfiguration$$FastClassBySpringCGLIB$$a7c76372'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.autoconfigure.web.WebClientAutoConfiguration$RestTemplateConfiguration$$EnhancerBySpringCGLIB$$5c362902$$FastClassBySpringCGLIB$$875bcac1'
[AppClassLoader@764c12b6] debug not weaving '.springframework.http.client.ClientHttpRequestFactory'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.client.ResponseErrorHandler'
[AppClassLoader@764c12b6] debug not weaving '.springframework.http.client.ClientHttpRequestInterceptor'
[AppClassLoader@764c12b6] debug not weaving '.springframework.web.util.UriTemplateHandler'
[AppClassLoader@764c12b6] debug not weaving '.springframework.http.client.support.BasicAuthorizationInterceptor'
[AppClassLoader@764c12b6] debug not weaving '.springframework.aop.scope.ScopedObject'
[AppClassLoader@764c12b6] debug not weaving '.springframework.aop.RawTargetAccess'
[AppClassLoader@764c12b6] debug not weaving '.springframework.context.event.EventListenerMethodProcessor$1'
[AppClassLoader@764c12b6] debug not weaving '.springframework.context.event.EventListener'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.logging.LoggerConfiguration'
2025-02-01 16:17:42 [ INFO] my-interceptor - AnnotationMBeanExporter:431 - Registering beans for JMX exposure on startup
[AppClassLoader@764c12b6] debug not weaving '.springframework.jmx.export.MBeanExporter$2'
[AppClassLoader@764c12b6] debug not weaving '.springframework.jmx.export.MBeanExporter$1'
[AppClassLoader@764c12b6] debug not weaving '.springframework.jmx.export.annotation.ManagedResource'
[AppClassLoader@764c12b6] debug not weaving '.springframework.context.support.DefaultLifecycleProcessor'
[AppClassLoader@764c12b6] debug not weaving '.springframework.context.support.LiveBeansView'
[AppClassLoader@764c12b6] debug not weaving '.springframework.context.support.LiveBeansViewMBean'
2025-02-01 16:17:42 [ INFO] my-interceptor - Http11NioProtocol:180 - Starting ProtocolHandler ["http-nio-8082"]
2025-02-01 16:17:42 [ INFO] my-interceptor - NioSelectorPool:180 - Using a shared selector for servlet write/read
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.util.NioBlockingSelector'
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.util.NioBlockingSelector$BlockPoller'
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.util.collections.SynchronizedQueue'
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.util.threads.TaskQueue'
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.util.threads.TaskThreadFactory'
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.util.threads.TaskThread'
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.util.threads.ThreadPoolExecutor'
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.util.threads.StopPooledThreadException'
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.util.threads.ThreadPoolExecutor$RejectHandler'
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.util.threads.TaskThread$WrappingRunnable'
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.util.threads.Constants'
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.util.NioEndpoint$NioSocketWrapper'
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.util.AbstractEndpoint$Acceptor$AcceptorState'
[AppClassLoader@764c12b6] debug not weaving '.apache.catalina.ContainerServlet'
[AppClassLoader@764c12b6] debug cannot weave 'javax.servlet.annotation.MultipartConfig'
[AppClassLoader@764c12b6] debug cannot weave 'javax.servlet.SingleThreadModel'
[AppClassLoader@764c12b6] debug not weaving '.apache.catalina.ContainerEvent'
2025-02-01 16:17:42 [ INFO] my-interceptor - TomcatEmbeddedServletContainer:210 - Tomcat started on port(s): 8082 (http)
[AppClassLoader@764c12b6] debug not weaving '.springframework.util.ConcurrentReferenceHashMap$EntrySet'
[AppClassLoader@764c12b6] debug not weaving '.springframework.util.ConcurrentReferenceHashMap$EntryIterator'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.ApplicationRunner'
[AppClassLoader@764c12b6] debug not weaving '.springframework.boot.CommandLineRunner'
[AppClassLoader@764c12b6] debug not weaving '.springframework.util.StopWatch$TaskInfo'
2025-02-01 16:17:42 [ INFO] my-interceptor - Application:57 - Started Application in 1.474 seconds (JVM running for 2.456)
[AppClassLoader@764c12b6] debug not weaving '.apache.catalina.webresources.Cache$EvictionOrder'
[AppClassLoader@764c12b6] debug not weaving '.apache.tomcat.PeriodicEventListener'

EDIT (2025-02-03): The demo app is actually a stand in for an application I'm trying to debug. We have a source code of the said app but unfortunately, it's not equal to the deployed one. So I'm trying to debug it using load-time weaving instead.

本文标签: