Search in sources :

Example 6 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-boot by spring-projects.

the class AtomikosDependsOnBeanFactoryPostProcessor method addTransactionManagerDependencies.

private void addTransactionManagerDependencies(ConfigurableListableBeanFactory beanFactory, String transactionManager) {
    BeanDefinition bean = beanFactory.getBeanDefinition(transactionManager);
    Set<String> dependsOn = new LinkedHashSet<>(asList(bean.getDependsOn()));
    int initialSize = dependsOn.size();
    addDependencies(beanFactory, "javax.jms.ConnectionFactory", dependsOn);
    addDependencies(beanFactory, "javax.sql.DataSource", dependsOn);
    if (dependsOn.size() != initialSize) {
        bean.setDependsOn(dependsOn.toArray(new String[dependsOn.size()]));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 7 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-framework by spring-projects.

the class AopNamespaceHandlerEventTests method testAspectEvent.

@Test
public void testAspectEvent() throws Exception {
    this.reader.loadBeanDefinitions(CONTEXT);
    ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
    assertEquals("Incorrect number of events fired", 5, componentDefinitions.length);
    assertTrue("No holder with nested components", componentDefinitions[0] instanceof CompositeComponentDefinition);
    CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0];
    assertEquals("aop:config", compositeDef.getName());
    ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents();
    assertEquals("Incorrect number of inner components", 2, nestedComponentDefs.length);
    AspectComponentDefinition acd = null;
    for (int i = 0; i < nestedComponentDefs.length; i++) {
        ComponentDefinition componentDefinition = nestedComponentDefs[i];
        if (componentDefinition instanceof AspectComponentDefinition) {
            acd = (AspectComponentDefinition) componentDefinition;
            break;
        }
    }
    assertNotNull("AspectComponentDefinition not found", acd);
    BeanDefinition[] beanDefinitions = acd.getBeanDefinitions();
    assertEquals(5, beanDefinitions.length);
    BeanReference[] beanReferences = acd.getBeanReferences();
    assertEquals(6, beanReferences.length);
    Set<String> expectedReferences = new HashSet<>();
    expectedReferences.add("pc");
    expectedReferences.add("countingAdvice");
    for (int i = 0; i < beanReferences.length; i++) {
        BeanReference beanReference = beanReferences[i];
        expectedReferences.remove(beanReference.getBeanName());
    }
    assertEquals("Incorrect references found", 0, expectedReferences.size());
    for (int i = 1; i < componentDefinitions.length; i++) {
        assertTrue(componentDefinitions[i] instanceof BeanComponentDefinition);
    }
    ComponentDefinition[] nestedComponentDefs2 = acd.getNestedComponents();
    assertEquals("Inner PointcutComponentDefinition not found", 1, nestedComponentDefs2.length);
    assertTrue(nestedComponentDefs2[0] instanceof PointcutComponentDefinition);
    PointcutComponentDefinition pcd = (PointcutComponentDefinition) nestedComponentDefs2[0];
    assertEquals("Incorrect number of BeanDefinitions", 1, pcd.getBeanDefinitions().length);
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanReference(org.springframework.beans.factory.config.BeanReference) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) ComponentDefinition(org.springframework.beans.factory.parsing.ComponentDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-framework by spring-projects.

the class DefaultListableBeanFactory method removeBeanDefinition.

@Override
public void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
    Assert.hasText(beanName, "'beanName' must not be empty");
    BeanDefinition bd = this.beanDefinitionMap.remove(beanName);
    if (bd == null) {
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("No bean named '" + beanName + "' found in " + this);
        }
        throw new NoSuchBeanDefinitionException(beanName);
    }
    if (hasBeanCreationStarted()) {
        // Cannot modify startup-time collection elements anymore (for stable iteration)
        synchronized (this.beanDefinitionMap) {
            List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames);
            updatedDefinitions.remove(beanName);
            this.beanDefinitionNames = updatedDefinitions;
        }
    } else {
        // Still in startup registration phase
        this.beanDefinitionNames.remove(beanName);
    }
    this.frozenBeanDefinitionNames = null;
    resetBeanDefinition(beanName);
}
Also used : ArrayList(java.util.ArrayList) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 9 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-framework by spring-projects.

the class DefaultListableBeanFactory method doResolveDependency.

public Object doResolveDependency(DependencyDescriptor descriptor, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException {
    InjectionPoint previousInjectionPoint = ConstructorResolver.setCurrentInjectionPoint(descriptor);
    try {
        Object shortcut = descriptor.resolveShortcut(this);
        if (shortcut != null) {
            return shortcut;
        }
        Class<?> type = descriptor.getDependencyType();
        Object value = getAutowireCandidateResolver().getSuggestedValue(descriptor);
        if (value != null) {
            if (value instanceof String) {
                String strVal = resolveEmbeddedValue((String) value);
                BeanDefinition bd = (beanName != null && containsBean(beanName) ? getMergedBeanDefinition(beanName) : null);
                value = evaluateBeanDefinitionString(strVal, bd);
            }
            TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
            return (descriptor.getField() != null ? converter.convertIfNecessary(value, type, descriptor.getField()) : converter.convertIfNecessary(value, type, descriptor.getMethodParameter()));
        }
        Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter);
        if (multipleBeans != null) {
            return multipleBeans;
        }
        Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor);
        if (matchingBeans.isEmpty()) {
            if (descriptor.isRequired()) {
                raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor);
            }
            return null;
        }
        String autowiredBeanName;
        Object instanceCandidate;
        if (matchingBeans.size() > 1) {
            autowiredBeanName = determineAutowireCandidate(matchingBeans, descriptor);
            if (autowiredBeanName == null) {
                if (descriptor.isRequired() || !indicatesMultipleBeans(type)) {
                    return descriptor.resolveNotUnique(type, matchingBeans);
                } else {
                    // (before 4.3 in particular when we didn't even look for collection beans).
                    return null;
                }
            }
            instanceCandidate = matchingBeans.get(autowiredBeanName);
        } else {
            // We have exactly one match.
            Map.Entry<String, Object> entry = matchingBeans.entrySet().iterator().next();
            autowiredBeanName = entry.getKey();
            instanceCandidate = entry.getValue();
        }
        if (autowiredBeanNames != null) {
            autowiredBeanNames.add(autowiredBeanName);
        }
        return (instanceCandidate instanceof Class ? descriptor.resolveCandidate(autowiredBeanName, type, this) : instanceCandidate);
    } finally {
        ConstructorResolver.setCurrentInjectionPoint(previousInjectionPoint);
    }
}
Also used : TypeConverter(org.springframework.beans.TypeConverter) InjectionPoint(org.springframework.beans.factory.InjectionPoint) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-framework by spring-projects.

the class DefaultListableBeanFactory method findAnnotationOnBean.

/**
	 * Find a {@link Annotation} of {@code annotationType} on the specified
	 * bean, traversing its interfaces and super classes if no annotation can be
	 * found on the given class itself, as well as checking its raw bean class
	 * if not found on the exposed bean reference (e.g. in case of a proxy).
	 */
@Override
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) throws NoSuchBeanDefinitionException {
    A ann = null;
    Class<?> beanType = getType(beanName);
    if (beanType != null) {
        ann = AnnotationUtils.findAnnotation(beanType, annotationType);
    }
    if (ann == null && containsBeanDefinition(beanName)) {
        BeanDefinition bd = getMergedBeanDefinition(beanName);
        if (bd instanceof AbstractBeanDefinition) {
            AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
            if (abd.hasBeanClass()) {
                ann = AnnotationUtils.findAnnotation(abd.getBeanClass(), annotationType);
            }
        }
    }
    return ann;
}
Also used : BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)593 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)188 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)117 Test (org.junit.jupiter.api.Test)105 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)89 Element (org.w3c.dom.Element)73 Test (org.junit.Test)70 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)54 ManagedList (org.springframework.beans.factory.support.ManagedList)40 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)39 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)36 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)35 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)33 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)33 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)30 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)29 ArrayList (java.util.ArrayList)28 Method (java.lang.reflect.Method)27 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)27