ConstructorInjectionPoint ConstructornInjectionPoint是构造函数注入点,在类前添加[Inject]元数据标签就会申明构造函数注入点。 继承自MethodInjectionPoint。 ConstructornInjectionPoint主要方法如下: | | ConstructorInjectionPoint(node : XML, clazz : Class, injector : Injector = null) | 构造函数,注意Flash player在一个类至少被实例化一次之前不会给出type信息。会调用createDummyInstance方法假装实例一下这个类,然后将获得的类的XML描述进行更新。 | applyInjection(target : Object, injector : Injector) : Object | 应用注入,会将注入的参数手动的new到构造函数里,注意只能有10个参数,由于构造函数无法apply,所以只能手动的来new,具体见注释 | initializeInjection(node : XML) : void | 初始化注入点,会调用父类的gatherParamenters方法来寻找到所有的要注入的参数名 | createDummyInstance( constructorNode : XML, clazz : Class) | 尝试实例化一个类,并且更新类的XML描述信息,以解决不能得到type信息的bug |
ConstructornInjectionPoint类完整注释如下: - /*
- * Copyright (c) 2009 the original author or authors
- *
- * Permission is hereby granted to use, modify, and distribute this file
- * in accordance with the terms of the license agreement accompanying it.
- */
-
- package org.swiftsuspenders.injectionpoints
- {
- import flash.utils.describeType;
-
- import org.swiftsuspenders.Injector;
- /**
- * 构造函数注入点,扩展自MethodInjectionPoint
- * */
- public class ConstructorInjectionPoint extends MethodInjectionPoint
- {
- /*******************************************************************************************
- * public methods *
- *******************************************************************************************/
- public function ConstructorInjectionPoint(
- node : XML, clazz : Class, injector : Injector = null)
- {
- /*
- In many cases, the flash player doesn't give us type information for constructors until
- the class has been instantiated at least once. Therefore, we do just that if we don't get
- type information for at least one parameter.
- */
- /**
- * 在许多情况下,Flash player在一个类至少被实例化一次之前不会给出type信息。因此我们先假装实例一下从而来获得类型信息
- * */
- if (node.parameter.(@type == '*').length() == node.parameter.@type.length())//如果所有的参数类型都是*
- {
- createDummyInstance(node, clazz);//调用createDummyInstance函数,以解决FP的bug
- }
- super(node, injector);
- }
-
- override public function applyInjection(target : Object, injector : Injector) : Object
- {
- var ctor : Class = Class(target);//target是一个Class
- var p : Array = gatherParameterValues(target, injector);//获得构造函数的参数的值,调用父类方法
- //the only way to implement ctor injections, really!
- /**
- * 唯一实现构造函数注入的方法,所以只能少于10个参数的构造函数
- * */
- switch (p.length)
- {
- case 0 : return (new ctor());
- case 1 : return (new ctor(p[0]));
- case 2 : return (new ctor(p[0], p[1]));
- case 3 : return (new ctor(p[0], p[1], p[2]));
- case 4 : return (new ctor(p[0], p[1], p[2], p[3]));
- case 5 : return (new ctor(p[0], p[1], p[2], p[3], p[4]));
- case 6 : return (new ctor(p[0], p[1], p[2], p[3], p[4], p[5]));
- case 7 : return (new ctor(p[0], p[1], p[2], p[3], p[4], p[5], p[6]));
- case 8 : return (new ctor(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]));
- case 9 : return (new ctor(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8]));
- case 10 : return (new ctor(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9]));
- }
- return null;
- }
-
- /*******************************************************************************************
- * protected methods *
- *******************************************************************************************/
- override protected function initializeInjection(node : XML) : void
- {
- var nameArgs : XMLList = node.parent().metadata.(@name == 'Inject').arg.(@key == 'name');//获得包含参数的子节点
- methodName = 'constructor';//将方法名赋值为constructor,其实不赋值也没关系,因为构造函数输入用不到这个熟悉
-
- gatherParameters(node, nameArgs);//获取要注入的参数名
- }
-
- /*******************************************************************************************
- * private methods *
- *******************************************************************************************/
- /**
- * 为了防止无法获得构造函数的type信息,从而
- * */
- private function createDummyInstance(constructorNode : XML, clazz : Class) : void
- {
- try//尝试new一下这个类,如果不行则抛出异常
- {
- switch (constructorNode.children().length())
- {
- case 0 : (new clazz()); break;
- case 1 : (new clazz(null)); break;
- case 2 : (new clazz(null, null)); break;
- case 3 : (new clazz(null, null, null)); break;
- case 4 : (new clazz(null, null, null, null)); break;
- case 5 : (new clazz(null, null, null, null, null)); break;
- case 6 : (new clazz(null, null, null, null, null, null)); break;
- case 7 : (new clazz(null, null, null, null, null, null, null)); break;
- case 8 : (new clazz(null, null, null, null, null, null, null, null)); break;
- case 9 : (new clazz(null, null, null, null, null, null, null, null, null)); break;
- case 10 : (new clazz(null, null, null, null, null, null, null, null, null, null)); break;
- }
- }
- catch (error : Error)
- {
- trace('Exception caught while trying to create dummy instance for constructor ' +
- 'injection. It\'s almost certainly ok to ignore this exception, but you ' +
- 'might want to restructure your constructor to prevent errors from ' +
- 'happening. See the SwiftSuspenders documentation for more details. ' +
- 'The caught exception was:\n' + error);
- }//在new完之后,不论是否new成功,类的xml描述就可以获得了
- constructorNode.setChildren(describeType(clazz).factory.constructor[0].children());//重新设置XML信息
- }
- }
- }
复制代码 NoParamsConstructorInjectionPointNoParamsConstructornInjectionPoint是无参构造函数注入点,解决所有没有构造函数注入的类的实例化问题 NoParamsConstructornInjectionPoint主要方法如下: | | NoParamConstructorInjectionPoint | 构造函数,直接默认即可,因为就是为了实现new一个目标类的问题 | applyInjection(target : Object, injector : Injector) : Object | |
很简单,就不给出注释了,直接看代码即可。 PostConstructInjectionPointPostConstructInjectionPoint表示了那些完成注入后需要执行的方法,也将他们抽象为注入点进行处理,通过[PostConstruct]标记出的方法就会被SwiftSuspenders添加为PostConstrucInjectionPoint。 PostConstructInjectionPoint主要属性如下: | | | | | 调用的顺序,可能一个类会有多个方法被标记为PostConstruct。通过该值决定调用方法的顺序 |
PostConstructInjectionPoint主要方法如下: | | PostConstructInjectionPoint(node:XML, injector : Injector = null) | | applyInjection(target : Object, injector : Injector) : Object | 直接调用目标的该方法即可,注意申明为PostConstruct的方法都是无参数的方法 | initializeInjection(node : XML) | 初始化该注入点,通过获得order参数的值得到调用的顺序 | | |
PostConstructInjectionPoint主要方法如下: - /*
- * Copyright (c) 2009 the original author or authors
- *
- * Permission is hereby granted to use, modify, and distribute this file
- * in accordance with the terms of the license agreement accompanying it.
- */
-
- package org.swiftsuspenders.injectionpoints
- {
- import org.swiftsuspenders.Injector;
-
- /**
- * 实际不是注入点,通过配置PostConstructInjectionPoint,可以再完成注入后执行相应的函数,order代表执行顺序顺序
- * 如
- [PostConstruct(order="2")]
- public function post2():void{
- trace("post2");
- }
- [PostConstruct(order="1")]
- public function post1():void{
- trace("post1");
- }
- * 当类中有这两个函数时
- * 在该类完成注入时
- * 就会先执行post1,再执行post2
- * order决定了执行顺序
- * */
- public class PostConstructInjectionPoint extends InjectionPoint
- {
- /*******************************************************************************************
- * private properties *
- *******************************************************************************************/
- protected var methodName : String;//方法名
- protected var orderValue:int;//顺序
-
-
- /*******************************************************************************************
- * public methods *
- *******************************************************************************************/
- public function PostConstructInjectionPoint(node:XML, injector : Injector = null)
- {
- super(node, injector);
- }
-
- public function get order():int
- {
- return orderValue;
- }
-
- override public function applyInjection(target : Object, injector : Injector) : Object
- {
- target[methodName]();//调用target的methodName方法
- return target;
- }
-
-
- /*******************************************************************************************
- * protected methods *
- *******************************************************************************************/
- override protected function initializeInjection(node : XML) : void
- {
- var orderArg : XMLList = node.arg.(@key == 'order');//获取key是order节点
- var methodNode : XML = node.parent();//获取PostConstruct XML节点
- orderValue = int(orderArg.@value);//orderArg的value属性赋值给orderValue
- methodName = methodNode.@name.toString();//节点的name属性赋值给method,方法名
- }
- }
- }
复制代码
|