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);
}
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);
}
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);
}
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());
}
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);
}
Aggregations