use of org.springframework.beans.BeanWrapperImpl 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.BeanWrapperImpl 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.BeanWrapperImpl 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.BeanWrapperImpl 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);
}
use of org.springframework.beans.BeanWrapperImpl in project grails-core by grails.
the class GrailsDomainClassValidator method cascadeValidationToOne.
/**
* Cascades validation to a one-to-one or many-to-one property.
*
* @param errors The Errors instance
* @param bean The original BeanWrapper
* @param associatedObject The associated object's current value
* @param persistentProperty The GrailsDomainClassProperty instance
* @param propertyName The name of the property
* @param indexOrKey
*/
@SuppressWarnings("rawtypes")
protected void cascadeValidationToOne(Errors errors, BeanWrapper bean, Object associatedObject, GrailsDomainClassProperty persistentProperty, String propertyName, Object indexOrKey) {
if (associatedObject == null) {
return;
}
GrailsDomainClass associatedDomainClass = getAssociatedDomainClass(associatedObject, persistentProperty);
if (associatedDomainClass == null || !isOwningInstance(bean, associatedDomainClass) && !persistentProperty.isExplicitSaveUpdateCascade()) {
return;
}
GrailsDomainClassProperty otherSide = null;
if (persistentProperty.isBidirectional()) {
otherSide = persistentProperty.getOtherSide();
}
Map associatedConstraintedProperties = associatedDomainClass.getConstrainedProperties();
GrailsDomainClassProperty[] associatedPersistentProperties = associatedDomainClass.getPersistentProperties();
String nestedPath = errors.getNestedPath();
try {
errors.setNestedPath(buildNestedPath(nestedPath, propertyName, indexOrKey));
for (GrailsDomainClassProperty associatedPersistentProperty : associatedPersistentProperties) {
if (persistentProperty.isEmbedded() && EMBEDDED_EXCLUDES.contains(associatedPersistentProperty.getName())) {
continue;
}
String associatedPropertyName = associatedPersistentProperty.getName();
if (associatedConstraintedProperties.containsKey(associatedPropertyName)) {
validatePropertyWithConstraint(errors.getNestedPath() + associatedPropertyName, associatedObject, errors, new BeanWrapperImpl(associatedObject), associatedConstraintedProperties);
}
if (associatedPersistentProperty.equals(otherSide))
continue;
if (associatedPersistentProperty.isAssociation()) {
cascadeToAssociativeProperty(errors, new BeanWrapperImpl(associatedObject), associatedPersistentProperty);
}
}
} finally {
errors.setNestedPath(nestedPath);
}
}
Aggregations