ioc and bean management

IOC called inversion of control, is simply the process lifecycle management rights and objects created object to the Spring framework to deal with in this development process no longer need to focus on creating and managing the life cycle of an object , but by the Spring framework when needed, the objects created by the Spring framework and life cycle management mechanism called inversion of control.
Wherein, spring can be set according to the attributes of the object disposed in the object creation, this process is called dependency injection (DI).

 

The principle IOC's
full path name when initializing a spring container, spring will go to resolve the specified xml file, which when resolved to the label, the label will be specified in terms of the class attribute of the class, the class created Rong reflection object and stores built-in management Map of the object. Wherein the key is the id of the tag, the value of the object is, then, to get the object from when the container through getBean method, in fact, looking for a match with the built-in key values passed in the Map conditions, and if so, the key to the preservation of the object is returned, if there is no match to the exception is thrown.
It can be speculated with knowledge:
By default, many times get the same id of bean, get will be the same object.

 

By creating an object constructor with no arguments tired
above case is that this method, when the most common way to configure a default is to use the accumulated amount constructor with no arguments to create objects in the initialization Spring container, through reflection on the class attribute configuration obtained bytecode object, the object created by newInstance ().
Class c = Class.forName ( "full path name of the class");
Object obj = c.newInstance ();
in this way spring to create an object, requires that the class be no-argument constructor, otherwise it can not be created by reflecting object, it will Throw an exception.

 

Spring of simple interest and more cases
Spring container-managed bean is a single case of default, that is, a bean will only create an object, there is a built-in map, followed regardless of obtaining the number of times the bean, returns the same object.

Spring single default manner, reduced to create an object, thereby reducing the memory consumption.
But in the actual development is the presence of multiple cases of demand, Spring also offers the option bean can be set to multi-pattern.

 

bean lifecycle in a single mode of interest:
bean in simple interest mode, xml parsing when spring starts container and found the bean tag directly create objects stored in the internal map of the bean saved, then no matter how many times getBean call () Gets the bean is to acquire the object is returned, it has been an object from the map. This object has been holding spring container until the container quit, quit with the object container is destroyed

bean lifecycle modes in Dolly:
bean in the multi-mode embodiment, the bean analytic xml tag found, but the management bean, spring does not create a container object is started, after each use the getBean () to obtain the when the bean, spring will return to re-create the object, every time a new object. This object spring container does not hold, when the destruction depends on when the user uses the object's own destruction of the object.

Lazy loading mechanism
spring default during container initialization, parsing xml, and created and saved to map the simple interest of bean, such a mechanism is not in question came from a relatively bean, but once the bean very long time, need to start spring the process takes a lot of time to create the bean, bean to spend a lot of storage space, but they do not have access bean may be a long time, resulting in a waste in time and space when started.

So spring provides a lazy loading mechanism, the so-called lazy loading mechanism is that you can immediately create provisions specified bean does not start, but only used once in the follow-up to create the first, so as to reduce the consumption of time and memory during startup .
Lazy loading mechanism only has an effect on singleton bean, bean setting for many cases of lazy loading does not make sense.
----------------
Disclaimer: This article is CSDN blogger "haló 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/weixin_43635271/article/details/86613083

Guess you like

Origin www.cnblogs.com/feng9exe/p/11835049.html