org.springframework.beans.factory.support.BeanDefinitionValidationException Java Examples

The following examples show how to use org.springframework.beans.factory.support.BeanDefinitionValidationException. 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: ExtProducerResetConfiguration.java    From rocketmq-spring with Apache License 2.0 6 votes vote down vote up
private void registerTemplate(String beanName, Object bean) {
    Class<?> clazz = AopProxyUtils.ultimateTargetClass(bean);

    if (!RocketMQTemplate.class.isAssignableFrom(bean.getClass())) {
        throw new IllegalStateException(clazz + " is not instance of " + RocketMQTemplate.class.getName());
    }

    ExtRocketMQTemplateConfiguration annotation = clazz.getAnnotation(ExtRocketMQTemplateConfiguration.class);
    GenericApplicationContext genericApplicationContext = (GenericApplicationContext) applicationContext;
    validate(annotation, genericApplicationContext);

    DefaultMQProducer mqProducer = createProducer(annotation);
    // Set instanceName same as the beanName
    mqProducer.setInstanceName(beanName);
    try {
        mqProducer.start();
    } catch (MQClientException e) {
        throw new BeanDefinitionValidationException(String.format("Failed to startup MQProducer for RocketMQTemplate {}",
            beanName), e);
    }
    RocketMQTemplate rocketMQTemplate = (RocketMQTemplate) bean;
    rocketMQTemplate.setProducer(mqProducer);
    rocketMQTemplate.setMessageConverter(rocketMQMessageConverter.getMessageConverter());
    log.info("Set real producer to :{} {}", beanName, annotation.value());
}
 
Example #2
Source File: WebappResourceManagerImpl.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
   if (applicationContext instanceof WebApplicationContext) {
      setWebApplicationContext((WebApplicationContext)applicationContext);
      if (getGlobalBeanId() != null) {
         WebappResourceManager other = (WebappResourceManager) ComponentManager.getInstance().get(getGlobalBeanId());
         if (other != null) {
            other.setWebApplicationContext((WebApplicationContext) applicationContext);
         }
         else {
            ComponentManager.getInstance().loadComponent(getGlobalBeanId(), this);
         }
      }
   }
   else {
      throw new BeanDefinitionValidationException(
         "WebappResourceManagerImpl must be used in a Web application context");
   }
}
 
Example #3
Source File: WebappResourceManagerImpl.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
   if (applicationContext instanceof WebApplicationContext) {
      setWebApplicationContext((WebApplicationContext)applicationContext);
      if (getGlobalBeanId() != null) {
         WebappResourceManager other = (WebappResourceManager) ComponentManager.getInstance().get(getGlobalBeanId());
         if (other != null) {
            other.setWebApplicationContext((WebApplicationContext) applicationContext);
         }
         else {
            ComponentManager.getInstance().loadComponent(getGlobalBeanId(), this);
         }
      }
   }
   else {
      throw new BeanDefinitionValidationException(
         "WebappResourceManagerImpl must be used in a Web application context");
   }
}
 
Example #4
Source File: DasTransactionalEnabler.java    From das with Apache License 2.0 5 votes vote down vote up
private void replaceBeanDefinition() {
    for(String beanName: getBeanDefinitionNames()) {
        BeanDefinition beanDef = getBeanDefinition(beanName);
        String beanClassName = beanDef.getBeanClassName();

        if(beanClassName == null || beanClassName.equals(BEAN_FACTORY_NAME) || isSpringSmartClassLoader(beanDef)) {
            continue;
        }

        Class beanClass;
        try {
            beanClass = Class.forName(beanDef.getBeanClassName());
        } catch (ClassNotFoundException e) {
            throw new BeanDefinitionValidationException("Cannot validate bean: " + beanName, e);
        }

        boolean annotated = false;
        for (Method method : beanClass.getMethods()) {
            if(isTransactionAnnotated(method)) {
                annotated = true;
                break;
            }
        }

        if(!annotated) {
            continue;
        }

        beanDef.setBeanClassName(BEAN_FACTORY_NAME);
        beanDef.setFactoryMethodName(FACTORY_METHOD_NAME);

        ConstructorArgumentValues cav = beanDef.getConstructorArgumentValues();

        if(cav.getArgumentCount() != 0) {
            throw new BeanDefinitionValidationException("The transactional bean can only be instantiated with default constructor.");
        }

        cav.addGenericArgumentValue(beanClass.getName());
    }
}
 
Example #5
Source File: ExtProducerResetConfiguration.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
private void validate(ExtRocketMQTemplateConfiguration annotation,
    GenericApplicationContext genericApplicationContext) {
    if (genericApplicationContext.isBeanNameInUse(annotation.value())) {
        throw new BeanDefinitionValidationException(String.format("Bean {} has been used in Spring Application Context, " +
                "please check the @ExtRocketMQTemplateConfiguration",
            annotation.value()));
    }
}
 
Example #6
Source File: ListenerContainerConfiguration.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
private void validate(RocketMQMessageListener annotation) {
    if (annotation.consumeMode() == ConsumeMode.ORDERLY &&
        annotation.messageModel() == MessageModel.BROADCASTING) {
        throw new BeanDefinitionValidationException(
            "Bad annotation definition in @RocketMQMessageListener, messageModel BROADCASTING does not support ORDERLY message!");
    }
}
 
Example #7
Source File: ConfigurationTypeInvalidConfigurationTest.java    From conf4j with MIT License 5 votes vote down vote up
@Test
public void shouldReportErrorWhenInvalidConfigurationIsRegisteredManually() {
    assertThrows(BeanDefinitionValidationException.class,
            () -> new AnnotationConfigApplicationContext(RegisterManually.class).close(),
            "not recognized as configuration type"
    );
}
 
Example #8
Source File: JupiterBeanDefinitionParser.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
    if (beanClass == JupiterSpringServer.class) {
        return parseJupiterServer(element, parserContext);
    } else if (beanClass == JupiterSpringClient.class) {
        return parseJupiterClient(element, parserContext);
    } else if (beanClass == JupiterSpringProviderBean.class) {
        return parseJupiterProvider(element, parserContext);
    } else if (beanClass == JupiterSpringConsumerBean.class) {
        return parseJupiterConsumer(element, parserContext);
    } else {
        throw new BeanDefinitionValidationException("Unknown class to definition: " + beanClass.getName());
    }
}
 
Example #9
Source File: ScriptFactoryPostProcessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
	// We only apply special treatment to ScriptFactory implementations here.
	if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
		return null;
	}

	Assert.state(this.beanFactory != null, "No BeanFactory set");
	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);
}
 
Example #10
Source File: ScriptFactoryPostProcessor.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
	// We only apply special treatment to ScriptFactory implementations here.
	if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
		return null;
	}

	Assert.state(this.beanFactory != null, "No BeanFactory set");
	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);
}
 
Example #11
Source File: ScriptFactoryPostProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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);
}
 
Example #12
Source File: ScriptFactoryPostProcessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example #13
Source File: DalTransactionalEnabler.java    From dal with Apache License 2.0 4 votes vote down vote up
private void replaceBeanDefinition() {
    for (String beanName : getBeanDefinitionNames()) {
        BeanDefinition beanDef = getBeanDefinition(beanName);
        String beanClassName = beanDef.getBeanClassName();

        if (beanClassName == null || beanClassName.equals(BEAN_FACTORY_NAME))
            continue;

        Class beanClass;
        try {
            beanClass = Class.forName(beanDef.getBeanClassName());
        } catch (ClassNotFoundException e) {
            throw new BeanDefinitionValidationException("Cannot validate bean: " + beanName, e);
        }

        boolean annotated = false;

        List<Method> methods = new ArrayList<>();
        ReflectUtils.addAllMethods(beanClass, methods);
        List<Method> unsupportedMethods = new ArrayList<>();

        for (int i = 0; i < methods.size(); i++) {
            Method currentMethod = methods.get(i);

            if (isTransactionAnnotated(currentMethod)) {
                if (Modifier.isFinal(currentMethod.getModifiers()) || Modifier.isStatic(currentMethod.getModifiers()) || Modifier.isPrivate(currentMethod.getModifiers()))
                    unsupportedMethods.add(currentMethod);
                annotated = true;
            }
        }

        if (!unsupportedMethods.isEmpty()){
            StringBuilder errMsg=new StringBuilder();
            errMsg.append(String.format("The Methods below are not supported in dal transaction due to private, final or static modifier, please use public,protected or default modifier instead:"));
            errMsg.append("\n");
            int index=1;
            for (Method method : unsupportedMethods) {
                errMsg.append(String.format("%d. %s", index, method.toString()));
                errMsg.append("\n");
                index++;
            }
            throw new DalRuntimeException(errMsg.toString());
        }

        if (!annotated)
            continue;

        beanDef.setBeanClassName(BEAN_FACTORY_NAME);
        beanDef.setFactoryMethodName(FACTORY_METHOD_NAME);

        ConstructorArgumentValues cav = beanDef.getConstructorArgumentValues();

        if (cav.getArgumentCount() != 0)
            throw new BeanDefinitionValidationException("The transactional bean can only be instantiated with default constructor.");

        cav.addGenericArgumentValue(beanClass.getName());
    }
}
 
Example #14
Source File: JupiterBeanDefinitionParser.java    From Jupiter with Apache License 2.0 4 votes vote down vote up
private static String checkAttribute(String attributeName, String attribute) {
    if (Strings.isNullOrEmpty(attribute)) {
        throw new BeanDefinitionValidationException("Attribute [" + attributeName + "] is required.");
    }
    return attribute;
}