Interview not know the difference between BeanFactory and the ApplicationContext?

BeanFactory and ApplicationContext interfaces are used to obtain the Spring beans from the container, but both of them are very different

I've seen a lot of questions asked and different points of BeanFactory ApplicationContext, take this into account, I should use the former or the latter get beans from the Spring container it? Please look down
directory-1161965_640.jpg

What is the Spring Bean

This is a very simple and very complex issue, generally speaking, Spring beans Spring Java object container is to be managed, a look at a simple example

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

XML based configuration, beans.xml provide metadata Spring container bean

What is the Spring container

Spring container is responsible for instantiating, configuration and assembly Spring beans, the following look at how to configure our HelloWorld POJO IoC container

<?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-3.0.xsd">
   <bean id = "helloWorld" class = "com.zoltanraffai.HelloWorld">
      <property name = "message" value = "Hello World!"/>
   </bean>
</beans>

Now, it has been the Spring container management, the next question is: How do we get it?

Different points of BeanFactory and ApplicationContext

BeanFactory interface

This is used to access a Spring container root interface to access the Spring container, we will use Spring Dependency Injection function, use BeanFactory interface and its sub-interface
features:

  • Bean instantiation / tandem
    Typically, to achieve BeanFactory that use lazy loading, which means beans only if we pass getBean () method call them directly when instantiated
    achieve BeanFactory most commonly used API is XMLBeanFactory
    here's how get a bean example by BeanFactory:
package com.zoltanraffai;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.beans.factory.InitializingBean; 
import org.springframework.beans.factory.xml.XmlBeanFactory; 
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 interface

Spring ApplicationContext is a central interface applications, to provide configuration information to the application
it inherits the BeanFactory interface, it contains all the functionality of BeanFactory ApplicationContext and much more! Its main function is to support the creation of large-scale business applications
features:

  • Bean instantiation/wiring
  • Bean instantiation / tandem
  • Automatic registration BeanPostProcessor
  • Automatic registration of BeanFactoryPostProcessor
  • MessageSource easy access (i18n)
  • ApplicationEvent release
    and lazy BeanFactory loaded in different ways, it is pre-loaded, so each bean in a start after ApplicationContext instantiation
    here is ApplicationContext usage example:
package com.zoltanraffai;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.beans.factory.InitializingBean; 
import org.springframework.beans.factory.xml.XmlBeanFactory; 
public class HelloWorldApp{ 
   public static void main(String[] args) { 
      ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml"); 
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");    
      obj.getMessage();    
   }
}

to sum up

ApplicationContext includes all the features of the BeanFactory, the former usually recommended. But there are some restrictions circumstances, such as mobile application memory consumption is relatively stringent, in those scenarios, the use of more lightweight BeanFactory is more reasonable. However, in most enterprise applications, ApplicationContext is your first choice.

Soul questioning

  • How to use BeanPostProcessor and BeanFactoryPostProcessor?
  • You know Spring Bean's life cycle it? Understanding and use of these bean will have a very big help.

Welcome sustained attention, follow-up will be a series of articles Spring knowledge points to explain the series, something that easy to get the interview, as well as at work that take advantage of Spring

a (1).png

翻译自:Difference Between BeanFactory and ApplicationContext in Spring

Guess you like

Origin www.cnblogs.com/FraserYu/p/11112122.html