模块 org.redkale

接口 ResourceAnnotationLoader<T extends Annotation>

  • 类型参数:
    T - Annotation

    public interface ResourceAnnotationLoader<T extends Annotation>
    自定义注入加载器
    
     @Documented
     @Target({FIELD})
     @Retention(RUNTIME)
     public @interface CustomConf {
         String path();
     }
    
    
      public class CustomConfAnnotationLoader implements ResourceAnnotationLoader<CustomConf> {
    
          @Override
          public void load(
                  ResourceFactory factory,
                  String srcResourceName,
                  Object srcObj,
                  CustomConf annotation,
                  Field field,
                  Object attachment) {
              try {
                  field.set(srcObj, new File(annotation.path()));
              } catch (Exception e) {
                  e.printStackTrace();
              }
              System.out.println("对象是 src =" + srcObj + ", path=" + annotation.path());
          }
    
          @Override
          public Class<CustomConf> annotationType() {
              return CustomConf.class;
          }
      }
    
    
      public class InjectBean {
    
          @CustomConf(path = "conf/test.xml")
          public File conf;
      }
    
    
      ResourceFactory factory = ResourceFactory.create();
      factory.register(new CustomConfAnnotationLoader());
      InjectBean bean = new InjectBean();
      factory.inject(bean);
    
    
     

    详情见: https://redkale.org

    从以下版本开始:
    2.8.0
    作者:
    zhangjx
    • 方法详细资料

      • load

        void load​(ResourceFactory factory,
                  String srcResourceName,
                  Object srcObj,
                  T annotation,
                  Field field,
                  Object attachment)
        自定义的对象注入
        参数:
        factory - ResourceFactory
        srcResourceName - 依附对象的资源名
        srcObj - 资源依附对象
        annotation - 注解
        field - 字段对象
        attachment - 附加对象
      • annotationType

        Class<T> annotationType()
        注入加载器对应的注解类型
        返回:
        类型