BeanFactory differs from ApplicationContext

BeanFactory container: (provides storage, stores Bean information)

It is the most simple container, provides basic support for DI, it USES org. Springframework. Beans. Factory. To define the BeanFactory interface. BeanFactory, or related interfaces such as BeanFactoryAware, InitializingBean, DisposableBean, still exists in Spring with a great deal of reverse compatibility with spring-integrated third party frameworks. In Spring, there are numerous implementations of the BeanFactory interface. One of the most commonly used is the XMLBeanFactory class. The container reads configuration metadata from an XML file to generate a configured system or application.

       The BeanFactory interfaceTo access the Spring container, we will use Spring dependency injection, using the BeanFactory interface and its subinterfaces.

Features:

  • Instantiation/concatenation of beans

In general, BeanFactory is implemented using lazy loading, which means beans are only instantiated when we call them directly via the getBean() method. The most common API for implementing BeanFactory is XMLBeanFactory:

public class Test{ 
   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

ApplicationContext interface. ApplicationContext is the central interface in a Spring application that provides configuration information to the application. It inherits the BeanFactory interface, so the ApplicationContext contains all the functionality of the BeanFactory and more! Its main function is to support the creation of large business applications

Features:

  • Bean instantiation/wiring
  • Instantiation/concatenation of beans
  • Automatic BeanPostProcessor registration
  • Automatic BeanFactoryPostProcessor registration
  • Easy access to MessageSource
  • ApplicationEvent release

Unlike BeanFactory lazy loading, it is preloaded, so each bean is instantiated after the ApplicationContext is started

public class Test{ 
   public static void main(String[] args) { 
    	 ApplicationContext xmlContext = new ClassPathXmlApplicationContext("classpath:application.xml");
       	 User user = xmlContext.getBean("user", User.class);
       	 System.out.println(user);

        ApplicationContext annotContext = new AnnotationConfigApplicationContext(Config.class);
        User beanUser = annotContext.getBean("user", User.class); System.out.println(beanUser); }}Copy the code

ApplicationContext container: (provides functionality for internationalization, loading beans, listeners, and more, based on BeanFactory)

The container adds more enterprise-specific functionality, such as the ability to parse text information from a properties file, and the ability to publish application events to interested event listeners. The container is made up of org. Springframework. Context. ApplicationContext interface definitions

  • BeanFactory is the bean factory, the top core interface of Spring. Without BeanFactory, there would be no bean. The factory is only responsible for producing beans according to requirements.
  • ApplicationContext is customer-oriented, so better service users are needed, not only to provide beans and call factories to produce beans, but also to provide a set of user-friendly services (internationalization, loading Bean definitions, listeners, etc.). How to produce beans is left to the factory.
  • The ApplicationContext container includes all the functionality of the BeanFactory container, so it is generally not recommended to use the BeanFactory container. BeanFactory can still be used for lightweight applications

The difference between BeanFactory and FactoryBean

  • BeanFactory is the Bean factory, the top core interface of Spring. Without BeanFactory, there would be no Bean.
  • A FactoryBean is also an interface. The Bean it decorates becomes an envoy Bean. The original Bean is hidden, but the getObject of the FactoryBean returns the final Bean.
  • BeanFactory is the top-level interface of the Spring container, and here FactoryBean is also an interface. It is easy to confuse the two interfaces. FactoryBean allows the Spring container to create bean objects through the implementation of this interface
  • ApplicationContext contains all features of the BeanFactory, and the former is generally recommended. However, there are some limiting situations, such as mobile application memory consumption, where it makes more sense to use a lighter BeanFactory. However, in most enterprise applications, ApplicationContext is your first choice.