List of usage examples for org.springframework.beans.factory.config BeanDefinition getFactoryBeanName
@Nullable String getFactoryBeanName();
From source file:me.springframework.di.spring.SpringConfigurationLoader.java
/** * Loads a {@link MutableInstance} from one of the {@link BeanDefinition}s * provided by the {@link BeanDefinitionRegistry} passed in. * // w w w .j a va 2 s.c om * @param instance * A {@link MutableInstance} to be populated. * @param definition * A {@link BeanDefinition}, providing the meta data. */ private static void load(MutableInstance instance, BeanDefinition definition, MutableContext context) { instance.setReferencedType(definition.getBeanClassName()); instance.setPrimitive(false); instance.setLazyInit(definition.isLazyInit()); instance.setId("source" + counter++); instance.setFactoryMethod(definition.getFactoryMethodName()); instance.setFactoryInstance(definition.getFactoryBeanName()); if (ConfigurableBeanFactory.SCOPE_SINGLETON.equals(definition.getScope())) { instance.setScope(Scope.SINGLETON); } if (ConfigurableBeanFactory.SCOPE_PROTOTYPE.equals(definition.getScope())) { instance.setScope(Scope.PROTOTYPE); } if (definition instanceof AbstractBeanDefinition) { instance.setInitMethod(((AbstractBeanDefinition) definition).getInitMethodName()); instance.setDestroyMethod(((AbstractBeanDefinition) definition).getDestroyMethodName()); } if (!definition.getConstructorArgumentValues().isEmpty()) { List<MutableConstructorArgument> arguments = new ArrayList<MutableConstructorArgument>(); for (Object object : definition.getConstructorArgumentValues().getGenericArgumentValues()) { MutableConstructorArgument argument = new MutableConstructorArgument(instance); argument.setInstance(instance); ValueHolder holder = (ValueHolder) object; argument.setSource(loadSource(context, argument, holder.getValue())); argument.setType(holder.getType()); arguments.add(argument); } instance.setConstructorArguments(arguments); } Set<MutablePropertySetter> setters = new HashSet<MutablePropertySetter>(); for (Object object : definition.getPropertyValues().getPropertyValueList()) { MutablePropertySetter setter = new MutablePropertySetter(instance); setter.setInstance(instance); PropertyValue value = (PropertyValue) object; setter.setName(value.getName()); setter.setSource(loadSource(context, setter, value.getValue())); setters.add(setter); } instance.setSetters(setters); // added by woj instance.setAutowireCandidate(definition.isAutowireCandidate()); if (definition instanceof AbstractBeanDefinition) { instance.setAutowireMode(((AbstractBeanDefinition) definition).getResolvedAutowireMode()); } else { instance.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_NO); } }
From source file:com.tacitknowledge.flip.spring.config.FeatureServiceHandlerParserTest.java
@Test public void testCreateFeatureServiceBean() { assertNotNull(context.containsBeanDefinition("featureService")); BeanDefinition beanDefinition = context.getBeanDefinition("featureService"); assertEquals("featureServiceFactory", beanDefinition.getFactoryBeanName()); assertEquals("createFeatureService", beanDefinition.getFactoryMethodName()); FeatureService featureService = context.getBean("featureService", FeatureService.class); assertNotNull(featureService);// w ww . ja va 2s . c o m }
From source file:lodsve.core.condition.BeanTypeRegistry.java
private Class<?> doGetFactoryBeanGeneric(ConfigurableListableBeanFactory beanFactory, BeanDefinition definition, String name) throws Exception, ClassNotFoundException, LinkageError { if (StringUtils.hasLength(definition.getFactoryBeanName()) && StringUtils.hasLength(definition.getFactoryMethodName())) { return getConfigurationClassFactoryBeanGeneric(beanFactory, definition, name); }//from w w w . jav a 2s.co m if (StringUtils.hasLength(definition.getBeanClassName())) { return getDirectFactoryBeanGeneric(beanFactory, definition, name); } return null; }
From source file:lodsve.core.condition.BeanTypeRegistry.java
private Method getFactoryMethod(ConfigurableListableBeanFactory beanFactory, BeanDefinition definition) throws Exception { if (definition instanceof AnnotatedBeanDefinition) { MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) definition) .getFactoryMethodMetadata(); if (factoryMethodMetadata instanceof StandardMethodMetadata) { return ((StandardMethodMetadata) factoryMethodMetadata).getIntrospectedMethod(); }/*from w w w .j a v a 2s . c o m*/ } BeanDefinition factoryDefinition = beanFactory.getBeanDefinition(definition.getFactoryBeanName()); Class<?> factoryClass = ClassUtils.forName(factoryDefinition.getBeanClassName(), beanFactory.getBeanClassLoader()); return ReflectionUtils.findMethod(factoryClass, definition.getFactoryMethodName()); }
From source file:org.opentestsystem.shared.test.listener.LifecycleResourceCombiner.java
private LifecycleResource[] getSortedResources() { // Build a doubly-linked tree of all Spring dependencies. final ApplicationContext context = getApplicationContext(); final BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context; final Map<String, BeanHolder> beanHolderMap = new HashMap<String, BeanHolder>(); for (String key_i : registry.getBeanDefinitionNames()) { final BeanDefinition definition_i = registry.getBeanDefinition(key_i); // Make sure that there is a holder for this bean in the map. // The holder may exist already if this bean's name has appeared as a // dependency for another bean. BeanHolder beanHolder = beanHolderMap.get(key_i); if (beanHolder == null) { beanHolder = new BeanHolder(key_i); beanHolderMap.put(key_i, beanHolder); }/*from w w w . j a v a2s . co m*/ beanHolder.bean = context.getBean(key_i); // Add the constructor arguments as antecedents BeanHolder antecedentHolder = null; String antecedentBeanName = null; for (final ValueHolder value_j : definition_i.getConstructorArgumentValues() .getGenericArgumentValues()) { final Object antecedentValue = value_j.getValue(); if (antecedentValue instanceof RuntimeBeanReference) { antecedentBeanName = ((RuntimeBeanReference) antecedentValue).getBeanName(); antecedentHolder = beanHolderMap.get(antecedentBeanName); if (antecedentHolder == null) { antecedentHolder = new BeanHolder(antecedentBeanName); beanHolderMap.put(antecedentBeanName, antecedentHolder); } beanHolder.beansIDependOn.add(antecedentHolder); antecedentHolder.beansWhoDependOnMe.add(beanHolder); } } // Add any factory bean as antecedent antecedentBeanName = definition_i.getFactoryBeanName(); if (!StringUtils.isBlank(antecedentBeanName)) { antecedentHolder = beanHolderMap.get(antecedentBeanName); if (antecedentHolder == null) { antecedentHolder = new BeanHolder(antecedentBeanName); beanHolderMap.put(antecedentBeanName, antecedentHolder); } beanHolder.beansIDependOn.add(antecedentHolder); antecedentHolder.beansWhoDependOnMe.add(beanHolder); } // Add the properties as antecedents if (definition_i.getPropertyValues() != null && definition_i.getPropertyValues().getPropertyValueList() != null) { for (final PropertyValue value_j : definition_i.getPropertyValues().getPropertyValueList()) { final Object antecedentValue = value_j.getValue(); if (antecedentValue instanceof RuntimeBeanReference) { antecedentBeanName = ((RuntimeBeanReference) antecedentValue).getBeanName(); antecedentHolder = beanHolderMap.get(antecedentBeanName); if (antecedentHolder == null) { antecedentHolder = new BeanHolder(antecedentBeanName); beanHolderMap.put(antecedentBeanName, antecedentHolder); } beanHolder.beansIDependOn.add(antecedentHolder); antecedentHolder.beansWhoDependOnMe.add(beanHolder); } } } // Add explicitly identified antecedents if (definition_i.getDependsOn() != null) { for (String name_j : definition_i.getDependsOn()) { antecedentBeanName = name_j; antecedentHolder = beanHolderMap.get(antecedentBeanName); if (antecedentHolder == null) { antecedentHolder = new BeanHolder(antecedentBeanName); beanHolderMap.put(antecedentBeanName, antecedentHolder); } beanHolder.beansIDependOn.add(antecedentHolder); antecedentHolder.beansWhoDependOnMe.add(beanHolder); } } } // Prune the tree of all items that aren't LifecycleResource // This step is easier with a stable ordering, so we copy things into a // list. List<BeanHolder> beanHolderList = new ArrayList<BeanHolder>(beanHolderMap.values()); int i = 0; while (i < beanHolderList.size()) { final BeanHolder beanHolder_i = beanHolderList.get(i); if (beanHolder_i.bean instanceof LifecycleResource) { i++; } else { for (final BeanHolder antecedent_j : beanHolder_i.beansIDependOn) { for (final BeanHolder dependent_k : beanHolder_i.beansWhoDependOnMe) { dependent_k.beansIDependOn.add(antecedent_j); antecedent_j.beansWhoDependOnMe.add(dependent_k); } antecedent_j.beansWhoDependOnMe.remove(beanHolder_i); } for (final BeanHolder dependent_j : beanHolder_i.beansWhoDependOnMe) { dependent_j.beansIDependOn.remove(beanHolder_i); } beanHolderList.remove(i); } } // Now do the topo sort. // // We cycle through the list repeatedly. Each time, we can pull out anyone // who doesn't have a dependency on anyone else who is still in the list. // This leads to everyone being pulled out before their dependencies. // // If at the end we still have people in the list who depend on each // other, we have a dependency cycle. i = 0; boolean found = true; int n = beanHolderList.size(); LifecycleResource[] ans = new LifecycleResource[n]; StringBuilder message = new StringBuilder( "Resolved sequence of LifecycleResource elements in Spring context:\r\n "); while (i < n) { if (!found) { // We got all the way through the list without finding any with no // antecedents. There is a cycle StringBuilder beanNames = new StringBuilder(); for (BeanHolder beanHolder_k : beanHolderList) { beanNames.append(beanHolder_k.beanName).append(" depends on ( "); for (BeanHolder beanHolder_l : beanHolder_k.beansIDependOn) { beanNames.append(beanHolder_l.beanName).append(" "); } beanNames.append(")\r\n"); } throw new RuntimeException(String.format( "LifecycleResource beans have one or more reference cycles involving:\r\n %s", beanNames)); } found = false; int j = 0; while (j < beanHolderList.size()) { final BeanHolder beanHolder_j = beanHolderList.get(j); if (beanHolder_j.beansIDependOn.size() == 0) { ans[i] = (LifecycleResource) beanHolder_j.bean; i++; found = true; beanHolderList.remove(j); for (final BeanHolder beanHolder_k : beanHolder_j.beansWhoDependOnMe) { beanHolder_k.beansIDependOn.remove(beanHolder_j); } message.append(beanHolder_j.beanName).append(" "); } else { j++; } } } _logger.info(message.toString()); return ans; }
From source file:org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.java
/** * Determines if the {@code dataSource} being used by Spring was created from * {@link EmbeddedDataSourceConfiguration}. * @param beanFactory the bean factory// ww w .jav a 2 s . c om * @return true if the data source was auto-configured. */ public static boolean containsAutoConfiguredDataSource(ConfigurableListableBeanFactory beanFactory) { try { BeanDefinition beanDefinition = beanFactory.getBeanDefinition("dataSource"); return EmbeddedDataSourceConfiguration.class.getName().equals(beanDefinition.getFactoryBeanName()); } catch (NoSuchBeanDefinitionException ex) { return false; } }
From source file:org.statefulj.framework.core.StatefulFactory.java
/** * @param bf/* w w w. j a v a2 s . co m*/ * @param reg * @param clazz * @return * @throws ClassNotFoundException */ private Class<?> getClassFromFactoryMethod(BeanDefinition bf, BeanDefinitionRegistry reg) throws ClassNotFoundException { Class<?> clazz = null; String factoryBeanName = bf.getFactoryBeanName(); if (factoryBeanName != null) { BeanDefinition factory = reg.getBeanDefinition(factoryBeanName); if (factory != null) { String factoryClassName = factory.getBeanClassName(); Class<?> factoryClass = Class.forName(factoryClassName); List<Method> methods = new LinkedList<Method>(); methods.addAll(Arrays.asList(factoryClass.getMethods())); methods.addAll(Arrays.asList(factoryClass.getDeclaredMethods())); for (Method method : methods) { method.setAccessible(true); if (method.getName().equals(bf.getFactoryMethodName())) { clazz = method.getReturnType(); break; } } } } return clazz; }