Spring boot container performs specific actions after loading

Source: Internet
Author: User

Sometimes we need to open some threads or some programs to do something after the Spring boot container starts and loads. At this point we need to configure the Contextrefreshedevent event to do what we have to do.

1, Applicationstartup class
public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent>{    public void onApplicationEvent(ContextRefreshedEvent event)      {        //在容器加载完毕后获取dao层来操作数据库        OSSVideoRepository ossVideoRepository = (OSSVideoRepository)event.getApplicationContext().getBean(OSSVideoRepository.class);        //在容器加载完毕后获取配置文件中的配置        ServerConfig serverConfig = (ServerConfig)event.getApplicationContext().getBean(ServerConfig.class);        ServerFileScanner fileScanner = new ServerFileScanner(                ossVideoRepository, serverConfig.getScanpath());        //在容器加载完毕后启动线程        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 = Aliyunossaccess    KeyId;    } public String Getaliyunossaccesskeysecret () {return aliyunossaccesskeysecret; } public void Setaliyunossaccesskeysecret (String aliyunossaccesskeysecret) {This.aliyunossaccesskeysecret = Ali    Yunossaccesskeysecret; } public String Getaliyunossbucketname () {return AliyunossbuckEtname; } public void Setaliyunossbucketname (String aliyunossbucketname) {this.aliyunossbucketname = Aliyunossbucketnam    E    } public String Getscanpath () {return scanpath;    } public void Setscanpath (String scanpath) {this.scanpath = Scanpath; }}
PS: There are also some spring built-in events

1. This event is triggered when the Contextrefreshedevent: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 using the Configurableapplicationcontext interface stops the ApplicationContext container.



Dedicated to the memory of a bad self
Links: http://www.jianshu.com/p/01f7a971a4b9
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Spring boot container performs specific actions after loading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.