@PostConstruct

Put @PostConstruct on a method that we want to run on startup just after all the beans have been wired.

Typically we open a resource like network connections to a remote resource (cache, queue, database etc).

@Singleton
public class CoffeeMaker {

  ...

  @PostConstruct
  public onStartup() {
    // connect to remote resource ...
    ...
  }
  ...

@PreDestory

Put @PreDestory on a method that we want to run on shutdown.

Typically we want this method to close resources.

@Singleton
public class CoffeeMaker {

  ...

  @PreDestroy
  public onShutdown() {
    // close resources
    ...
  }
  ...