Spring common interfaces and classes (i)

Source: Internet
Author: User

First, Applicationcontextaware interface

When a class needs to acquire an ApplicationContext instance, you can let the class implement the Applicationcontextaware interface. The code is shown below:

 Public classAnimalImplementsApplicationcontextaware, beannameaware{PrivateString Beanname; PrivateApplicationContext ApplicationContext;  Public voidsetbeanname (String name) { This. Beanname =name; }        /**     * @paramApplicationContext This parameter is automatically assigned by the spring container*/     Public voidSetapplicationcontext (ApplicationContext ApplicationContext)throwsbeansexception { This. ApplicationContext =ApplicationContext; }     Public voidrun () {System.out.println (beanname); //Publish custom EventsAnimalevent event =NewAnimalevent ( This, "Tiger");    Applicationcontext.publishevent (event); }}

Some common object instances can be automatically assembled by @autowired annotations:

@Autowired Private messagesource Messagesource; @Autowired Private resourceloader Resourceloader; @Autowired Private ApplicationContext ApplicationContext;

Second, Applicationevent abstract class

When creating custom events, you can create a new class that inherits from the Applicationevent abstract class. The code is shown below:

/*** Custom Events*/ Public classAnimaleventextendsapplicationevent {PrivateString name;  PublicString GetName () {returnname; }    /**     * @paramSource Event Sources Object*/     Publicanimalevent (Object source) {Super(source); }         Publicanimalevent (Object source, String name) {Super(source);  This. Name =name; }}

Third, Applicationlistener interface

When you need to listen to custom events, you can create a new class that implements the Applicationlistener interface and configure that class into a spring container. The code is shown below:

 /**   * Custom Event listener  */ public  class  Customeventlistener implements   Applicationlistener { public  void   Onapplicationevent (Applicationevent event) { if  (Event instanceof   Animalevent) {an            Imalevent animalevent  = (animalevent) event;        SYSTEM.OUT.PRINTLN ( "Trigger custom event: Animal name is" + Animalevent.getname ()); }    }}
<!---<id= "Customeventlistener"  class  = "Com.cjm.spring.CustomEventListener"/>

To publish a custom event, you need to call ApplicationContext's Publishevent method, see the animal class of source code for specific usage.

Four, Beannameaware interface

The Beannameaware interface can be implemented when the bean needs to acquire its own id/name in the container.

Five, Initializingbean interface

This bean implements the Initializingbean interface when it is necessary to do some special processing after all the Bean's property settings are successful.       The effect is equivalent to the use of the bean's Init-method attribute or the use of @postcontsuct annotations. The order of execution in three ways: annotate first, then execute the method defined in the Initializingbean interface, and finally execute the method specified by the Init-method property.

Vi. Disposablebean interface When you need to do some special processing before the bean is destroyed, you can let the bean implement the Disposablebean interface.       The effect is equivalent to the use of the bean's Destroy-method attribute or the use of @predestory annotations. Three ways to execute the order: annotate first, then execute the method defined in the Disposablebean interface, and finally execute the method specified by the Destroy-method property

Spring common interfaces and classes (i)

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.