Skip to content

Commit

Permalink
I hope to implement a feature where the name of the Resource can be d…
Browse files Browse the repository at this point in the history
…ynamically configured during IoC, so that different view classes can be used at runtime. (close #754)
  • Loading branch information
goodjava committed Nov 18, 2023
1 parent 89cf871 commit 8f72ac7
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 2 deletions.
16 changes: 14 additions & 2 deletions jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Ioc.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.xiaomi.youpin.docean.test.demo.mydemo;

import com.xiaomi.youpin.docean.anno.Service;

import javax.annotation.Resource;

/**
* @author [email protected]
* @date 2023/11/18 14:52
*/


@Service
public class DemoCall {

@Resource(name = "$demoName")
private MyDemo demo;


public String hi() {
return demo.hi();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.xiaomi.youpin.docean.test.demo.mydemo;

/**
* @author [email protected]
* @date 2023/11/18 14:50
*/
public interface MyDemo {

String hi();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.xiaomi.youpin.docean.test.demo.mydemo;

import com.xiaomi.youpin.docean.anno.Component;

/**
* @author [email protected]
* @date 2023/11/18 14:50
*/
@Component
public class MyDemo1 implements MyDemo{
@Override
public String hi() {
return "demo1";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.xiaomi.youpin.docean.test.demo.mydemo;

import com.xiaomi.youpin.docean.anno.Component;

/**
* @author [email protected]
* @date 2023/11/18 14:50
*/
@Component
public class MyDemo2 implements MyDemo{
@Override
public String hi() {
return "demo2";
}
}

0 comments on commit 8f72ac7

Please sign in to comment.