模块 org.redkale

接口 ResourceTypeLoader


  • public interface ResourceTypeLoader
    自定义注入加载器
    
      public class CustomTypeLoader implements ResourceTypeLoader {
    
          @Override
          public Object load(
                  ResourceFactory factory,
                  String srcResourceName,
                  Object srcObj,
                  CustomConf annotation,
                  Field field,
                  Object attachment) {
              DataSource source = new DataMemorySource(resourceName);
              factory.register(resourceName, DataSource.class, source);
              if (field != null) {
                  try {
                      field.set(srcObj, source);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
              return source;
          }
    
          @Override
          public Type resourceType() {
              return DataSource.class;
          }
      }
    
    
      public class InjectBean {
    
          @Resource(name = "platf")
          public DataSource source;
      }
    
    
      ResourceFactory factory = ResourceFactory.create();
      factory.register(new CustomTypeLoader());
      InjectBean bean = new InjectBean();
      factory.inject(bean);
    
    
     

    详情见: https://redkale.org

    作者:
    zhangjx
    • 方法详细资料

      • load

        Object load​(ResourceFactory factory,
                    @Nullable
                    String srcResourceName,
                    @Nullable
                    Object srcObj,
                    String resourceName,
                    @Nullable
                    Field field,
                    @Nullable
                    Object attachment)
        自定义的对象注入, 实现需要兼容Field为null的情况
        参数:
        factory - ResourceFactory
        srcResourceName - 依附对象的资源名
        srcObj - 资源依附对象
        resourceName - 资源名
        field - 字段对象
        attachment - 附加对象
        返回:
        Object
      • resourceType

        Type resourceType()
        注入加载器对应的类型
        返回:
        类型
      • autoNone

        default boolean autoNone()
        是否注入默认值null
        返回true: 表示调用ResourceLoader之后资源仍不存在,则会在ResourceFactory里注入默认值null。
        返回false: 表示资源不存在下次仍会调用ResourceTypeLoader自行处理。
        返回:
        是否注入默认值null