Spring's IoC framework

Meaning the container

In Spring inherits from ApplicationContext BeanFactory, in addition to providing a BeanFactory function, but also provided additional dependency management, messaging, lifecycle listener function and so on, it is the so-called "containers"

Bean definitions

Bean defines a container defined by BeanDefinition specific content comprises:

  • Fully qualified class name

  • Dependency class

  • Lifecycle callback class

  • Other attributes of the classes

bean named

  • Each may be named by a Bean id, a plurality of name; when necessary by the alias can also be defined alias

  • For the bean does not declare the name, spring container automatically generates a name for it, generated name follows the Java specification, the first letter lowercase class name, and then abide by the principles of camelCase

Bean initialization

As can be seen from the content of BeanDefinition, the bean class name is an indispensable element in the spring configuration, this property is defined by a class attribute value of the class attribute can be specified in three ways:

  • Directly designating the class name created by the container, this situation is somewhat similar to the object directly generate new operator

  • Generated by other classes classes corresponding a static factory method, in this case, the class name of the class is a static class methods, and the static method specified by the factory-method returns the object type of the object type actually generated.

As a rule, use the prototype scope for all stateful beans and the singleton scope for stateless beans.

  • Other classes generated by the method of the corresponding class, in this case, the class attribute value is empty, factory-bean name with an object generation process, factory-method is a method of generating a corresponding object names, and the real object defined type factory-method type was returned. this method is a static method of generating bean object class is substantially similar to a but the former method is achieved by a static class, and this is achieved by a method of the object.

Dependency Injection

In the context of the Spring, the dependency injection means generates a corresponding object in a container, the container according to the dependency relationship between the injection configuration information automatically bean, bean themselves and not by the corresponding management and maintenance practices dependencies . In the spring, can be done in two ways dependency injection, usually, are implanted that have dependencies in the constructor, and the method of using setter injection completion those optional dependencies.

1. Constructor

Completed using a constructor dependency injection and static class factory method with parameters used to achieve dependency injection is the same reason, so the description which follows, without further distinguish these two cases. Completed using a constructor dependency injection time required to complete the parameter type of resolution.

  • In the case where the type of ambiguity ten million not (i.e., all parameters are not the same type), the parameter is automatically supplied to the corresponding constructor according to the corresponding type, without the need to explicitly specify the position of the parameter

  • However, if you encounter can not be completed analytical type of automatic, you can specify the type of the parameter type attribute explicitly

  • Location parameters may be specified directly by the index parameter, this will be provided to provide a bean as the constructor argument constructor index

2. setter method

As the name suggests, the setter method is finished in the spring after the object is constructed (typically no argument constructor call class or static class methods without parameters generated), the completion of injection by the corresponding attribute set method.

Dependency injection configuration details

  • In the configuration file, the value of the attribute is always expressed by a string type, but in reality is not necessarily the string parameter type, the conversion between the two may be accomplished by ConversionService

  • P namespace can be easily implemented properties to assign

  • Idref label can be referenced by the corresponding bean, this reference so that the container can then check whether the object referenced in the real deployment time, rather than waiting until runtime checking again

Scope

  • Singleton : Each container single instance

  • prototype : prototype, create a new instance every request will; in this case a scope, only the container is responsible for initializing bean, once the initialization is complete, the vessel will no longer manage the bean, ongoing maintenance and other issues, including resource recovery , all maintained by the client itself, so that the object in terms of scope, its destructor will not be called and the container, if the client needs to perform these operations, consider using PostProcessor interface to complete.
    in addition, If injected prototype objects in the object singleton scope, this relationship is referenced during initialization on the introduction. this means that when you initialize the singleton object, an object prototype type of internal change will not happen ; because the initialization operation only happens once, if you need each time to obtain a new prototype objects at run time, you need to provide a spring 'method injection' way to solve

  • file application : Each servletContext one example, it differs in that it is a singleton servletContext range, and the range of the container in the singleton

  • Request : Each instance of a request

  • the session : Each instance of a session

  • globalSession, the WebSocket : the former is used PortletServlet, which is used webSocket, slightly

Original: Big Box  Spring's IoC framework


Guess you like

Origin www.cnblogs.com/petewell/p/11615079.html