Java Code Examples for org.springframework.beans.BeanWrapper#getWrappedInstance()

The following examples show how to use org.springframework.beans.BeanWrapper#getWrappedInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractAutowireCapableBeanFactory.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
		}
	}
	finally {
		// Finished partial creation of this bean.
		afterPrototypeCreation(beanName);
	}
	return getFactoryBean(beanName, instance);
}
 
Example 2
Source File: TaskExecutorFactoryBean.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	BeanWrapper bw = new BeanWrapperImpl(ThreadPoolTaskExecutor.class);
	determinePoolSizeRange(bw);
	if (this.queueCapacity != null) {
		bw.setPropertyValue("queueCapacity", this.queueCapacity);
	}
	if (this.keepAliveSeconds != null) {
		bw.setPropertyValue("keepAliveSeconds", this.keepAliveSeconds);
	}
	if (this.rejectedExecutionHandler != null) {
		bw.setPropertyValue("rejectedExecutionHandler", this.rejectedExecutionHandler);
	}
	if (this.beanName != null) {
		bw.setPropertyValue("threadNamePrefix", this.beanName + "-");
	}
	this.target = (TaskExecutor) bw.getWrappedInstance();
	if (this.target instanceof InitializingBean) {
		((InitializingBean) this.target).afterPropertiesSet();
	}
}
 
Example 3
Source File: CukeUtils.java    From objectlabkit with Apache License 2.0 6 votes vote down vote up
public static <T> T copyFieldValues(final List<String> fieldsToCopy, final T source, final Class<T> typeOfT) {
    try {
        final BeanWrapper src = new BeanWrapperImpl(source);
        final BeanWrapper target = new BeanWrapperImpl(typeOfT.newInstance());
        fieldsToCopy.forEach(t -> {
            try {
                Object propertyValue = src.getPropertyValue(t);
                if (propertyValue instanceof String) {
                    propertyValue = ((String) propertyValue).trim();
                }
                target.setPropertyValue(t, propertyValue);
            } catch (final NullValueInNestedPathException ignore) {
            }
        });
        return (T) target.getWrappedInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        return null;
    }
}
 
Example 4
Source File: CukeUtils.java    From objectlabkit with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked" })
public static <T> T copyFields(final T source, final Class<T> typeOfT, final List<String> propertiesToCopy) {
    try {
        final BeanWrapper src = new BeanWrapperImpl(source);
        final BeanWrapper target = new BeanWrapperImpl(typeOfT.newInstance());
        Arrays.stream(src.getPropertyDescriptors()).forEach(property -> {
            final String propertyName = property.getName();
            if (propertiesToCopy.contains(propertyName)) {
                final Object propertyValue = String.class.isAssignableFrom(property.getPropertyType())
                        ? StringUtil.nullIfEmpty((String) src.getPropertyValue(propertyName))
                        : src.getPropertyValue(propertyName);
                target.setPropertyValue(propertyName, propertyValue);
            }
        });
        return (T) target.getWrappedInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        return null;
    }
}
 
Example 5
Source File: BeanUtil.java    From newbee-mall with GNU General Public License v3.0 5 votes vote down vote up
public static <T> T toBean(Map<String, Object> map, Class<T> beanType) {
    BeanWrapper beanWrapper = new BeanWrapperImpl(beanType);
    map.forEach((key, value) -> {
        if (beanWrapper.isWritableProperty(key)) {
            beanWrapper.setPropertyValue(key, value);
        }
    });
    return (T) beanWrapper.getWrappedInstance();
}
 
Example 6
Source File: AbstractAutowireCapableBeanFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Obtain a "shortcut" 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
 */
@Nullable
private FactoryBean<?> getSingletonFactoryBeanForTypeCheck(String beanName, RootBeanDefinition mbd) {
	synchronized (getSingletonMutex()) {
		BeanWrapper bw = this.factoryBeanInstanceCache.get(beanName);
		if (bw != null) {
			return (FactoryBean<?>) bw.getWrappedInstance();
		}
		Object beanInstance = getSingleton(beanName, false);
		if (beanInstance instanceof FactoryBean) {
			return (FactoryBean<?>) beanInstance;
		}
		if (isSingletonCurrentlyInCreation(beanName) ||
				(mbd.getFactoryBeanName() != null && isSingletonCurrentlyInCreation(mbd.getFactoryBeanName()))) {
			return null;
		}

		Object instance;
		try {
			// Mark this bean as currently in creation, even if just partially.
			beforeSingletonCreation(beanName);
			// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
			instance = resolveBeforeInstantiation(beanName, mbd);
			if (instance == null) {
				bw = createBeanInstance(beanName, mbd, null);
				instance = bw.getWrappedInstance();
			}
		}
		finally {
			// Finished partial creation of this bean.
			afterSingletonCreation(beanName);
		}

		FactoryBean<?> fb = getFactoryBean(beanName, instance);
		if (bw != null) {
			this.factoryBeanInstanceCache.put(beanName, bw);
		}
		return fb;
	}
}
 
Example 7
Source File: AbstractAutowireCapableBeanFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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
 */
@Nullable
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);
}
 
Example 8
Source File: AbstractAutowireCapableBeanFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtain a "shortcut" 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<?> getSingletonFactoryBeanForTypeCheck(String beanName, RootBeanDefinition mbd) {
	synchronized (getSingletonMutex()) {
		BeanWrapper bw = this.factoryBeanInstanceCache.get(beanName);
		if (bw != null) {
			return (FactoryBean<?>) bw.getWrappedInstance();
		}
		if (isSingletonCurrentlyInCreation(beanName) ||
				(mbd.getFactoryBeanName() != null && isSingletonCurrentlyInCreation(mbd.getFactoryBeanName()))) {
			return null;
		}

		Object instance = null;
		try {
			// Mark this bean as currently in creation, even if just partially.
			beforeSingletonCreation(beanName);
			// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
			instance = resolveBeforeInstantiation(beanName, mbd);
			if (instance == null) {
				bw = createBeanInstance(beanName, mbd, null);
				instance = bw.getWrappedInstance();
			}
		}
		finally {
			// Finished partial creation of this bean.
			afterSingletonCreation(beanName);
		}

		FactoryBean<?> fb = getFactoryBean(beanName, instance);
		if (bw != null) {
			this.factoryBeanInstanceCache.put(beanName, bw);
		}
		return fb;
	}
}
 
Example 9
Source File: AbstractAutowireCapableBeanFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 10
Source File: AbstractAutowireCapableBeanFactory.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
/**
 * Obtain a "shortcut" 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<?> getSingletonFactoryBeanForTypeCheck(String beanName, RootBeanDefinition mbd) {
	synchronized (getSingletonMutex()) {
		BeanWrapper bw = this.factoryBeanInstanceCache.get(beanName);
		if (bw != null) {
			return (FactoryBean<?>) bw.getWrappedInstance();
		}
		if (isSingletonCurrentlyInCreation(beanName)) {
			return null;
		}
		Object instance = null;
		try {
			// Mark this bean as currently in creation, even if just partially.
			beforeSingletonCreation(beanName);
			// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
			instance = resolveBeforeInstantiation(beanName, mbd);
			if (instance == null) {
				bw = createBeanInstance(beanName, mbd, null);
				instance = bw.getWrappedInstance();
			}
		}
		finally {
			// Finished partial creation of this bean.
			afterSingletonCreation(beanName);
		}
		FactoryBean<?> fb = getFactoryBean(beanName, instance);
		if (bw != null) {
			this.factoryBeanInstanceCache.put(beanName, bw);
		}
		return fb;
	}
}
 
Example 11
Source File: AbstractAutowireCapableBeanFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Obtain a "shortcut" 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<?> getSingletonFactoryBeanForTypeCheck(String beanName, RootBeanDefinition mbd) {
	synchronized (getSingletonMutex()) {
		BeanWrapper bw = this.factoryBeanInstanceCache.get(beanName);
		if (bw != null) {
			return (FactoryBean<?>) bw.getWrappedInstance();
		}
		if (isSingletonCurrentlyInCreation(beanName) ||
				(mbd.getFactoryBeanName() != null && isSingletonCurrentlyInCreation(mbd.getFactoryBeanName()))) {
			return null;
		}
		Object instance = null;
		try {
			// Mark this bean as currently in creation, even if just partially.
			beforeSingletonCreation(beanName);
			// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
			instance = resolveBeforeInstantiation(beanName, mbd);
			if (instance == null) {
				bw = createBeanInstance(beanName, mbd, null);
				instance = bw.getWrappedInstance();
			}
		}
		finally {
			// Finished partial creation of this bean.
			afterSingletonCreation(beanName);
		}
		FactoryBean<?> fb = getFactoryBean(beanName, instance);
		if (bw != null) {
			this.factoryBeanInstanceCache.put(beanName, bw);
		}
		return fb;
	}
}
 
Example 12
Source File: AbstractAutowireCapableBeanFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 13
Source File: SimpleBeanCopier.java    From onetwo with Apache License 2.0 5 votes vote down vote up
private boolean isSrcHasProperty(BeanWrapper srcBean, String targetPropertyName){
	if(srcBean.getWrappedInstance() instanceof Map){
		Map<?, ?> map = (Map<?, ?>)srcBean.getWrappedInstance();
		return map.containsKey(targetPropertyName);
	}else{
		return srcBean.isReadableProperty(targetPropertyName);
	}
}
 
Example 14
Source File: SimpleBeanCopier.java    From onetwo with Apache License 2.0 5 votes vote down vote up
/***
 * 如果原对象没有目标对象的属性,则返回null
 * @param srcBean
 * @param targetPropertyName
 * @return
 */
private Object getPropertyValue(BeanWrapper srcBean, String targetPropertyName){
	Object srcValue = null;
	if(srcBean.getWrappedInstance() instanceof Map){
		Map<?, ?> map = (Map<?, ?>)srcBean.getWrappedInstance();
		srcValue = map.get(targetPropertyName);
	}/*else if(srcBean.isReadableProperty(targetPropertyName)){
		srcValue = srcBean.getPropertyValue(targetPropertyName);
	}*/else{
		srcValue = srcBean.getPropertyValue(targetPropertyName);
	}
	return srcValue;
}