All Downloads are FREE. Search and download functionalities are using the official Maven repository.

infra.beans.factory.config.PropertyPathFactoryBean Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 - 2024 the original author or authors.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see [https://www.gnu.org/licenses/]
 */
package infra.beans.factory.config;

import infra.beans.BeanWrapper;
import infra.beans.BeansException;
import infra.beans.factory.BeanFactory;
import infra.beans.factory.BeanFactoryAware;
import infra.beans.factory.BeanFactoryUtils;
import infra.beans.factory.BeanNameAware;
import infra.beans.factory.FactoryBean;
import infra.lang.Assert;
import infra.lang.Nullable;
import infra.logging.Logger;
import infra.logging.LoggerFactory;
import infra.util.StringUtils;

/**
 * {@link FactoryBean} that evaluates a property path on a given target object.
 *
 * 

The target object can be specified directly or via a bean name. * *

Usage examples: * *

<!-- target bean to be referenced by name -->
 * <bean id="tb" class="infra.beans.factory.TestBean" singleton="false">
 *   <property name="age" value="10"/>
 *   <property name="spouse">
 *     <bean class="infra.beans.factory.TestBean">
 *       <property name="age" value="11"/>
 *     </bean>
 *   </property>
 * </bean>
 *
 * <!-- will result in 12, which is the value of property 'age' of the inner bean -->
 * <bean id="propertyPath1" class="infra.beans.factory.config.PropertyPathFactoryBean">
 *   <property name="targetObject">
 *     <bean class="infra.beans.factory.TestBean">
 *       <property name="age" value="12"/>
 *     </bean>
 *   </property>
 *   <property name="propertyPath" value="age"/>
 * </bean>
 *
 * <!-- will result in 11, which is the value of property 'spouse.age' of bean 'tb' -->
 * <bean id="propertyPath2" class="infra.beans.factory.config.PropertyPathFactoryBean">
 *   <property name="targetBeanName" value="tb"/>
 *   <property name="propertyPath" value="spouse.age"/>
 * </bean>
 *
 * <!-- will result in 10, which is the value of property 'age' of bean 'tb' -->
 * <bean id="tb.age" class="infra.beans.factory.config.PropertyPathFactoryBean"/>
* * @author Juergen Hoeller * @author Harry Yang * @see #setTargetObject * @see #setTargetBeanName * @see #setPropertyPath * @since 4.0 2021/11/30 15:08 */ public class PropertyPathFactoryBean implements FactoryBean, BeanNameAware, BeanFactoryAware { private static final Logger logger = LoggerFactory.getLogger(PropertyPathFactoryBean.class); @Nullable private BeanWrapper targetBeanWrapper; @Nullable private String targetBeanName; @Nullable private String propertyPath; @Nullable private Class resultType; @Nullable private String beanName; @Nullable private BeanFactory beanFactory; /** * Specify a target object to apply the property path to. * Alternatively, specify a target bean name. * * @param targetObject a target object, for example a bean reference * or an inner bean * @see #setTargetBeanName */ public void setTargetObject(Object targetObject) { this.targetBeanWrapper = BeanWrapper.forBeanPropertyAccess(targetObject); } /** * Specify the name of a target bean to apply the property path to. * Alternatively, specify a target object directly. * * @param targetBeanName the bean name to be looked up in the * containing bean factory (e.g. "testBean") * @see #setTargetObject */ public void setTargetBeanName(@Nullable String targetBeanName) { this.targetBeanName = StringUtils.trimAllWhitespace(targetBeanName); } /** * Specify the property path to apply to the target. * * @param propertyPath the property path, potentially nested * (e.g. "age" or "spouse.age") */ public void setPropertyPath(@Nullable String propertyPath) { this.propertyPath = StringUtils.trimAllWhitespace(propertyPath); } /** * Specify the type of the result from evaluating the property path. *

Note: This is not necessary for directly specified target objects * or singleton target beans, where the type can be determined through * introspection. Just specify this in case of a prototype target, * provided that you need matching by type (for example, for autowiring). * * @param resultType the result type, for example "java.lang.Integer" */ public void setResultType(@Nullable Class resultType) { this.resultType = resultType; } /** * The bean name of this PropertyPathFactoryBean will be interpreted * as "beanName.property" pattern, if neither "targetObject" nor * "targetBeanName" nor "propertyPath" have been specified. * This allows for concise bean definitions with just an id/name. */ @Override public void setBeanName(String beanName) { this.beanName = StringUtils.trimAllWhitespace(BeanFactoryUtils.originalBeanName(beanName)); } @Override public void setBeanFactory(BeanFactory beanFactory) { this.beanFactory = beanFactory; if (targetBeanWrapper != null && targetBeanName != null) { throw new IllegalArgumentException("Specify either 'targetObject' or 'targetBeanName', not both"); } if (targetBeanWrapper == null && targetBeanName == null) { if (propertyPath != null) { throw new IllegalArgumentException( "Specify 'targetObject' or 'targetBeanName' in combination with 'propertyPath'"); } // No other properties specified: check bean name. int dotIndex = beanName != null ? beanName.indexOf('.') : -1; if (dotIndex == -1) { throw new IllegalArgumentException( "Neither 'targetObject' nor 'targetBeanName' specified, and PropertyPathFactoryBean " + "bean name '" + beanName + "' does not follow 'beanName.property' syntax"); } this.targetBeanName = beanName.substring(0, dotIndex); this.propertyPath = beanName.substring(dotIndex + 1); } else if (propertyPath == null) { // either targetObject or targetBeanName specified throw new IllegalArgumentException("'propertyPath' is required"); } if (targetBeanWrapper == null && beanFactory.isSingleton(targetBeanName)) { // Eagerly fetch singleton target bean, and determine result type. Object bean = beanFactory.getBean(targetBeanName); this.targetBeanWrapper = BeanWrapper.forBeanPropertyAccess(bean); this.resultType = targetBeanWrapper.getPropertyType(propertyPath); } } @Override @Nullable public Object getObject() throws BeansException { BeanWrapper target = this.targetBeanWrapper; if (target != null) { if (logger.isWarnEnabled() && targetBeanName != null && beanFactory instanceof ConfigurableBeanFactory configurable && configurable.isCurrentlyInCreation(targetBeanName)) { logger.warn("Target bean '{}' is still in creation due to a circular " + "reference - obtained value for property '{}' may be outdated!", targetBeanName, propertyPath); } } else { // Fetch prototype target bean... Assert.state(beanFactory != null, "No BeanFactory available"); Assert.state(targetBeanName != null, "No target bean name specified"); Object bean = beanFactory.getBean(targetBeanName); target = BeanWrapper.forBeanPropertyAccess(bean); } Assert.state(propertyPath != null, "No property path specified"); return target.getPropertyValue(propertyPath); } @Override public Class getObjectType() { return this.resultType; } /** * While this FactoryBean will often be used for singleton targets, * the invoked getters for the property path might return a new object * for each call, so we have to assume that we're not returning the * same object for each {@link #getObject()} call. */ @Override public boolean isSingleton() { return false; } }