Two configuration containers of spring

Spring provides two container types

The spring IOC container is an IOC service provider. Two container types are provided: beanfactory and ApplicationContext. Spring's IOC container is a lightweight container that provides IOC support. In addition to basic IOC support, it also provides support beyond IOC as a lightweight container.

beanfactory

Beanfactory is the base type IOC container. As the name suggests, it is the factory that produces beans. Able to provide complete IOC services. If there is no special specification, it adopts the delayed initialization strategy by default. Only when the client object needs to access a managed object in the container, the object can be initialized and dependency injected. Therefore, relatively speaking, the speed at the initial stage of container startup is relatively fast. The resources required are limited. Therefore, beanfactory is a suitable IOC container for scenarios with limited resources and less strict functional requirements

Beanfactory has several implementations, of which the most commonly used is XML beanfactory, which loads beans according to the definitions in the XML file. When creating an XML bean factory, you need to pass an org. Org springframework. core. io. An instance of resource is given to the constructor, which is used to provide XML files to beanfactory.

Spring provides the following resource implementations:

For example, use filesystemresource to create an XML bean factory. The code is as follows:

ApplicationContext

ApplicationContext is built on the basis of beanfactory. It is a relatively advanced container implementation. In addition to all the support of beanfactory, ApplicationContext also provides other advanced features. For example, event release, internationalization information support, etc. All objects managed by ApplicationContext are initialized and bound by default after the type container is started. Compared with beanfactory, ApplicationContext requires more system resources. Because all initialization is completed at startup, the container startup time is longer than beanfactory. Therefore, ApplicationContext is more suitable for scenarios where system resources are sufficient and more functions are required.

ApplicationContext indirectly inherits from beanfactory, so it is an IOC container built on beanfactory.

Among the many implementations of ApplicationContext, the following three implementations are often used:

Loading application context from file system or classpath is similar to loading bean into beanfactory, for example:

The difference between using filesystemxmlapplicationcontext and classpathxmlapplicationcontext is:

The former can only be found in the specified path; The latter can find XML files in the entire classpath, including jar files.

summary

The above is all about the two configuration containers of spring. I hope it will be helpful to you. Interested friends can continue to refer to this site: talk about docker security mechanism, network security between kernel security and containers, explain in detail docker's use of Linux iptables and interfaces to manage container network, etc. if you have any questions, you can leave a message at any time, and the editor will reply to you in time. Thanks for reading!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>