コード例 #1
0
 private void injectBeansToComponents() {
   for (Class<?> c : componentsClasses) {
     try {
       Object object = c.newInstance();
       for (Field f : c.getDeclaredFields()) {
         if (f.isAnnotationPresent(Inject.class)) {
           f.setAccessible(true);
           Class<?> fieldClass = f.getType();
           BeanDefinition beanDefinition = findBeenByClass(fieldClass);
           f.set(object, beanDefinition.getInstance());
         }
       }
       preparedComponents.put(c.getName(), new SingletonBeanDefinition(c, object));
     } catch (InstantiationException | IllegalAccessException e) {
       e.printStackTrace();
     }
   }
 }
コード例 #2
0
 @Override
 public Object getComponent(Class<?> clazz) {
   Object objectToReturn = preparedComponents.get(clazz.getName()).getInstance();
   if (objectToReturn != null) {
     return objectToReturn;
   } else {
     for (BeanDefinition definition : preparedComponents.values()) {
       if (Arrays.asList(definition.getClazz().getInterfaces()).contains(clazz)) {
         if (objectToReturn == null) {
           objectToReturn = definition.getInstance();
         } else {
           throw new FailureComponentCreationException(
               "Can't resolve component to return, more than one component found, for class: "
                   + clazz.getName());
         }
       }
     }
     if (objectToReturn != null) {
       return objectToReturn;
     } else {
       throw new ComponentNotFoundException("Can't find component  for class: " + clazz.getName());
     }
   }
 }