“This is the third day of my participation in the First Challenge 2022, for more details: First Challenge 2022”.
Introduction to the
The essence of Spring is a Bean Factory, or bean container, that produces all the beans we need for our use.
- BeanFactory is the infrastructure of the Spring framework, oriented towards Spring itself;
- ApplicationContext is intended for developers using the Spring framework, and in almost all applications we use ApplicationContext directly rather than the underlying BeanFactory.
BeanFactory
The beanFactory manages beans at various stages of their life cycle, and Spring exposes these stages to us through interfaces that allow us to process beans. We simply have the bean implement the corresponding interface, Spring then calls the interface we implemented to process the bean during its life cycle.
BeanFacotry is the primitive Factory in Spring. XMLBeanFactory, for example, is a typical BeanFactory. The original BeanFactory didn’t support many of Spring’s plug-ins, such as AOP functionality, Web applications, and so on.
example
public class MainApp { public static void main(String[] args) { XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource("Beans.xml")); HelloWorld obj = (HelloWorld) factory.getBean("helloWorld"); obj.getMessage(); }}Copy the code
- The XmlBeanFactory loads the Spring configuration information through the Resource and starts the IoC container, which can then be retrieved from the IoC container using the BeanFactory#getBean(beanName) method. When the IoC container is started through the BeanFactory, the Bean defined in the configuration file is not initialized; the initialization action occurs on the first call.
- The BeanFactory caches Bean instances, so the second time you get the Bean using getBean() you get the Bean instance directly from the IoC container’s cache. Spring DefaultSingletonBeanRegistry class provides a used to slow the CDS instance Bean cache, it is a HashMap with the cache, Single-instance beans are stored in the HashMap with a beanName key.
ApplicationContext
ApplicationContext contains all the functionality of a BeanFactory, and is generally superior to a BeanFactory. Of course, BeanFactory can still be used in lightweight applications, such as mobile devices or applet-based applications
The most commonly used ApplicationContext interface implementation
- FileSystemXmlApplicationContext: the container load from the XML file has been defined a bean. Here, you need to provide the full path to the constructor XML file.
- ClassPathXmlApplicationContext: the container load from the XML file has been defined a bean. Here, you don’t need to provide the full path to the XML file, just configure the CLASSPATH environment variable correctly, because the container will search the bean configuration file from the CLASSPATH.
- WebXmlApplicationContext: This container loads beans defined in an XML file within the scope of a Web application.
example
public class MainApp { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext ("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); }}Copy the code
- After loading the bean under the specified path configuration file, using the framework provides FileSystemXmlApplicationContext API to generate bean plant. FileSystemXmlApplicationContext responsible for generating and initialize all the objects.
- Use the getBean() method in the context to get the beans you want.
The difference between
1.BeanFactroy uses lazy loading to inject beans, whereas ApplicationContext creates all beans at once when the container starts. The only disadvantage of ApplicationContext is that it takes up memory. When an application has many configured beans, the application starts slowly.
BeanFactory and ApplicationContext both support BeanPostProcessor and BeanFactoryPostProcessor, but the differences between them are as follows: BeanFactory needs to be registered manually, whereas ApplicationContext is automatically registered.
3. BeanFactory is primarily geared towards the infrastructure of the Spring framework, and towards Spring itself. Applicationcontex is aimed at developers who use spring.
conclusion
- BeanFactory is responsible for reading bean configuration documents, managing bean loading, instantiation, maintaining bean dependencies, and bean declaration cycles.
- ApplicationContext provides more complete framework functionality in addition to the above functionality provided by the BeanFactory: