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

Can the Doc container framework support more elegant injection methods #757

Merged
merged 3 commits 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: 16 additions & 0 deletions jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Ioc.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,18 @@ public void removeBean(String name) {
private void initIoc0(String name, Bean bean, Field field) {
String realName = getRealName(name);
Bean b = this.beans.get(realName);

//If it is an implemented interface, check whether a unique implementation class can be matched.
if (!Optional.ofNullable(b).isPresent() && getBean(Cons.AUTO_FIND_IMPL, "false").equals("true")) {
Class clazz = field.getType();
if (clazz.isInterface()) {
Set<Bean> set = getBeanSet(clazz);
if (set.size() == 1) {
b = set.toArray(new Bean[]{})[0];
}
}
}

Optional.ofNullable(b).ifPresent(o -> {
o.incrReferenceCnt();
o.getDependenceList().add(bean.getName());
Expand Down Expand Up @@ -513,6 +525,10 @@ public <T> Set<T> getBeans(Class<T> clazz) {
return beans.values().stream().filter(it -> (clazz.isAssignableFrom(it.getClazz()))).map(it -> (T) it.getObj()).collect(Collectors.toSet());
}

public Set<Bean> getBeanSet(Class<?> clazz) {
return beans.values().stream().filter(it -> (clazz.isAssignableFrom(it.getClazz()))).collect(Collectors.toSet());
}

/**
* Get a list of beans by type
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public abstract class Cons {
public static final String WebSocketPath = "/ws";

public static final String Service = "/service";

public static final String AUTO_FIND_IMPL = "$autoFindImpl";
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.xiaomi.youpin.docean.Ioc;
import com.xiaomi.youpin.docean.aop.EnhanceInterceptor;
import com.xiaomi.youpin.docean.bo.Bean;
import com.xiaomi.youpin.docean.common.Cons;
import com.xiaomi.youpin.docean.common.Safe;
import com.xiaomi.youpin.docean.listener.event.EventType;
import com.xiaomi.youpin.docean.test.anno.TAnno;
Expand Down Expand Up @@ -145,12 +146,27 @@ public void testIoc6() {

@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");

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 testIoc8() {
Ioc ioc = Ioc.ins()
.putBean(Cons.AUTO_FIND_IMPL, "true")
.init("com.xiaomi.youpin.docean.test.demo.mydemo");
DemoCall dc = ioc.getBean(DemoCall.class);
System.out.println(dc.call());
}



@Test
public void testIoc44() {
Ioc.ins().init("com.xiaomi.youpin.docean", "run.mone");
Expand All @@ -165,12 +181,12 @@ public void testIoc44() {
@Test
public void testLookup() {
Aop.ins().init(Maps.newLinkedHashMap());
Ioc.ins().init("com.xiaomi.youpin.docean.test","com.xiaomi.youpin.docean.plugin.config");
Ioc.ins().init("com.xiaomi.youpin.docean.test", "com.xiaomi.youpin.docean.plugin.config");
DemoService ds = Ioc.ins().getBean(DemoService.class);
IntStream.range(0, 5).forEach(i -> {
DemoVo dv = ds.demoVo();
System.out.println(dv.getId());
dv.setId(System.currentTimeMillis()+"");
dv.setId(System.currentTimeMillis() + "");
System.out.println(ds.demoVo());
});
}
Expand Down
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/20 10:33
*/
@Component
public class CallImpl implements ICall{
@Override
public String call() {
return "call";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,21 @@ public class DemoCall {
private MyDemo demo;


//This is just an interface, but if it only has one implementation class, then Ioc will automatically find this unique implementation class.
@Resource
private ICall call;



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



public String call() {
return call.call();
}


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

/**
* @author [email protected]
* @date 2023/11/20 10:33
*/
public interface ICall {


String call();

}
Loading