Wu Yuxiong - a natural framework for the development of natural JAVA SPRING study notes: Spring IoC container BeanFactory and ApplicationContext

IoC means in program development, create an instance of the caller is no longer managed, but created by the Spring container. Spring container will be responsible for the relationship between the process control, and not directly controlled by the program code, so, control is transferred by the program code into a Spring container, the control of the reverse happened, and this is the Spring IoC thought. 
Spring provides two IoC container, respectively, and ApplicationContext BeanFactory
BeanFactory 
BeanFactory is the basis of the type of IoC container, which is defined by the org.springframework.beans.facytory.BeanFactory interface, and provides full service support IoC. In simple terms, BeanFactory is a managed Bean plant, which is mainly responsible for initiating all kinds of Bean, and call their life cycle approach. 
BeanFactory interface has multiple implementation classes, the most common is org.springframework.beans.factory.xml.XmlBeanFactory, it is by definition assembled Bean XML configuration file is. 
When you create a BeanFactory instance, you need to provide detailed configuration information managed by Spring container, such information usually in the form of an XML file management. Code is loaded configuration information as shown below: 
the BeanFactory beanFactory = new new the XmlBeanFactory ( new new a FileSystemResource ( "D: //applicationContext.xml"));
The ApplicationContext 
the ApplicationContext BeanFactory sub-interface is also referred to as the application context. The full path of the interface is org.springframework.context.ApplicationContext, which not only provides all the functionality of BeanFactory, also adds support for the good aspects of i18n (internationalization), resource access, and other events spread. 
There are two common ApplicationContext interface implementation classes, as follows. 
. 1 ) the ClassPathXmlApplicationContext 
class from class to find the path ClassPath specified XML configuration file to find and load the working examples of the ApplicationContext completed, as shown below. 
ApplicationContext the ApplicationContext = new new the ClassPathXmlApplicationContext (String the configLocation); 
In the above code, the configLocation parameter name and location for Spring configuration file, such as applicationContext.xml. 

2 ) a FileSystemXmlApplicationContext 
class from a specified file system to find the path specified in the XML configuration file, and will find examples of working the ApplicationContext loading is completed, as shown below. 
ApplicationContext ApplicationContext =new new a FileSystemXmlApplicationContext (String the configLocation); 
it is the difference between ClassPathXmlApplicationContext: Spring in reading the configuration file, a FileSystemXmlApplicationContext not read the configuration file from the class path, but the location specified by the configuration file parameter, which gets the classpath resources outside, such as "F: /workspaces/applicationContext.xml."
When using the Spring Framework, which can be any of a class Spring ApplicationContext created by instantiating the container. 
Usually in a Java project, will be used by ClassPathXmlApplicationContext class is instantiated ApplicationContext container, rather in the Web project, instantiate work ApplicationContext container will be referred to the Web server to complete. Examples of Web server ApplicationContext container is typically, only need to add the following code based web.xml ContextLoaderListener embodiment implemented using:
 <! - Spring specify the position of the profile, a plurality of profiles, separated by commas -> 
<context-param> 
    <param-name> contextConfigLocation </ param-name> 
    <- - applicationContext.xml spring loading the files in the spring catalog!> 
    <param-value> 
        the CLASSPATH: spring / applicationContext.xml
     </ param -value> 
</ context-param> 
<-! ContextLoaderListener manner specify start Spring container -> 
<listener> 
    <
        org.springframework.web.context.ContextLoaderListener
     </ listener- class > 
</ listener> 
It should be noted, BeanFactory and ApplicationContext Bean are loaded via XML configuration file. 
The main difference is that, if one does not attribute Bean injection is used after BeanFacotry load, once the first call getBean () method throws an exception, while the ApplicationContext self-test during initialization, it is a good check whether or not the attribute dependency injection. 
Therefore, in the actual development, often choose to use ApplicationContext, but only when there are fewer system resources before considering the use of BeanFactory.

 

Guess you like

Origin www.cnblogs.com/tszr/p/12128814.html