org.springframework.beans.BeanWrapper#getWrappedInstance ( )源码实例Demo

下面列出了org.springframework.beans.BeanWrapper#getWrappedInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * 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);
}
 
@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();
	}
}
 
源代码3 项目: objectlabkit   文件: CukeUtils.java
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;
    }
}
 
源代码4 项目: objectlabkit   文件: CukeUtils.java
@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;
    }
}
 
源代码5 项目: newbee-mall   文件: BeanUtil.java
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();
}
 
/**
 * 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;
	}
}
 
/**
 * 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);
}
 
源代码8 项目: lams   文件: AbstractAutowireCapableBeanFactory.java
/**
 * 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;
	}
}
 
源代码9 项目: lams   文件: AbstractAutowireCapableBeanFactory.java
/**
 * 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);
}
 
/**
 * 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;
	}
}
 
/**
 * 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;
	}
}
 
/**
 * 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);
}
 
源代码13 项目: onetwo   文件: SimpleBeanCopier.java
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);
	}
}
 
源代码14 项目: onetwo   文件: SimpleBeanCopier.java
/***
 * 如果原对象没有目标对象的属性,则返回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;
}