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)