Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I hope to implement a feature where the name of the Resource can be d… #755

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
}
}
Loading