Search in sources :

Example 1 with BeanDefinitionValidationException

use of org.springframework.beans.factory.support.BeanDefinitionValidationException in project spring-framework by spring-projects.

the class ScriptFactoryPostProcessor method postProcessBeforeInstantiation.

@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
    // We only apply special treatment to ScriptFactory implementations here.
    if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
        return null;
    }
    BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName);
    String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName;
    String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName;
    prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName);
    ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
    ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
    boolean isFactoryBean = false;
    try {
        Class<?> scriptedObjectType = scriptFactory.getScriptedObjectType(scriptSource);
        // Returned type may be null if the factory is unable to determine the type.
        if (scriptedObjectType != null) {
            isFactoryBean = FactoryBean.class.isAssignableFrom(scriptedObjectType);
        }
    } catch (Exception ex) {
        throw new BeanCreationException(beanName, "Could not determine scripted object type for " + scriptFactory, ex);
    }
    long refreshCheckDelay = resolveRefreshCheckDelay(bd);
    if (refreshCheckDelay >= 0) {
        Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
        RefreshableScriptTargetSource ts = new RefreshableScriptTargetSource(this.scriptBeanFactory, scriptedObjectBeanName, scriptFactory, scriptSource, isFactoryBean);
        boolean proxyTargetClass = resolveProxyTargetClass(bd);
        String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE);
        if (proxyTargetClass && (language == null || !language.equals("groovy"))) {
            throw new BeanDefinitionValidationException("Cannot use proxyTargetClass=true with script beans where language is not 'groovy': '" + language + "'");
        }
        ts.setRefreshCheckDelay(refreshCheckDelay);
        return createRefreshableProxy(ts, interfaces, proxyTargetClass);
    }
    if (isFactoryBean) {
        scriptedObjectBeanName = BeanFactory.FACTORY_BEAN_PREFIX + scriptedObjectBeanName;
    }
    return this.scriptBeanFactory.getBean(scriptedObjectBeanName);
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanDefinitionValidationException(org.springframework.beans.factory.support.BeanDefinitionValidationException) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) BeanDefinitionValidationException(org.springframework.beans.factory.support.BeanDefinitionValidationException) BeanCurrentlyInCreationException(org.springframework.beans.factory.BeanCurrentlyInCreationException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) FactoryBean(org.springframework.beans.factory.FactoryBean) ScriptSource(org.springframework.scripting.ScriptSource) ScriptFactory(org.springframework.scripting.ScriptFactory)

Aggregations

BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 BeanCurrentlyInCreationException (org.springframework.beans.factory.BeanCurrentlyInCreationException)1 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)1 FactoryBean (org.springframework.beans.factory.FactoryBean)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)1 BeanDefinitionValidationException (org.springframework.beans.factory.support.BeanDefinitionValidationException)1 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)1 ScriptFactory (org.springframework.scripting.ScriptFactory)1 ScriptSource (org.springframework.scripting.ScriptSource)1