Home > other >  Springboot 2.7.1 error: cannot find symbol @Lazy
Springboot 2.7.1 error: cannot find symbol @Lazy

Time:07-15

I'm trying to fix a circular dependency in a legacy project.

    @Service
    @Slf4j
    public class FlowpathSubscriber {

    @Autowired
    @Qualifier("pubSubTemplate")
    private PubSubTemplate pubSubTemplate;

    @Autowired
    private FlowpathBigQueryProcessor flowpathBigQueryProcessor;

    @Value("${subscription}")
    private String subscription;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    @Lazy
    private SlackNotificationService slackNotificationService;

I added @Lazy to SlackNotificationService as it was causing a circular dependency issue. However adding @Lazy causes an error during Gradle compilation:

error: cannot find symbol @Lazy

CodePudding user response:

Add:

import org.springframework.context.annotation.Lazy;

CodePudding user response:

Do 2 things:

  1. don't forget to import Lazy i.e. import org.springframework.context.annotation.Lazy;
  2. You need to put @Lazy on SlackNotificationService bean(component) as well.

See this: https://www.baeldung.com/spring-lazy-annotation#2-with-autowired

  • Related