SwiftSuspenders 1.6 in-depth 2

Source: Internet
Author: User
Class description Injector

Injector is the core of the entire SwiftSuspenders framework. The Injector class that deals with developers the most. Injector is the Injector, and its function is equivalent to ApplicationContext in Spring. Is an IOC container. A simple understanding can be understood as a factory. When a developer needs an object, instead of a new one, the developer obtains the object through Injector. Injector automatically injects the dependencies of returned objects.

InjectorThe private attributes of are as follows::

Attribute name

Function

INJECTION_POINTS_CACHE [static]: Dictionary

A static attribute that caches the description of all injection points of all injection devices. If a injector uses xml to configure the injection point, it will not share the cache of the injection point with other injector. If the injection point is not configured using xml, all the injection devices share the cache of the injection point, this is also based on performance considerations. To analyze the injection points of a class, you must use the describeType method to parse the XML Description of the class. If the two injector maps the same class, you only need to parse it once.Note: This variable caches class injection points.(InjectionPoints)Information instead of injection Configuration(InjectionConfig). The injector does not share the injection configuration information. For exampleA1The injector has been mapped once.BClass, ifA2Injector re ingBClass, you can directly useA1The injector mapsBClass, instead of having to perform another operation. Because the injection points of the same class must be the same.

M_parentInjector: Injector

Reference of parent Injector

M_applicationDomain:

ApplicationDomain

Application domain in which the injector works

M_mappings: Dictionary

Store all injection configurations for this note (InjectionConfig)

M_injecteeDescriptions: Dictionary

Stores the injection points related to the injector. If you do not configure the injection point through xml, this attribute points to INJECTION_POINTS_CACHE, that is, all the injection devices share the injection point information of the class.

M_attendedToInjectees: Dictionary

Stores injected objects. In this way, the next request does not need to be injected. To improve performance.

M_xmlMetadata: XML

Configure the XML file for the injection information.

 

InjectorThe public method is as follows::

 

Method Name

Function

Injector (xmlConfig: XML = null)

The constructor initializes some private variables of the injector, IF configured through xml. Write in the parameter. In this way, the injector is set to the xml configuration mode.

MapValue (whenAskedFor: Class, useValue: Object, named: String = ""):*

Value ing. Map the full name of a request [class + # + injection name] to an existing object.

MapClass (whenAskedFor: Class, instantiateClass: Class, named: String = ""):*

Class ing: maps the full name of a request [class + # + injection name] to a class.Note that the ing class can be unrelated to the requested class.

MapSingleton (

WhenAskedFor: Class, named: String = ""):*

Singleton ing maps the full name of a request [class + # + injection name] to a singleton object of the class.

MapSingletonOf (

WhenAskedFor: Class, useSingletonOf: Class, named: String = ""):*

Singleton class ing maps the full name of one request [class + # + injection name] To the singleton object of another class.Note that the ing class can be unrelated to the requested class.

MapRule (whenAskedFor: Class, useRule: *, named: String = ""):*

Rule ing. Map the full name of a request [class + # + injection name] To the return policy of another injection configuration (I personally think this method does not work. It is better to set the second parameter to a custom InjectionResult. This also increases scalability)

GetMapping

(WhenAskedFor: Class, named: String = "")

Obtain the corresponding injection configuration according to the request. If the corresponding injection configuration already exists, it is directly returned from the mappings cache. If not, a new injection configuration will be created. And save it in mapping.

InjectInto (target: Object): void

Inject dependencies into the target object. (Only include property injection and method injection ). Constructor injection is completed when the target object is generated.

Instantiate (clazz: Class ):*

Instantiate a class. The class is created by using the constructor injection method, and then the injectInto method is called to complete the dependency injection of this object. Finally, the injected object is returned.

Unmap (clazz: Class, named: String = ""):*

Unbind A ing. Internally, the InjectionConfig Return Policy of the ing is set to null. (In my opinion, you should also delete the references in mapping ).

HasMapping (clazz: Class, named: String = ''): Boolean

InjectionConfig)

GetInstance (clazz: Class, named: String = ''):*

Return the corresponding instance according to the request. All dependency injection has been completed for this instance.

CreateChildInjector (applicationDomain: ApplicationDomain = null): Injector

Create a sub-Injector

SetApplicationDomain (applicationDomain: ApplicationDomain): void

Set the application domain of the injector

GetApplicationDomain (): ApplicationDomain

Get the application domain of the injector

SetParentInjector (parentInjector: Injector)

Set the parent Injector of the injector

GetParentInjector (): Injector

Obtain the parent Injector of the injector.

PurgeInjectionPointsCache ()

Clear the injection point cache.

 

The Injector package contains the following methods:

Method Name

Function

GetAncestorMapping (

WhenAskedFor: Class, named: String = null): InjectionConfig

The request injection configuration is obtained from the parent Injector in sequence from the bottom up. Until it is found.Note: Only the injection configuration closest to the current injector will be searched. For exampleAThe parent Injector of isB,BThe parent Injector of isC.BAndCThe injection configuration has the same request name. SoAOnly useB.

Get attendedToInjectees (): Dictionary

Obtain all existing objects.

 

The private method of Injector is as follows:

Method Name

Function

GetInjectionPoints (clazz: Class): InjecteeDescription

Obtains the InjecteeDescription of a class ). This injection description contains all the injection points of this class. Use the describeType method to obtain the XML Representation of this class. Then, find the node that contains the Inject metadata and PostConstruct metadata to configure the corresponding injection point. Finally, it is encapsulated into the InjecteeDescription object.

GetConfigurationForRequest (clazz: Class, named: String, traverseAncestors: Boolean = true): InjectionConfig

Return the corresponding InjectionConfig according to the request ). The traverseAncestors determines that it will be searched from the parent injector.

CreateInjectionPointsFromConfigXML (description: XML)

Configure the injection point information from xml. The general process is to first clear all metadata information in the original class, and then add new metadata information based on the configured xml.

AddParentInjectionPoints (description: XML, injectionPoints: Array): void

Find the injection point from the parent class and add it to the injection point description of the subclass. This is because subclass should inherit the injection point information of the parent class.

 

Injector package external class: InjecteeDescription

InjecteeDescription describes the injection point information of a class. It contains two public attributes:

Attribute name

Function

Ctor: InjectionPoint

Constructor injection point

InjectionPoints: Array

Attributes, methods, and PostConstruct injection points

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.