简简单单的实现一个spring中的自动装配和容器管理的小骚操作。
1,创建AutoSetBean.java
使用injectBeans静态方法,可以扫描指定包下的所有带MyInject注解的字段,如果在beans的Map中存在这个字段的实例化类,则执行装配。
import java.lang.reflect.Field;
import java.util.Set;
import org.example.test.custominj.myAnno.MyInject;
import org.reflections.Reflections;
import org.reflections.scanners.Scanners;
import org.reflections.util.ConfigurationBuilder;public class AutoSetBean {public static void injectBeans(String packageName) {// 使用Reflections库来扫描指定包名下的所有类Reflections reflections = new Reflections(new ConfigurationBuilder().forPackages(packageName).addScanners(Scanners.FieldsAnnotated));// 获取所有带有@MyInject注解的字段Set<Field> fields = reflections.getFieldsAnnotatedWith(MyInject.class);for (Field field : fields) {try {// 获取字段所在的类Class<?> declaringClass = field.getDeclaringClass();// 设置字段可访问field.setAccessible(true);// 如果MyBean.beans中已经存在这个类的实例,则直接使用Object instance = MyBean.beans.get(declaringClass);if (instance == null) {// 如果不存在这个类的实例,则创建一个新的实例instance = declaringClass.getDeclaredConstructor().newInstance();MyBean.beans.put(declaringClass, instance);}// 获取需要注入的字段的类型Class<?> fieldType = field.getType();// 从MyBean.beans中获取该类型的实例Object beanToInject = MyBean.beans.get(fieldType);if (beanToInject != null) {// 注入实例field.set(instance, beanToInject);}} catch (Exception e) {e.printStackTrace();}}}
}
2, 创建MyBean.java
通过使用registerBean静态方法,扫描传入的包名,检查其是否有@MyRegister注解,并将所有有此注解的类实例化后装入beans中,以备后续的装配。
import org.example.test.custominj.myAnno.MyRegister;
import org.reflections.Reflections;
import org.reflections.scanners.Scanners;
import org.reflections.util.ConfigurationBuilder;import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class MyBean {public static Map<Class<?>, Object> beans = new HashMap<>();public static void registBean(String packageName) {// 配置Reflections库Reflections reflections = new Reflections(new ConfigurationBuilder().forPackages(packageName).addScanners(Scanners.TypesAnnotated));// 获取所有带有@MyRegister注解的类Set<Class<?>> annotatedClasses = reflections.getTypesAnnotatedWith(MyRegister.class);for (Class<?> clazz : annotatedClasses) {try {// 实例化对象并放入beans Map中beans.put(clazz, clazz.getDeclaredConstructor().newInstance());} catch (Exception e) {e.printStackTrace();}}}
}
3, 创建MyInject.java 和MyRegister注解
配置两个自定义注解,用于标识是否执行装配。
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyInject {
}import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.RUNTIME)
public @interface MyRegister {
}
4,创建测试的Client.java 和被注入的MyService.java
@MyRegister作用于类上,注释后可以将这个类放入beans中,
@MyInject作用于字段,可以实现从容器中找到特定类型的实例化对象后执行装配。
import org.example.test.custominj.myAnno.MyInject;
import org.example.test.custominj.myAnno.MyRegister;@MyRegister
public class Client {@MyInjectprivate MyService myService;public void doSomething() {myService.doSomething();}
}import org.example.test.custominj.myAnno.MyRegister;@MyRegister
public class MyService {public void doSomething() {System.out.println("do something");}
}
5,创建测试类
@Test 测试自定义的自动装配
@Testpublic void test4() {MyBean.registBean("org.example.test.myinjtest");// 打印已注册的beansfor (Map.Entry<Class<?>, Object> entry : MyBean.beans.entrySet()) {System.out.println(entry.getKey() + ": " + entry.getValue());}AutoSetBean.injectBeans("org.example.test.myinjtest");client = (Client) MyBean.beans.get(Client.class);client.doSomething();}
执行结果,成功执行Service下的dosomething方法,成功打印!