Spring’s IOC container

Spring is a lightweight java development framework. Its two basic functions are IOC and AOP. IOC is the inversion of control. The basic concept of IOC container is "serving others". What to serve others? The most important is the construction management of business objects and the dependency binding between business objects.

To manage business objects, the IOC container first needs to know the dependencies between business objects. There are several ways to tell the IOC container the binding relationship between the objects it manages:

Note: no matter what method is used to inform the IOC container of the binding relationship between objects, the information needs to be "written" to the IOC container through coding.

An example of XML configuration method:

Spring's IOC container provides two basic container types: beanfactory and ApplicationContext.

Beanfactory usage example (XML configuration method):

ApplicationContext usage example (XML configuration method):

Three ways of dependency injection

Bean configuration of spring (XML configuration mode)

Attribute injection is to inject the attribute value or dependent object of the bean through the setter method. Use the < property > element, use the name attribute to specify the attribute name of the bean, and the value attribute or < value > child node to specify the attribute value. Attribute injection is the most commonly used injection method in practical applications. The attribute injection bean class must have a default constructor.

Inject the attribute value or dependent object of the bean through the construction method, which ensures that the bean instance can be used after instantiation. The constructor injection declares the attribute in the < constructor Arg > element.

The beans that make up the application often need to cooperate with each other to complete the function of the application. To enable beans to access each other, you must specify a reference to beans in the bean configuration file. In the bean configuration file, you can specify a reference to beans for bean attributes or constructor parameters through the < ref > element or ref attribute. You can also include the declaration of a bean in the attribute or constructor. Such a bean is called an internal bean.

When a bean instance is only used for a specific attribute, it can be declared as an internal bean. The internal bean declaration is directly contained in the < property > or < constructor Arg > element without setting any ID or name attribute. Internal beans cannot be used anywhere else.

Collection properties can be configured through a set of built-in XML tags (for example: < list >, < set > or < Map >).

Scope of bean

Spring initially provided two bean scope types: singleton and prototype. After the release of 2.0, request, session and global session types were added, but these three new types can only be used in web applications. You can specify the scope type of the bean through the bean attribute scope. If it is a singleton type, the container will still take over the life cycle of the bean after the user obtains the bean; If it is a prototype, after the user obtains the bean, the container will not take over the bean, that is, the container will create a new bean object and return it to the user every time.

Create beans through static methods

Calling static factory methods to create beans encapsulates the process of object creation into static methods. When users need objects, they only need to simply call static methods, and they don't care about the details of creating objects. To declare a bean created by a static method, you need to specify the class that owns the method of the factory in the class attribute of the bean, and specify the name of the factory method in the factory method attribute Finally, use the < constctor Arg > element to pass method parameters for the method.

Create beans through instance methods

Encapsulate the creation process of an object into a method of another object instance When users need to request an object, they only need to simply call the instance method without paying attention to the creation details of the object. To declare a bean created through an instance factory method, specify the bean that owns the factory method in the factory bean attribute of the bean, specify the name of the factory method in the factory method attribute, and use the constructor Arg element to pass method parameters for the factory method.

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
分享
二维码
< <上一篇
下一篇>>