Spring Boot annotation @PreDestroy not working

I'm new to Spring Boot, and I have a Spring boot application that seems to ignore the @PreDestroy annotation - when I run from the command line or through Eclipse, I never see @PreDestroy executed when the application terminates (for example, using ctrl-c)

Code below ...

Application.java:

@SpringBootApplication
public class Application {

    @Autowired
    private MessageProcessor messageProcessor;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @PostConstruct
    public void init() {    
        messageProcessor.run();
    }

}

Message Server Configuration:

@Configuration
public class MessageProcessorConfiguration {

    @Bean
    public MessageProcessor messageProcessor() {
        return new MessageProcessorImpl();
    }
}

Message Processor:

public class MessageProcessorImpl implements MessageProcessor {

    @Override
    public void run() {

        while (isActive()) {
        }
    }


   @PreDestroy
   public void shutdown() { 
       System.out.println("MessageProcessorImpl - shutting down");
   }

}
+4
source share
1 answer

Spring . org.springframework.context.support.AbstractApplicationContext#registerShutdownHook. crtl + c ( java -jar app.jar cmd), , , @Predestroy. .

. MessageProcessorImpl bean @Component

+1

Source: https://habr.com/ru/post/1674749/


All Articles