Beanfactory & applicationcontext
In spring, containers are divided into two types:
N beanfactory
N applicationcontext
Beanfactory:
From the above name, we can see that beanfactory uses the factory design pattern. That is to say, this class can distribute all beans.
Beanfactory interface has multiple implementations, commonly used xmlbeanfactory.
// Use beanfactory to read the spring configuration file in classpath Beanfactory factory =NewXmlbeanfactory (NewClasspathresource ("Spring. xml ")); |
Or
// Use beanfactory to read the spring configuration file in the file system Beanfactory factory =NewXmlbeanfactory (NewFilesystemresource ("C:/spring. xml ")); |
Applicationcontext:
Beanfactory is powerful enough for simple applications. However, to obtain more powerful functions provided by spring, a more advanced spring container applicationcontext should be used. Applicationcontext and beanfactory load bean definitions from the configuration file, assemble them, and distribute beans as needed. However, applicationcontext provides the following functions:
N provides text parsing tools, including i18n internationalization;
N provides a general method for loading file resources;
N can send events to the bean registered as the listener.
In the implementation of applicationcontext, three implementations are often used:
N classpathxmlapplicationcontext read the spring configuration file from classpath (including jar)
Applicationcontext context =NewClasspathxmlapplicationcontext ("Spring. xml "); |
N filesystemxmlapplicationcontext read the spring configuration file from the file system
Applicationcontext context =NewFilesystemxmlapplicationcontext ("C:/spring. xml "); |
N xmlwebapplicationcontext read the spring configuration file from the Web System
In addition to the additional functions provided by applicationcontext, an important difference between applicationcontext and beanfactory is how the singleton bean is loaded. Beanfactory delays loading all beans until the getbean () method is called. Applicationcontext will pre-load the singleton bean. When the getbean () method is called, the bean has been created successfully.
Schema of the spring configuration file
<? 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-2.0.xsd"> </Beans> |
Configure a bean in the spring configuration file
<Bean id = "userdao" class = "com. Kay. Dao. impl. userdaoimpl"> </bean> |
Or
<Bean name = "userdao" class = "com. Kay. Dao. impl. userdaoimpl"> </bean> |
Note the differences between the ID and name attributes and their connection:
The ID and name attributes are used to specify the bean identifier. The ID is unique and is the real ID attribute in XML. It is verified by the XML parser. The id value must be named like the name variable in Java, for example, you cannot start with a number. However, the name attribute value is not required. If you want to, you can assign a value such as "123". The name attribute can also be used ", to separate multiple Identifiers (aliases) specified for the bean. For example:
<Bean name = "userdao, Dao, user" class = "com. Kay. Dao. impl. userdaoimpl"> </bean> |
The name attribute is not mandatory. When the name attribute is not written, the spring container automatically generates the name attribute value.
<Bean class = "com. Kay. Dao. impl. userdaoimpl"> </bean> |
The name attribute is the same as the class attribute value when obtaining the bean.
If three identical beans are declared in the spring configuration file at the same time:
<Bean class = "com. Kay. Dao. impl. userdaoimpl"> </bean> <Bean class = "com. Kay. Dao. impl. userdaoimpl"> </bean> <Bean class = "com. Kay. Dao. impl. userdaoimpl"> </bean> |
Then the values of name are:
Com. Kay. Dao. impl. userdaoimpl Com. Kay. Dao. impl. userdaoimpl #1 Com. Kay. Dao. impl. userdaoimpl #2 |