Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot @PreDestroy annotation not working

I'm new to Spring Boot, and I've got a Spring Boot application that seems to be ignoring the @PreDestroy annotation - when I run from command line or via Eclipse, I'm never seeing the @PreDestroy code being run when application is shutdown (e.g. via ctrl-c)

Code is 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 Processor Config:

@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");
   }

}
like image 469
finbin Avatar asked Apr 13 '17 12:04

finbin


People also ask

Is PostConstruct deprecated?

In jdk9 @PostConstruct and @PreDestroy are in java. xml. ws. annotation which is deprecated and scheduled for removal.

How many times is PostConstruct called?

2. @PostConstruct. Spring calls the methods annotated with @PostConstruct only once, just after the initialization of bean properties.

When @PostConstruct is executed?

The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This method MUST be invoked before the class is put into service. This annotation MUST be supported on all classes that support dependency injection.

Why do we use PostConstruct in Spring?

When we annotate a method in Spring Bean with @PostConstruct annotation, it gets executed after the spring bean is initialized. We can have only one method annotated with @PostConstruct annotation. This annotation is part of Common Annotations API and it's part of JDK module javax.


2 Answers

Spring Boot register shutdown hook during context creation see org.springframework.context.support.AbstractApplicationContext#registerShutdownHookmethod. As result context must be closed on crtl+c combination (if you run your app through java -jar app.jar cmd) and which in turn triggers your @Predestroy method. This works for me.

I don't see any remarks in your code. I can suggest annotating your MessageProcessorImpl bean with @Component annotation instead of a manual @Bean declaration.

like image 77
MeetJoeBlack Avatar answered Oct 23 '22 00:10

MeetJoeBlack


The @PreDestroy does not work when the Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE) is used on the bean since the bean's spawned with this method is not completely managed by the IOC container.

The @PreDestroy works when the Scope(value=ConfigurableBeanFactory.SCOPE_SINGLETON) is used on the bean.

like image 1
Madhusudan N Avatar answered Oct 23 '22 02:10

Madhusudan N