protected static void autowired( AbstractServicer ser, ApplicationContext appContext, DatabaseHandler dbhandler) { Collection<Field> fields = ReflectUtils.getDeclaredFields((Map<String, Field>) null, ser.getClass(), false).values(); for (Field field : fields) { if (!field.isAnnotationPresent(Autowired.class)) { continue; } String fieldName = field.getName(); try { Method setter = ReflectUtils.findSetter(ser, field, null, null); Object autoInstance = null; try { autoInstance = appContext.getBean(fieldName); } catch (BeansException be) { logger.log(Level.SEVERE, null, be); autoInstance = null; } if (autoInstance == null) { try { Class<?> clazz = ReflectUtils.classForName(field.getType().getName()); autoInstance = clazz.newInstance(); ReflectUtils.updateFieldValue( autoInstance, clazz.getField("databaseHandler"), null, dbhandler); } catch (ClassNotFoundException | InstantiationException | BeansException | NoSuchFieldException | SecurityException e) { logger.log(Level.SEVERE, null, e); autoInstance = null; } } ReflectUtils.updateFieldValue(ser, field, setter, autoInstance); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { logger.log(Level.SEVERE, null, fieldName); logger.log(Level.SEVERE, null, e); } } }
public static AbstractServicer getService( HttpSession session, String className, String instanceName, ApplicationContext appContext, String spring, AbstractController worker, DatabaseHandler dbhandler) throws ClassNotFoundException, InstantiationException, IllegalAccessException { String servicerId = getServicerID(session, className, instanceName, worker); Object servicer = session.getAttribute(servicerId); String date = buildDateString(); if (servicer != null && servicer instanceof AbstractServicer) { newServiceInstanceIds.remove(date + "_" + servicerId); return (AbstractServicer) servicer; } else { Class<?> clazz = ReflectUtils.classForName(className); Object newInstance = clazz.newInstance(); autowired((AbstractServicer) newInstance, appContext, dbhandler); session.setAttribute(servicerId, newInstance); newServiceInstanceIds.add(date + "_" + servicerId); return (AbstractServicer) newInstance; } }