From 5e3edb7e1d8ced497664964aa884c87ebd451c41 Mon Sep 17 00:00:00 2001 From: Redkale <8730487+redkale@users.noreply.github.com> Date: Wed, 9 Oct 2019 13:43:11 +0800 Subject: [PATCH] --- .../redkale/test/util/ResourceInjectMain.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 test/org/redkale/test/util/ResourceInjectMain.java diff --git a/test/org/redkale/test/util/ResourceInjectMain.java b/test/org/redkale/test/util/ResourceInjectMain.java new file mode 100644 index 000000000..8e8beab47 --- /dev/null +++ b/test/org/redkale/test/util/ResourceInjectMain.java @@ -0,0 +1,67 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.test.util; + +import java.io.File; +import java.lang.annotation.*; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.reflect.Field; +import org.redkale.convert.json.JsonConvert; +import org.redkale.util.*; + +/** + * + * @author zhangjx + */ +public class ResourceInjectMain { + + public static void main(String[] args) throws Throwable { + ResourceFactory factory = ResourceFactory.root(); + factory.register(new CustomConfLoader()); + InjectBean bean = new InjectBean(); + factory.inject(bean); + } + + public static class CustomConfLoader implements ResourceInjectLoader { + + @Override + public void load(ResourceFactory factory, Object src, CustomConf annotation, Field field, Object attachment) { + try { + field.set(src, new File(annotation.path())); + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println("对象是 src =" + src + ", path=" + annotation.path()); + } + + @Override + public Class annotationType() { + return CustomConf.class; + } + + } + + public static class InjectBean { + + @CustomConf(path = "conf/test.xml") + public File conf; + + @Override + public String toString() { + return JsonConvert.root().convertTo(this); + } + } + + @Documented + @Target({FIELD}) + @Retention(RUNTIME) + public static @interface CustomConf { + + String path(); + } + +}