diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Ioc.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Ioc.java index 318ae73b0..f60edb17b 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Ioc.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Ioc.java @@ -305,7 +305,8 @@ public void removeBean(String name) { private void initIoc0(String name, Bean bean, Field field) { - Bean b = this.beans.get(name); + String realName = getRealName(name); + Bean b = this.beans.get(realName); Optional.ofNullable(b).ifPresent(o -> { o.incrReferenceCnt(); o.getDependenceList().add(bean.getName()); @@ -315,11 +316,22 @@ private void initIoc0(String name, Bean bean, Field field) { //If there is a parent container, try to retrieve it from the parent container (such as Spring). if (!Optional.ofNullable(b).isPresent()) { - Object obj = Safe.callAndLog(()-> this.contextFunction.apply(name),null); + Object obj = Safe.callAndLog(() -> this.contextFunction.apply(realName), null); Optional.ofNullable(obj).ifPresent(o -> ReflectUtils.setField(bean.getObj(), field, o)); } } + private String getRealName(String name) { + //替换成配置中的值 + if (name.startsWith("$")) { + Bean bean = this.beans.get(name); + if (null != bean) { + return bean.getObj().toString(); + } + } + return name; + } + private void callInit(Bean it) { Stopwatch sw = Stopwatch.createStarted(); String methodName = Plugin.ins().getInitMethodName(it.getObj(), it.getClazz()); diff --git a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/IocTest.java b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/IocTest.java index cb7ca5675..df77b57ba 100644 --- a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/IocTest.java +++ b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/IocTest.java @@ -30,6 +30,7 @@ import com.xiaomi.youpin.docean.test.demo.DemoA; import com.xiaomi.youpin.docean.test.demo.DemoService; import com.xiaomi.youpin.docean.test.demo.DemoVo; +import com.xiaomi.youpin.docean.test.demo.mydemo.DemoCall; import com.xiaomi.youpin.docean.test.demo3.ControllerDemo; import com.xiaomi.youpin.docean.test.demo3.DaoDemo; import com.xiaomi.youpin.docean.test.demo3.ServiceDemo; @@ -142,6 +143,13 @@ public void testIoc6() { System.out.println(list); } + @Test + public void testIoc7() { + Ioc ioc = Ioc.ins().putBean("$demoName", "com.xiaomi.youpin.docean.test.demo.mydemo.MyDemo1").init("com.xiaomi.youpin.docean.test.demo.mydemo"); + DemoCall dc = ioc.getBean(DemoCall.class); + System.out.println(dc.hi()); + } + @Test public void testIoc44() { diff --git a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/DemoCall.java b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/DemoCall.java new file mode 100644 index 000000000..db675c923 --- /dev/null +++ b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/DemoCall.java @@ -0,0 +1,25 @@ +package com.xiaomi.youpin.docean.test.demo.mydemo; + +import com.xiaomi.youpin.docean.anno.Service; + +import javax.annotation.Resource; + +/** + * @author goodjava@qq.com + * @date 2023/11/18 14:52 + */ + + +@Service +public class DemoCall { + + @Resource(name = "$demoName") + private MyDemo demo; + + + public String hi() { + return demo.hi(); + } + + +} diff --git a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/MyDemo.java b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/MyDemo.java new file mode 100644 index 000000000..dd45b21e1 --- /dev/null +++ b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/MyDemo.java @@ -0,0 +1,11 @@ +package com.xiaomi.youpin.docean.test.demo.mydemo; + +/** + * @author goodjava@qq.com + * @date 2023/11/18 14:50 + */ +public interface MyDemo { + + String hi(); + +} diff --git a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/MyDemo1.java b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/MyDemo1.java new file mode 100644 index 000000000..600e167cf --- /dev/null +++ b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/MyDemo1.java @@ -0,0 +1,15 @@ +package com.xiaomi.youpin.docean.test.demo.mydemo; + +import com.xiaomi.youpin.docean.anno.Component; + +/** + * @author goodjava@qq.com + * @date 2023/11/18 14:50 + */ +@Component +public class MyDemo1 implements MyDemo{ + @Override + public String hi() { + return "demo1"; + } +} diff --git a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/MyDemo2.java b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/MyDemo2.java new file mode 100644 index 000000000..ce8dabb91 --- /dev/null +++ b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/MyDemo2.java @@ -0,0 +1,15 @@ +package com.xiaomi.youpin.docean.test.demo.mydemo; + +import com.xiaomi.youpin.docean.anno.Component; + +/** + * @author goodjava@qq.com + * @date 2023/11/18 14:50 + */ +@Component +public class MyDemo2 implements MyDemo{ + @Override + public String hi() { + return "demo2"; + } +}