Search in sources :

Example 6 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project spring-framework by spring-projects.

the class AbstractAutowireCapableBeanFactory method getNonSingletonFactoryBeanForTypeCheck.

/**
	 * Obtain a "shortcut" non-singleton FactoryBean instance to use for a
	 * {@code getObjectType()} call, without full initialization of the FactoryBean.
	 * @param beanName the name of the bean
	 * @param mbd the bean definition for the bean
	 * @return the FactoryBean instance, or {@code null} to indicate
	 * that we couldn't obtain a shortcut FactoryBean instance
	 */
private FactoryBean<?> getNonSingletonFactoryBeanForTypeCheck(String beanName, RootBeanDefinition mbd) {
    if (isPrototypeCurrentlyInCreation(beanName)) {
        return null;
    }
    Object instance = null;
    try {
        // Mark this bean as currently in creation, even if just partially.
        beforePrototypeCreation(beanName);
        // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
        instance = resolveBeforeInstantiation(beanName, mbd);
        if (instance == null) {
            BeanWrapper bw = createBeanInstance(beanName, mbd, null);
            instance = bw.getWrappedInstance();
        }
    } catch (BeanCreationException ex) {
        // Can only happen when getting a FactoryBean.
        if (logger.isDebugEnabled()) {
            logger.debug("Bean creation exception on non-singleton FactoryBean type check: " + ex);
        }
        onSuppressedException(ex);
        return null;
    } finally {
        // Finished partial creation of this bean.
        afterPrototypeCreation(beanName);
    }
    return getFactoryBean(beanName, instance);
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanWrapper(org.springframework.beans.BeanWrapper)

Example 7 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project spring-framework by spring-projects.

the class AbstractAutowireCapableBeanFactory method createBeanInstance.

/**
	 * Create a new instance for the specified bean, using an appropriate instantiation strategy:
	 * factory method, constructor autowiring, or simple instantiation.
	 * @param beanName the name of the bean
	 * @param mbd the bean definition for the bean
	 * @param args explicit arguments to use for constructor or factory method invocation
	 * @return BeanWrapper for the new instance
	 * @see #instantiateUsingFactoryMethod
	 * @see #autowireConstructor
	 * @see #instantiateBean
	 */
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) {
    // Make sure bean class is actually resolved at this point.
    Class<?> beanClass = resolveBeanClass(mbd, beanName);
    if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
        throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
    }
    Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
    if (instanceSupplier != null) {
        BeanWrapper bw = new BeanWrapperImpl(instanceSupplier.get());
        initBeanWrapper(bw);
        return bw;
    }
    if (mbd.getFactoryMethodName() != null) {
        return instantiateUsingFactoryMethod(beanName, mbd, args);
    }
    // Shortcut when re-creating the same bean...
    boolean resolved = false;
    boolean autowireNecessary = false;
    if (args == null) {
        synchronized (mbd.constructorArgumentLock) {
            if (mbd.resolvedConstructorOrFactoryMethod != null) {
                resolved = true;
                autowireNecessary = mbd.constructorArgumentsResolved;
            }
        }
    }
    if (resolved) {
        if (autowireNecessary) {
            return autowireConstructor(beanName, mbd, null, null);
        } else {
            return instantiateBean(beanName, mbd);
        }
    }
    // Need to determine the constructor...
    Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
    if (ctors != null || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
        return autowireConstructor(beanName, mbd, ctors, args);
    }
    // No special handling: simply use no-arg constructor.
    return instantiateBean(beanName, mbd);
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Constructor(java.lang.reflect.Constructor)

Example 8 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project spring-framework by spring-projects.

the class AbstractAutowireCapableBeanFactory method autowireBeanProperties.

@Override
public void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck) throws BeansException {
    if (autowireMode == AUTOWIRE_CONSTRUCTOR) {
        throw new IllegalArgumentException("AUTOWIRE_CONSTRUCTOR not supported for existing bean instance");
    }
    // Use non-singleton bean definition, to avoid registering bean as dependent bean.
    RootBeanDefinition bd = new RootBeanDefinition(ClassUtils.getUserClass(existingBean), autowireMode, dependencyCheck);
    bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    BeanWrapper bw = new BeanWrapperImpl(existingBean);
    initBeanWrapper(bw);
    populateBean(bd.getBeanClass().getName(), bd, bw);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl)

Example 9 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project spring-framework by spring-projects.

the class AbstractAutowireCapableBeanFactory method applyBeanPropertyValues.

@Override
public void applyBeanPropertyValues(Object existingBean, String beanName) throws BeansException {
    markBeanAsCreated(beanName);
    BeanDefinition bd = getMergedBeanDefinition(beanName);
    BeanWrapper bw = new BeanWrapperImpl(existingBean);
    initBeanWrapper(bw);
    applyPropertyValues(beanName, bd, bw, bd.getPropertyValues());
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 10 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project grails-core by grails.

the class GrailsDomainClassValidator method validate.

/**
     * @see CascadingValidator#validate(Object, org.springframework.validation.Errors, boolean)
     */
@SuppressWarnings({ "unchecked", "rawtypes" })
public void validate(Object obj, Errors errors, boolean cascade) {
    if (obj == null) {
        throw new IllegalArgumentException("Argument [" + obj + "] is not an instance of [" + domainClass.getClazz() + "] which this validator is configured for");
    }
    BeanWrapper bean = new BeanWrapperImpl(obj);
    Map constrainedProperties = domainClass.getConstrainedProperties();
    Set<String> constrainedPropertyNames = new HashSet(constrainedProperties.keySet());
    for (Object key : constrainedProperties.keySet()) {
        String propertyName = (String) key;
        validatePropertyWithConstraint(propertyName, obj, errors, bean, constrainedProperties);
    }
    GrailsDomainClassProperty[] persistentProperties = domainClass.getPersistentProperties();
    for (GrailsDomainClassProperty persistentProperty : persistentProperties) {
        String propertyName = persistentProperty.getName();
        if ((persistentProperty.isAssociation() || persistentProperty.isEmbedded()) && cascade) {
            cascadeToAssociativeProperty(errors, bean, persistentProperty);
        }
        // Remove this property from the set of constrained property
        // names because we have already processed it.
        constrainedPropertyNames.remove(propertyName);
    }
    postValidate(obj, errors);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) GString(groovy.lang.GString) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

BeanWrapper (org.springframework.beans.BeanWrapper)144 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)79 Test (org.junit.jupiter.api.Test)31 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)18 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)18 TestBean (org.springframework.beans.testfixture.beans.TestBean)18 Consumes (javax.ws.rs.Consumes)16 PUT (javax.ws.rs.PUT)16 Path (javax.ws.rs.Path)16 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 HashSet (java.util.HashSet)12 BeansException (org.springframework.beans.BeansException)12 PropertyDescriptor (java.beans.PropertyDescriptor)11 OnmsNode (org.opennms.netmgt.model.OnmsNode)9 Test (org.junit.Test)6 BigDecimal (java.math.BigDecimal)5