org.springframework.beans
And
org.springframework.context
Packages are the basis of Spring IoC containers.
BeanFactory
The Advanced Configuration mechanism makes it possible to manage objects of any nature.
ApplicationContext
Yes
BeanFactory
Functions are further enhanced, such as easier integration with Spring AOP, message resource processing (International processing), event transfer, and context implementation of different application layers (such as for Web Applications
WebApplicationContext
). In short,
BeanFactory
The preparation framework and basic functions are provided.
ApplicationContext
Then, more functions are added to support the core content of the enterprise.
ApplicationContext
Completely
BeanFactory
Therefore
BeanFactory
The capabilities and behaviors are also applicable
ApplicationContext
.
org.springframework.beans.factory.BeanFactory
Is Spring IoC
ContainerThe IOC container is responsible for accommodating the previously described bean and managing the bean. In spring,
BeanFactory
Is the core interface of the IOC container. Its responsibilities include instantiating, locating, configuring objects in the application, and establishing dependencies between these objects. Spring provides us with a lot of easy-to-use
BeanFactory
Implementation,
XmlBeanFactory
Is the most commonly used one. This implementation describes the objects that constitute the application and the dependencies between objects in XML format.
XmlBeanFactory
Class will hold this XML
Configure metadataAnd use it to build a fully configurable system or application. The instantiation of the Spring IoC container is very simple, as shown in the following example:
Resource resource = new FileSystemResource("beans.xml");BeanFactory factory = new XmlBeanFactory(resource);
... Or...
ClassPathResource resource = new ClassPathResource("beans.xml");BeanFactory factory = new XmlBeanFactory(resource);
... Or...
ApplicationContext context = new ClassPathXmlApplicationContext( new String[] {"applicationContext.xml", "applicationContext-part2.xml"});// of course, an ApplicationContext
is just a BeanFactory
BeanFactory factory = (BeanFactory) context;
3.2.2.1. It is very useful to split the xml configuration file into multiple parts based on XML configuration metadata. To load multiple XML files to generate an applicationcontext instance, you can use the file path as a string array to send it to the applicationcontext constructor. Bean Factory reads bean definitions from multiple files by calling bean defintion reader. Usually, the spring team tends to do this, because the configuration does not find their combination with other configuration files. Another method is to use one or more
<import/>
Element to load bean definitions from one or more files. All
<import/>
The element must be placed in
<bean/>
To complete bean-defined import. Let's take an example:
<beans><import resource="services.xml"/> <import resource="resources/messageSource.xml"/> <import resource="/resources/themeSource.xml"/> <bean id="bean1" class="..."/> <bean id="bean2" class="..."/> </beans>
In the above example, we start from three external files:
services.xml
,
messageSource.xml
And
themeSource.xml
To load bean definitions. All relative paths are used here. Therefore, in this example
services.xml
Must be in the same directory or class path as the import file, and
messageSource.xml
And
themeSource.xml
Must be placed in the directory where the import file is located
resources
Directory. As you can see, the slash '/' At the beginning can be ignored. Therefore, it may be better to skip the slash. According to the schema (or DTD) of the spring xml configuration file, the imported file must be a fully valid XML bean definition file and the root node must be
<beans/>
Element. When XML is used to describe the configuration metadata
<bean/>
Element
class
Attribute to specify the type of the instantiated object.
class
Attribute (corresponding
BeanDefinition
Instance
Class
Attribute) is usually required (but there are two exceptions, "instantiate using the instance factory method" and "bean-defined inheritance "). The instance factory method instantiation is similar to the static factory method instantiation. The instance factory method used for instantiation is located in another existing bean, the container will call the Bean Factory method to create a new bean instance to use this mechanism,
class
Property must be empty, and
factory-bean
The property must be specified as the name of the bean containing the factory method in the current (or its ancestor) container, and the factory method of the factory Bean must pass
factory-method
Attribute (see the following example ).
<!-- the factory bean, which contains a method called createInstance()
--><bean id="myFactoryBean" class="..."> ...</bean> <!-- the bean to be created via the factory bean --><bean id="exampleBean" factory-bean="myFactoryBean" factory-method="createInstance"/>
Although the mechanism for setting bean properties is still mentioned here, it is implicitly managed by the factory bean and configured by dependency injection (DI. In essence,
BeanFactory
It is only an advanced factory interface that maintains bean definitions and dependencies. Pass
BeanFactory
We can access bean definitions. The following example creates a bean factory that reads bean definitions from the XML file:
InputStream is = new FileInputStream("beans.xml");BeanFactory factory = new XmlBeanFactory(is);
This is basically the case, and then use
getBean(String)
Method to obtain the bean instance;
BeanFactory
The provided method is extremely simple. It only provides six methods for the customer's code to call:
boolean containsBean(String)
: IfBeanFactory
Returns true if the bean definition (or bean instance) contains the given name.
Object getBean(String)
: Return the bean instance registered with a given name. According to bean configuration, if Singleton mode is used, a shared instance is returned. Otherwise, a new instance is returned. If the specified bean is not found, this method may throwBeansException
Exception (will actually throwNoSuchBeanDefinitionException
Exception). Exceptions may also be thrown during bean instantiation and preprocessing.
Object getBean(String, Class)
: Return the bean instance registered with a given name and convert it to an instance of the given class type. If the conversion fails, the corresponding exception (BeanNotOfRequiredTypeException
) Will be thrown. The abovegetBean(String)
The method also applies to this rule.
Class getType(String name)
: ReturnsClass
. If the specified bean instance is not foundNoSuchBeanDefinitionException
Exception.
boolean isSingleton(String)
: Determines whether the bean definition (or bean instance) of the given name is in singleton mode (Singleton will be discussed in bean scope). If the bean is not found, it will be thrownNoSuchBeanDefinitionException
Exception.
String[] getAliases(String)
: Returns all aliases of the given bean name.
========================================================== ==================================== Beanfactory its responsibilities include: instantiate, locate, configure objects in the application, and establish dependencies between these objects. Factorybean ), the function is to generate other bean instances. Generally, this bean has no special requirements. You only need to provide a factory method to return other bean instances. Other bean instances generated by factory beans are no longer generated by spring containers. Therefore, unlike common bean configurations, class elements are no longer required. ========================================================== ========================================================== ========================================================== ====== Proxyfactorybean is used to create a proxy (based on the bean generated by advisor, that is, the targetbean proxy) Our advisor, pointcut, and so on, the ultimate goal is to create this proxy.