Spring~Basic: Difference and connection between ApplicationContext and BeanFactory

BeanFactory

BeanFactoryIt is a relatively basic container in String. This container interface is defined under the spring.beanspackage, and its main function is to provide support for dependency injection (DI) .

In Spring, there are a large number BeanFactoryof implementations of interfaces, among which XmlBeanFactory is the most commonly used . This container can read metadata from an XML file and generate a configured system and application from these metadata.

Generally, BeanFactory is used in mobile devices or applet-based applications where resources are very valuable. In other cases, BeanFactory is not commonly used, but is preferred ApplicationContext.

How to use it in the code:
We first create a common class as a Bean object.

package model;

public class HelloWorld {
    
    
    private String message;
    public void setMessage(String message){
    
    
        this.message=message;
    }
    public void getMessage(){
    
    
        System.out.println("Your Message : "+message);
    }
}

Then store this Bean object in the spring configuration file:

 <beans>
<!--         <bean id="user" class="model.UserBean"></bean>-->
         <bean id="helloWorld" class="model.HelloWorld">
             <property name="message" value="Hello BeanFactory!"></property>
         </bean>
     </beans>

Use this Bean object in the main program of the test class App:

import model.HelloWorld;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class App {
    
    
    public static void main(String[] args) {
    
    
        BeanFactory factory= new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
        HelloWorld obj=(HelloWorld)factory.getBean("helloWorld");
        obj.getMessage();
    }
}

The first step, we use BeanFactory()to generate a factory bean, and then use ClassPathResource() to load the configuration file of the available beans under the path ClassPath path, and then the BeanFactory will create and initialize all the bean objects, that is, we store in the configuration file. Bean . In the second step, we obtain the business object through the getBean
method of the factory bean , and then query the corresponding bean in the configuration file through the id, and force the bean object into the HelloWorld class. When we obtain this object, we can call any method of it.

After the above program is executed, the result is as follows:

Your Message : Hello BeanFactory!

ApplicationContext

  • ApplicationContextIt is a sub-interface of BeanFactory, also known as Spring's context . Through this context object, we can get the Bean we stored in the configuration file. This context object is equivalent to the above factory Bean.
  • ApplicationContext is a relatively advanced container in Spring. Similar to BeanFactory, it can read and load beans in configuration files, and then collect these bean objects together, and allocate beans when requested.
  • ApplicationContext not only has the functions of BeanFactory, but also extends it on its basis, such as parsing text information from property files and passing events to listeners.
  • The ApplicationContext container is defined in the org.springframework.context.ApplicationContext interface, which is defined under the spring.contextpackage.
  • ApplicationContext inherits all the functions of BeanFactory, so in general, we prefer to use ApplicationContext over BeanFactory, but BeanFactory can still be used in lightweight applications, such as mobile devices and applet-based applications, etc...

ApplicationContext has three frequently used implementations:

  • ClassPathXmlApplicationContext : Load and read the defined bean from the XML file. When using this container, we do not need to provide the specific XML file path, but only need to configure the CLASSPATH environment variable correctly, and the container will automatically search for the bean's configuration file from the CLASSPATH.
  • FileSystemXmlApplicationContext : This container loads and reads defined beans from XML files. When using this container, we need to provide the full path to the xml file.
  • WebXmlApplicationContext : Loads already defined beans from the web application scope.

Use in code:
Create a base class as a Bean object:

package model;

public class HelloApplicationContext {
    
    
    private String message;
    public void setMessage(String message){
    
    
        this.message=message;
    }
    public void getMessage(){
    
    
        System.out.println("Your Message : "+message);
    }
}

Store the Bean in the XML configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

     <beans>
<!--         <bean id="user" class="model.UserBean"></bean>-->
         <bean id="helloAppCon" class="model.HelloApplicationContext">
             <property name="message" value="Hello ApplicationContext!"></property>
         </bean>
     </beans>

</beans>

Read out the Bean object in the test class and call its getMessage method:

import model.HelloApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext factory=new ClassPathXmlApplicationContext("spring-config.xml");
        HelloApplicationContext obj=(HelloApplicationContext) factory.getBean("helloAppCon");
        obj.getMessage();
    }
}

The first step is to obtain the spring context object through ApplicationContext, then load the xml configuration file through ClassPathXmlApplicationContext, and create and initialize all bean objects.
In the second step, by calling the getBean method of the context object, the specific bean object is obtained according to the id query, and then its getMessage method is called.

After the above program is executed, the result is as follows:

Your Message : Hello ApplicationContext!

difference and connection

  • The two are defined in different packages : ApplicationContext is defined in spring.context, and BeanFactory is defined in spring.beans.
  • Both BeanFactory and ApplicationContext are top-level interfaces under Spring. ApplicationContext is a subclass of BeanFactory . In addition to inheriting the basic functions of BeanFactory, ApplicationContext also supports other features: such as internationalization (MessageSource), accessing resources (URL/file), loading Enter multiple contexts, AOP (interceptors), message sending responses, etc.
  • From a performance point of view , the implementation of BeanFactory uses lazy loading , which is instantiated only when we call it, and which class is loaded as needed, which is relatively lightweight. The ApplicationContext is preloaded , all beans are loaded and initialized at one time, the startup process is slow, but the subsequent execution is faster.

Guess you like

Origin blog.csdn.net/Merciful_Lion/article/details/124000481