Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common interfaces of Spring

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the common interfaces of Spring". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the common interfaces of Spring.

1. InitializingBean interface

There is only one afterPropertiesSet method in the InitializingBean interface, which is easy to understand from the name of the method, which is called after all the properties of the Bean are set to do some initialization work. Of course, in the configuration file of Spring, the configuration of init-method is also called after the properties of Bean are set to complete some initialization work, but in the order of execution, the method of the interface precedes the configuration. It is worth noting that both methods are used to complete some initialization work, so do not write some complex and time-consuming logic in the corresponding methods.

When you need to do some special processing after all the properties of the bean are successfully set, you can have the bean implement the InitializingBean interface. The effect is equivalent to the use of the init-method attribute of bean or the use of the @ PostContsuct annotation.

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.

2. DisposableBean interface

There is only one destroy method in the DisposableBean interface, which is called before the Bean is destroyed and the life cycle ends, and is used to do some finishing work on the destruction. Similarly, the destroy-method configuration does the same in the Spring configuration file, but in the order of execution, the method of the interface precedes the configuration.

When you need to do some special processing before the bean is destroyed, you can have the bean implement the DisposableBean interface. The effect is equivalent to the use of the destroy-method attribute of bean or the use of the @ PreDestory annotation.

The order of execution in three ways: annotate first, then execute the method defined in the DisposableBean interface, and finally execute the method specified by the destroy-method property.

3. ApplicationContextAware interface

There is only one setApplicationContext method in ApplicationContextAware. The class that implements the ApplicationContextAware interface can obtain the ApplicationContext of the application context of the Spring when the Bean is loaded, and a lot of information in the Spring container can be obtained through ApplicationContext.

When a class needs to get an ApplicationContext instance, you can have the class implement the ApplicationContextAware interface. The code is shown as follows:

Public class Animal implements ApplicationContextAware, BeanNameAware {

Private String beanName

Private ApplicationContext applicationContext

Public void setBeanName (String name) {

This.beanName = name

}

/ * *

* @ param applicationContext this parameter will be automatically assigned by the Spring container

, /

Public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {

This.applicationContext = applicationContext

}

Public void run () {

System.out.println (beanName)

/ / publish custom events

AnimalEvent event = new AnimalEvent (this, "tiger")

ApplicationContext.publishEvent (event)

}

}

You can automatically assemble some commonly used object instances through the @ Autowired annotation:

@ Autowired

Private MessageSource messageSource

@ Autowired

Private ResourceLoader resourceLoader

@ Autowired

Private ApplicationContext applicationContext

4. BeanFactoryAware interface

There is only one setBeanFactory method in the BeanFactoryAware interface. For a class that implements the BeanFactoryAware interface, you can get the BeanFactory that loads the Bean when the Bean is loaded, and you can also get other Bean loaded in the BeanFactory.

5. FactoryBean interface

The FactoryBean interface enables personalized customization of Bean instantiation, allowing the Spring container to load the Bean we want. The class that implements the FactoryBean interface can load the Bean we want by implementing the getObject method.

6. BeanPostProcessor interface

There are two methods in the BeanPostProcessor interface, postProcessBeforeInitialization and postProcessAfterInitialization. A class that implements the BeanPostProcessor interface calls the postProcessBeforeInitialization method and the postProcessAfterInitialization method in this class before and after each Bean initialization (that is, calling setter) to implement the logical control of initialization.

7. InstantiationAwareBeanPostProcessor interface

In the InstantiationAwareBeanPostProcessor interface, the common methods are postProcessBeforeInstantiation and postProcessAfterInstantiation. Before and after the instantiation of each Bean (that is, calling the constructor), the postProcessBeforeInstantiation and postProcessAfterInstantiation methods in the class that implement the interface are called, respectively.

8. BeanFactoryPostProcessor interface

There are only postProcessBeanFactory methods in the BeanFactoryPostProcessor interface. The class that implements this interface can obtain the definition information of the Bean in the container and modify it before the Bean is created. The postProcessBeanFactory method in the implementation class is executed only once and precedes the method of the BeanPostProcessor interface.

9. When you need to listen for custom events, you can create a new class that implements the ApplicationListener interface and configure the class in the Spring container. The code is shown as follows:

/ * *

* Custom event listeners

, /

Public class CustomEventListener implements ApplicationListener {

Public void onApplicationEvent (ApplicationEvent event) {

If (event instanceof AnimalEvent) {

AnimalEvent animalEvent = (AnimalEvent) event

System.out.println ("trigger Custom event: Animal name is" + animalEvent.getName ())

}

}

}

To publish a custom event, you need to call the publishEvent method of ApplicationContext

10. ApplicationEvent abstract class

When you need to create a custom event, you can create a new class that inherits from the ApplicationEvent abstract class. The code is shown as follows:

/ * *

* Custom event

, /

Public class AnimalEvent extends ApplicationEvent {

Private String name

Public String getName () {

Return name

}

/ * *

* @ param source event source object

, /

Public AnimalEvent (Object source) {

Super (source)

}

Public AnimalEvent (Object source, String name) {

Super (source)

This.name = name

}

}

Thank you for your reading, the above is the content of "what are the common interfaces of Spring". After the study of this article, I believe you have a deeper understanding of what the common interfaces of Spring have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report