Search in sources :

Example 11 with AbstractBeanDefinition

use of org.springframework.beans.factory.support.AbstractBeanDefinition in project spring-framework by spring-projects.

the class ConfigurationClassUtils method checkConfigurationClassCandidate.

/**
	 * Check whether the given bean definition is a candidate for a configuration class
	 * (or a nested component class declared within a configuration/component class,
	 * to be auto-registered as well), and mark it accordingly.
	 * @param beanDef the bean definition to check
	 * @param metadataReaderFactory the current factory in use by the caller
	 * @return whether the candidate qualifies as (any kind of) configuration class
	 */
public static boolean checkConfigurationClassCandidate(BeanDefinition beanDef, MetadataReaderFactory metadataReaderFactory) {
    String className = beanDef.getBeanClassName();
    if (className == null) {
        return false;
    }
    AnnotationMetadata metadata;
    if (beanDef instanceof AnnotatedBeanDefinition && className.equals(((AnnotatedBeanDefinition) beanDef).getMetadata().getClassName())) {
        // Can reuse the pre-parsed metadata from the given BeanDefinition...
        metadata = ((AnnotatedBeanDefinition) beanDef).getMetadata();
    } else if (beanDef instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) beanDef).hasBeanClass()) {
        // Check already loaded Class if present...
        // since we possibly can't even load the class file for this Class.
        Class<?> beanClass = ((AbstractBeanDefinition) beanDef).getBeanClass();
        metadata = new StandardAnnotationMetadata(beanClass, true);
    } else {
        try {
            MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(className);
            metadata = metadataReader.getAnnotationMetadata();
        } catch (IOException ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Could not find class file for introspecting configuration annotations: " + className, ex);
            }
            return false;
        }
    }
    if (isFullConfigurationCandidate(metadata)) {
        beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
    } else if (isLiteConfigurationCandidate(metadata)) {
        beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
    } else {
        return false;
    }
    // It's a full or lite configuration candidate... Let's determine the order value, if any.
    Map<String, Object> orderAttributes = metadata.getAnnotationAttributes(Order.class.getName());
    if (orderAttributes != null) {
        beanDef.setAttribute(ORDER_ATTRIBUTE, orderAttributes.get(AnnotationUtils.VALUE));
    }
    return true;
}
Also used : Order(org.springframework.core.annotation.Order) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata) MetadataReader(org.springframework.core.type.classreading.MetadataReader) IOException(java.io.IOException) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata)

Example 12 with AbstractBeanDefinition

use of org.springframework.beans.factory.support.AbstractBeanDefinition in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScanner method doScan.

/**
	 * Perform a scan within the specified base packages,
	 * returning the registered bean definitions.
	 * <p>This method does <i>not</i> register an annotation config processor
	 * but rather leaves this up to the caller.
	 * @param basePackages the packages to check for annotated classes
	 * @return set of beans registered if any for tooling registration purposes (never {@code null})
	 */
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
    Assert.notEmpty(basePackages, "At least one base package must be specified");
    Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
    for (String basePackage : basePackages) {
        Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
        for (BeanDefinition candidate : candidates) {
            ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
            candidate.setScope(scopeMetadata.getScopeName());
            String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
            if (candidate instanceof AbstractBeanDefinition) {
                postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
            }
            if (candidate instanceof AnnotatedBeanDefinition) {
                AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
            }
            if (checkCandidate(beanName, candidate)) {
                BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
                definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
                beanDefinitions.add(definitionHolder);
                registerBeanDefinition(definitionHolder, this.registry);
            }
        }
    }
    return beanDefinitions;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 13 with AbstractBeanDefinition

use of org.springframework.beans.factory.support.AbstractBeanDefinition in project spring-framework by spring-projects.

the class MBeanServerBeanDefinitionParser method parseInternal.

@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    String agentId = element.getAttribute(AGENT_ID_ATTRIBUTE);
    if (StringUtils.hasText(agentId)) {
        RootBeanDefinition bd = new RootBeanDefinition(MBeanServerFactoryBean.class);
        bd.getPropertyValues().add("agentId", agentId);
        return bd;
    }
    AbstractBeanDefinition specialServer = findServerForSpecialEnvironment();
    if (specialServer != null) {
        return specialServer;
    }
    RootBeanDefinition bd = new RootBeanDefinition(MBeanServerFactoryBean.class);
    bd.getPropertyValues().add("locateExistingServerIfPossible", Boolean.TRUE);
    // Mark as infrastructure bean and attach source location.
    bd.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    bd.setSource(parserContext.extractSource(element));
    return bd;
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition)

Example 14 with AbstractBeanDefinition

use of org.springframework.beans.factory.support.AbstractBeanDefinition in project spring-framework by spring-projects.

the class ScriptFactoryPostProcessor method createConfigInterface.

/**
	 * Create a config interface for the given bean definition, defining setter
	 * methods for the defined property values as well as an init method and
	 * a destroy method (if defined).
	 * <p>This implementation creates the interface via CGLIB's InterfaceMaker,
	 * determining the property types from the given interfaces (as far as possible).
	 * @param bd the bean definition (property values etc) to create a
	 * config interface for
	 * @param interfaces the interfaces to check against (might define
	 * getters corresponding to the setters we're supposed to generate)
	 * @return the config interface
	 * @see org.springframework.cglib.proxy.InterfaceMaker
	 * @see org.springframework.beans.BeanUtils#findPropertyType
	 */
protected Class<?> createConfigInterface(BeanDefinition bd, Class<?>[] interfaces) {
    InterfaceMaker maker = new InterfaceMaker();
    PropertyValue[] pvs = bd.getPropertyValues().getPropertyValues();
    for (PropertyValue pv : pvs) {
        String propertyName = pv.getName();
        Class<?> propertyType = BeanUtils.findPropertyType(propertyName, interfaces);
        String setterName = "set" + StringUtils.capitalize(propertyName);
        Signature signature = new Signature(setterName, Type.VOID_TYPE, new Type[] { Type.getType(propertyType) });
        maker.add(signature, new Type[0]);
    }
    if (bd instanceof AbstractBeanDefinition) {
        AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
        if (abd.getInitMethodName() != null) {
            Signature signature = new Signature(abd.getInitMethodName(), Type.VOID_TYPE, new Type[0]);
            maker.add(signature, new Type[0]);
        }
        if (StringUtils.hasText(abd.getDestroyMethodName())) {
            Signature signature = new Signature(abd.getDestroyMethodName(), Type.VOID_TYPE, new Type[0]);
            maker.add(signature, new Type[0]);
        }
    }
    return maker.create();
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) InterfaceMaker(org.springframework.cglib.proxy.InterfaceMaker) Signature(org.springframework.cglib.core.Signature) PropertyValue(org.springframework.beans.PropertyValue)

Example 15 with AbstractBeanDefinition

use of org.springframework.beans.factory.support.AbstractBeanDefinition in project spring-framework by spring-projects.

the class XmlBeanFactoryTests method testNonLenientDependencyMatchingFactoryMethod.

@Test
public void testNonLenientDependencyMatchingFactoryMethod() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("lenientDependencyTestBeanFactoryMethod");
    bd.setLenientConstructorResolution(false);
    try {
        xbf.getBean("lenientDependencyTestBeanFactoryMethod");
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected
        ex.printStackTrace();
        assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Aggregations

AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)44 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)13 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)13 Test (org.junit.Test)10 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)7 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)6 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)5 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)4 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)4 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)3 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)3 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)3 ArrayList (java.util.ArrayList)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)2 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)2 ChildBeanDefinition (org.springframework.beans.factory.support.ChildBeanDefinition)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1