/** Adds object instance to the container as singleton bean. */ public void addBean(String name, Object bean, WiringMode wiringMode) { wiringMode = petiteConfig.resolveWiringMode(wiringMode); registerPetiteBean(bean.getClass(), name, SingletonScope.class, wiringMode, false); BeanDefinition def = lookupExistingBeanDefinition(name); Map<String, Object> acquiredBeans = new HashMap<String, Object>(); acquiredBeans.put(name, bean); wireBeanInjectParamsAndInvokeInitMethods(def, bean, acquiredBeans); def.scopeRegister(bean); }
/** * Creates and wires a bean within the container and optionally invokes init methods. However, * bean is <b>not</b> registered. */ @SuppressWarnings({"unchecked"}) public <E> E createBean(Class<E> type, WiringMode wiringMode) { wiringMode = petiteConfig.resolveWiringMode(wiringMode); BeanDefinition def = new BeanDefinition(null, type, null, wiringMode); Map<String, Object> acquiredBeans = new HashMap<String, Object>(); Object bean = newBeanInstance(def, acquiredBeans); wireBeanInjectParamsAndInvokeInitMethods(def, bean, acquiredBeans); return (E) bean; }
/** * Returns Petite bean instance. * * @see PetiteContainer#createBean(Class) */ protected Object getBean(String name, Map<String, Object> acquiredBeans) { // First check if bean is already acquired within this call. // This prevents cyclic dependencies problem. It is expected than single // object tree path contains less elements, therefore, this search is faster // then the next one. Object bean = acquiredBeans.get(name); if (bean != null) { if (bean == Void.TYPE) { throw new PetiteException("Cycle dependencies on constructor injection detected!"); } return bean; } // Lookup for registered bean definition. BeanDefinition def = lookupBeanDefinition(name); if (def == null) { // try provider ProviderDefinition providerDefinition = providers.get(name); if (providerDefinition != null) { return invokeProvider(providerDefinition); } return null; } // Find the bean in its scope bean = def.scopeLookup(); if (bean == null) { // Create new bean in the scope bean = newBeanInstance(def, acquiredBeans); wireBeanInjectParamsAndInvokeInitMethods(def, bean, acquiredBeans); def.scopeRegister(bean); } return bean; }
/** * Wires provided bean with the container and optionally invokes init methods. Bean is <b>not</b> * registered. */ public void wire(Object bean, WiringMode wiringMode) { wiringMode = petiteConfig.resolveWiringMode(wiringMode); BeanDefinition def = new BeanDefinition(null, bean.getClass(), null, wiringMode); Map<String, Object> acquiredBeans = new HashMap<String, Object>(); wireBeanInjectParamsAndInvokeInitMethods(def, bean, acquiredBeans); }