Sometimes we need to open some threads or programs to do something after the spring boot container is started and loaded. At this time, we need to configure the ContextRefreshedEvent event to achieve what we want to do
1. ApplicationStartup class
public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent>{ public void onApplicationEvent(ContextRefreshedEvent event) { //After the container is loaded, get the dao layer to operate the database OSSVideoRepository ossVideoRepository = (OSSVideoRepository) event.getApplicationContext().getBean(OSSVideoRepository.class); //After the container is loaded, getConfig in the configuration file 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 setAliyunossBucketName(String aliyunossBucketName) { this.aliyunossBucketName = aliyunossBucketName; } public String getScanpath() { return scanpath; } public void setScanpath(String scanpath) { this.scanpath = scanpath; }}PS: There are also some events built into spring
1. ContextRefreshedEvent: This event is triggered when the ApplicationContext container is initialized or refreshed.
2. ContextStartedEvent: This event is triggered when the ApplicationContext container is started using the start() method of the ConfigurableApplicationContext interface.
3. ContextClosedEvent: This event is triggered when the ApplicationContext container is closed using the close() method of the ConfigurableApplicationContext interface.
4. ContextStopedEvent: This event is triggered when the stop() method of the ConfigurableApplicationContext interface is used to stop the ApplicationContext container.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.