类org.springframework.beans.factory.support.BeanDefinitionValidationException源码实例Demo

下面列出了怎么用org.springframework.beans.factory.support.BeanDefinitionValidationException的API类实例代码及写法,或者点击链接到github查看源代码。

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());
}
 
源代码2 项目: sakai   文件: WebappResourceManagerImpl.java
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");
   }
}
 
源代码3 项目: sakai   文件: WebappResourceManagerImpl.java
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");
   }
}
 
源代码4 项目: das   文件: DasTransactionalEnabler.java
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());
    }
}
 
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()));
    }
}
 
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!");
    }
}
 
@Test
public void shouldReportErrorWhenInvalidConfigurationIsRegisteredManually() {
    assertThrows(BeanDefinitionValidationException.class,
            () -> new AnnotationConfigApplicationContext(RegisterManually.class).close(),
            "not recognized as configuration type"
    );
}
 
源代码8 项目: Jupiter   文件: JupiterBeanDefinitionParser.java
@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());
    }
}
 
@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);
}
 
@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);
}
 
源代码11 项目: lams   文件: ScriptFactoryPostProcessor.java
@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);
}
 
@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);
}
 
源代码13 项目: dal   文件: DalTransactionalEnabler.java
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());
    }
}
 
源代码14 项目: Jupiter   文件: JupiterBeanDefinitionParser.java
private static String checkAttribute(String attributeName, String attribute) {
    if (Strings.isNullOrEmpty(attribute)) {
        throw new BeanDefinitionValidationException("Attribute [" + attributeName + "] is required.");
    }
    return attribute;
}
 
 同包方法