spring boot Performs a specific operation after the container is loaded

  • 2021-01-03 20:55:59
  • OfStack

Sometimes we need to open a thread or a program to do something after the spring boot container has been started and loaded. At this point we need to configure ContextRefreshedEvent events to do what we want to do

1, ApplicationStartup class


public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent>{
  public void onApplicationEvent(ContextRefreshedEvent event)
   {
    // Obtained after the container has been loaded dao Layer to manipulate the database 
    OSSVideoRepository ossVideoRepository = (OSSVideoRepository)event.getApplicationContext().getBean(OSSVideoRepository.class);
    // Gets the configuration in the configuration file after the container has loaded 
    ServerConfig serverConfig = (ServerConfig)event.getApplicationContext().getBean(ServerConfig.class);
    
    ServerFileScanner fileScanner = new ServerFileScanner(
        ossVideoRepository, serverConfig.getScanpath());
    // Start the thread after the container is loaded 
    Thread thread = new Thread(fileScanner);
    thread.start();
   }
}

2, ServerConfig class


@Component
@ConfigurationProperties(prefix = "server")
public class ServerConfig {
  private String aliyunossEndpoint;
  private String aliyunossAccessKeyId;
  private String aliyunossAccessKeySecret;
  private String aliyunossBucketName;
  private String scanpath;

  public String getAliyunossEndpoint() {
    return aliyunossEndpoint;
  }

  public void setAliyunossEndpoint(String aliyunossEndpoint) {
    this.aliyunossEndpoint = aliyunossEndpoint;
  }

  public String getAliyunossAccessKeyId() {
    return aliyunossAccessKeyId;
  }

  public void setAliyunossAccessKeyId(String aliyunossAccessKeyId) {
    this.aliyunossAccessKeyId = aliyunossAccessKeyId;
  }

  public String getAliyunossAccessKeySecret() {
    return aliyunossAccessKeySecret;
  }

  public void setAliyunossAccessKeySecret(String aliyunossAccessKeySecret) {
    this.aliyunossAccessKeySecret = aliyunossAccessKeySecret;
  }

  public String getAliyunossBucketName() {
    return aliyunossBucketName;
  }

  public void setAliyunossBucketName(String aliyunossBucketName) {
    this.aliyunossBucketName = aliyunossBucketName;
  }

  public String getScanpath() {
    return scanpath;
  }

  public void setScanpath(String scanpath) {
    this.scanpath = scanpath;
  }

}

PS: There are also a few events built into spring

1. ContextRefreshedEvent: This event is triggered when the ApplicationContext container is initialized or refreshed.
ContextStartedEvent: This event is triggered when the ApplicationContext container is started using the start() method of the ConfigurableApplicationContext interface.
ContextClosedEvent: This event is triggered when the ApplicationContext container is closed using the close() method of the ConfigurableApplicationContext interface.
ContextStopedEvent: This event is triggered when the ApplicationContext container is stopped using the stop() method of the ConfigurableApplicationContext interface.


Related articles: