实例化Bean
/** * 一个简单的Beans工厂,实例化具体的Bean */ public class BeansFactory { private ConcurrentHashMap<String, Object> singletonObjects = new ConcurrentHashMap<>(); private ConcurrentHashMap<String, BeanDefinition> beanDefinitions = new ConcurrentHashMap<>(); @VisibleForTesting protected Object createBean(BeanDefinition beanDefinition) { // 类对象的实例 Object bean = null; try { // 根据字符串得到类 Class beanClass = Class.forName(beanDefinition.getClassName()); List<BeanDefinition.ConstructorArg> args = beanDefinition.getConstructorArgs(); if (args.isEmpty()) { bean = beanClass.newInstance(); } else { Class[] argClasses =