Use of Springboot listener Applicationlistener-Monitor applicationreadyevent events

Source: Internet
Author: User

Reference: http://www.shareniu.com/article/73.htm

The need is to write a method of my writing to run after the project started, before using the Redis message listener, feeling can follow the listener this idea, and then thought of Sringboot listener

Two. The following types of events are currently supported in spring boot:

    1. Applicationfailedevent: This event is an action when spring boot fails

    2. Applicationpreparedevent: Triggered when context is ready

    3. Applicationreadyevent: Trigger When context is ready to complete

    4. applicationstartedevent:spring Boot Boot Listener class

    5. Springapplicationevent: Get Springapplication

    6. Applicationenvironmentpreparedevent: Environment prepared beforehand

Three. Use of listeners

First: First define a listener class that you use and implement the Applicationlistener interface.

@Componen
PublicclassMessagereceiverImplementsApplicationlistener<applicationreadyevent> { PrivateLogger Logger = Loggerfactory.getlogger (messagereceiver.class); PrivateUserService UserService =NULL; @Override Public voidonapplicationevent (Applicationreadyevent event) {Configurableapplicationcontext ApplicationContext=Event.getapplicationcontext (); //resolve UserService always emptyUserService = Applicationcontext.getbean (userservice.class);
System.out.println ("name" +userservice.getname ()); }}

Second: Register the custom Listener with the Addlisteners method in the Springapplication class

 Public class Application {    publicstaticvoid  main (string[] args) {        new springapplication (application.  Class);        Application.addlisteners (new  messagereceiver ());        Application.Run (args);}        }

Start Project

Use of Springboot listener Applicationlistener-Monitor applicationreadyevent events

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.