Effect: 1. The beanfactory is responsible for reading the bean configuration document, managing the bean's loading, instantiating, maintaining the dependency between the beans, and responsible for the bean's declaration cycle.
2. ApplicationContext provides more complete framework functionality in addition to the functionality provided by the beanfactory above:
A. Internationalization support
B. Resource access: Resource rs = ctx. GetResource ("Classpath:config.properties"), "File:c:/config.properties"
C. Event delivery: By implementing the Applicationcontextaware interface
3. Common ways to get ApplicationContext:
Filesystemxmlapplicationcontext: Created from a file system or URL-specified XML configuration file with an array of configuration file names or file names
Classpathxmlapplicationcontext: Created from the classpath XML configuration file, you can read the configuration file from the jar package
Webapplicationcontextutils: Reading a configuration file from the Web application's root directory requires a configuration in XML. config, which can be configured with a listener or servlet to implement
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Either way, the default profile is Web-inf/applicationcontext.xml, or you can use Context-param to specify the configuration file
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>
Spring's IOC container is an instantiated class that implements the Beanfactory interface. In fact, Spring provides two different kinds of containers: one is the most basic beanfactory, the other is the extended applicationcontext. Beanfactory provides only the most basic dependency injection support, while ApplicationContext expands the beanfactory to provide additional functionality. There is a difference between instantiating these two types of containers:
public class Beanfactorytest {public static void Main (string[] args) throws throwable{ Resourcepatternresolver Resolver = new Pathmatchingresourcepatternresolver (); Resource res = Resolver.getresource ("Classpath:com/baobaotao/beanfactory/beans.xml"); System.out.println (Res.geturl ()); beanfactory bf = new Xmlbeanfactory (res); System.out.println ("Init beanfactory."); Car car = Bf.getbean ("Car", car.class); System.out.println ("Car bean is ready for use!"); Car.introduce (); }}
public class Xmlapplicationcontexttest {public static void Main (string[] args) {
If the configuration file is placed under the classpath, the user may prefer to use the Classpathxmlapplicationcontext implementation class ApplicationContext CTX = new Classpathxmlapplicationcontext ("Com/baobaotao/context/*.xml"); Car car1 = Ctx.getbean ("Car", car.class);
If the configuration file is placed under the path of the file system, it is preferable to use the Filesystemxmlapplicationcontext implementation class// CTX = new Filesystemxmlapplicationcontext (" D:/masterspring/chapter3/src/com/baobaotao/context/*.xml ");// Car car2 = Ctx.getbean (" Car ", Car.class);} }
There is a significant difference between ApplicationContext initialization and beanfactory: Beanfactory does not instantiate the bean when it initializes the container, and the target bean is not instantiated until it accesses a bean for the first time; ApplicationContext, however, instantiates all single-instance beans when the application context is initialized. Therefore, the initialization time of ApplicationContext is slightly longer than that of beanfactory.
What are the roles and differences between Beanfactory and ApplicationContext in spring?