目前在写一个 mvc 框架,IoC 部分自己写的不满意,使用了 Guice,在做整合的时候,因为想实现类似于 SpringBoot 那样开箱即用的方式,所以打算实现两个功能:
- 自动扫描
- 自动配置
目前出现了问题:
首先有两个 bean 类
@Singleton public class TestBean { private int a =1; private int b =1; @Override public String toString() { return "TestBean{" + "a=" + a + ", b=" + b + '}'; } }
@Singleton public class TestBean2 { @Inject private TestBean testBean; public void say() { System.out.println(testBean+"111111"); } }
然后通过
Injector injector = Guice.createInjector(new AutoScanModule(reflections, scanAnnotationSet));
AutoScanModule 是配置自动扫描的部分,这部分会将标注了 Singleton
注解的类进行扫描,然后将其注册到 Guice 容器中,代码如下:
public class AutoScanModule extends AbstractModule { private final Reflections packageReflections; private final Set<Class<? extends Annotation>> bindingAnnotations; public AutoScanModule(Reflections packageReflections, final Set<Class<? extends Annotation>> bindingAnnotations) { this.packageReflections = packageReflections; this.bindingAnnotations = bindingAnnotations; } @Override public void configure() { this.bindingAnnotations.stream() .map(getClassSetFunction()) .flatMap(Set::stream) .forEach(this::bind); } private Function<Class<? extends Annotation>, Set<Class<?>>> getClassSetFunction() { return bindingAnnotation -> packageReflections.getTypesAnnotatedWith(bindingAnnotation, true); } }
然后就可以通过如下代码获取装配好的 TestBean2 对象,并且调用 say()方法后,可以看到打印结果,testBean2 也不是 null
Injector injector = Guice.createInjector(new AutoScanModule(reflections, scanAnnotationSet)); Provider<TestBean2> provider1 = injector.getProvider(TestBean2.class); provider1.get().say(); //TestBean{a=1, b=1}111111 console打印结果
然后我准备做的就是自动装配,也就是不用通过 injector.getProvider
或 injector.getInstance
的方式来获取,而是
想将标注了 Singleton
注解的类获取到所有的 Field,再根据是否标注了 Inject
注解,然后根据 Field Type 从 Guice 的容器中进行获取后再通过反射赋值
public void start() throws IllegalAccessException, InstantiationException { Injector injector = Guice.createInjector(new AutoScanModule(reflections, scanAnnotationSet)); for (Class<? extends Annotation> annotationCls : scanAnnotationSet) { this.typesAnnotatedWith.addAll(this.reflections.getTypesAnnotatedWith(annotationCls, true)); } for (Class<?> typeClass : typesAnnotatedWith) { Field[] fields = typeClass.getDeclaredFields(); if (fields.length == 0) { continue; } for (Field field : fields) { if (field.getAnnotation(javax.inject.Inject.class) != null || field.getAnnotation(com.google.inject.Inject.class) != null) { Object type = typeClass.newInstance(); field.setAccessible(true); Provider<?> provider = injector.getProvider(field.getType());//这里从Guice中根据Type拿到 if (provider != null && provider.get() != null) { field.set(type, provider.get());//然后通过反射Set } System.out.println(field.get(type)); System.out.println(field); } } if (typeClass.getTypeName().equals(TestBean2.class.getName())){ TestBean2 testBean2 = (TestBean2) typeClass.newInstance(); testBean2.say(); } } }
打印结果如下:
TestBean{a=1, b=1}
private org.aquiver.bean.TestBean org.aquiver.bean.TestBean2.testBean
null111111
可以看到这两句都不为 null
System.out.println(field.get(type)); System.out.println(field);
但是调用 say 方法的时候,显示为 null111111
TestBean2 testBean2 = (TestBean2) typeClass.newInstance(); testBean2.say();
请问这我测试方式有问题? 还是 newInstance 会重现创建个对象,所以为 null,如果是?那我该换什么方式来做呢?
我通过反射调用 say 方法还是为 field 还是为 null
Method say = typeClass.getMethod("say"); say.invoke(typeClass.newInstance());