Skip to content

@SentryTransaction breaks @Autowired when using SpringBeanJobFactory #2221

@buckett

Description

@buckett

Integration

sentry-spring-boot-starter

Java Version

17

Version

6.3.0

Steps to Reproduce

  • Create a spring boot project using sentry-spring-boot-starter and spring-boot-starter-quartz.
  • Create a simple spring service that has a method that can be called from a quartz job, eg:
    @Component
    public class HelloWorld {
        public String say() {
             return "Hello World"
        }
     }
  • Create a Quartz job that has this service autowired into it and calls the say() method. eg:
    public class HelloWorldJob implements Job {
         @Autowired
         private HelloWorld helloWorld;

         @SentryTransaction(operation = "job")
         public final void execute(JobExecutionContext context) throws JobExecutionException {
             helloWorld.say();
         }
     }
  • Create a job and schedule this to run regularly. The SpringBeanJobFactory used by Spring Boot means that the @Autowired annotation is processed. As long as sentry tracing isn't setup this work correctly.
  • By setting the Spring property sentry.dsn=... the Sentry configuration is enabled and now the HelloWorldJob no longer has its dependencies autowired resulting in a NPE.

When debugging this it was the org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator code that was creating a new instance of the Job (after the dependencies had been autowired into the original Job). The call stack when this was happening was:

applyBeanPostProcessorsAfterInitialization:456, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
initializeBean:1808, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
doCreateBean:620, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBean:542, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBean:378, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createJobInstance:90, SpringBeanJobFactory (org.springframework.scheduling.quartz)
newJob:43, AdaptableJobFactory (org.springframework.scheduling.quartz)
initialize:127, JobRunShell (org.quartz.core)
run:392, QuartzSchedulerThread (org.quartz.core)

Expected Result

The job is correctly initialised with it's dependencies, but also sends a transaction to sentry.

Actual Result

A NPE when accessing the autowired dependency.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions