The similarities and differences between BeanFactory and ApplicationContext

the same:

  • Spring provides two different IOC container, is a BeanFactory, the other is ApplicationContext, they are Java interface, ApplicationContext inherited BeanFactory (ApplicationContext inherited ListableBeanFactory.
  • They can be used to configure the XML attributes, also supports automatic injection property.
  • The ListableBeanFactory inherited BeanFactory), BeanFactory and ApplicationContext provides a way to use getBean ( "bean name") to obtain bean.

BeanFactory bean obtain registration information

public class HelloWorldApp{ 
   public static void main(String[] args) { 
      XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource("beans.xml")); 
      HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");    
      obj.getMessage();    
   }
}

ApplicationContext bean obtain registration information

public class HelloWorldApp{ 
   public static void main(String[] args) { 
      ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml"); 
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");    
      obj.getMessage();    
   }
}

But they have some differences in work and characteristics:

  • Then instantiated when you call getBean () method, BeanFactory only instantiated bean, but when you start ApplicationContext container instantiate singleton bean, do not wait to call getBean () method.
  • BeanFactory does not support internationalization, namely i18n, but ApplicationContext provide support for it.
  • Another difference between BeanFactory and ApplicationContext is able to publish events to be registered as a listener bean.
  • A core BeanFactory and ApplicationContext implementation is XMLBeanFactory of a core implementation is ClassPathXmlApplicationContext, environmental Web container we use and increases WebApplicationContext getServletContext method.
  • If you use the automatic implantation using the BeanFactory, you need to use API registration AutoWiredBeanPostProcessor, if you use ApplicationContext, you can use XML for configuration.
  • In short, the BeanFactory provides the basic functions of IOC and DI, while the ApplicationContext provides advanced features, BeanFactory available for testing and non-production use, but ApplicationContext is more feature-rich container implementation, should be better than the BeanFactory

Guess you like

Origin www.cnblogs.com/cxuanBlog/p/10934960.html