Example usage for org.springframework.beans.factory.support BeanDefinitionValidationException BeanDefinitionValidationException

List of usage examples for org.springframework.beans.factory.support BeanDefinitionValidationException BeanDefinitionValidationException

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support BeanDefinitionValidationException BeanDefinitionValidationException.

Prototype

public BeanDefinitionValidationException(String msg) 

Source Link

Document

Create a new BeanDefinitionValidationException with the specified message.

Usage

From source file:de.blizzy.documentr.markdown.macro.MacroBeanPostProcessor.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
    Macro annotation = bean.getClass().getAnnotation(Macro.class);
    if (annotation != null) {
        if (StringUtils.isBlank(annotation.name())) {
            throw new BeanDefinitionValidationException("no macro name specified in @" + //$NON-NLS-1$
                    Macro.class.getSimpleName() + " annotation for bean: " + beanName); //$NON-NLS-1$
        }/*from  w  ww  .jav  a  2s .  co m*/

        if (!(bean instanceof IMacro)) {
            if (bean instanceof ISimpleMacro) {
                return createMacro((ISimpleMacro) bean, annotation);
            } else if (bean instanceof IMacroRunnable) {
                @SuppressWarnings("unchecked")
                Class<? extends IMacroRunnable> clazz = (Class<? extends IMacroRunnable>) bean.getClass();
                return createMacro(clazz, annotation);
            } else {
                throw new BeanNotOfRequiredTypeException(beanName, ISimpleMacro.class, bean.getClass());
            }
        } else {
            throw new BeanNotOfRequiredTypeException(beanName, ISimpleMacro.class, bean.getClass());
        }
    } else {
        return bean;
    }
}

From source file:uk.co.unclealex.process.spring.PackageCheckingPostProcessor.java

/**
 * {@inheritDoc}/*from  w  w w .  jav a  2  s.  c  o m*/
 */
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    SortedSet<String> missingPackages = getPackageChecker().listMissingPackages(bean);
    if (!missingPackages.isEmpty()) {
        throw new BeanDefinitionValidationException(
                String.format("Bean %s requires the following missing packages: %s", beanName,
                        Joiner.on(", ").join(missingPackages)));
    }
    return bean;
}

From source file:org.brushingbits.jnap.util.BeanLocator.java

public <T> T getBean(Class<T> requiredType) {
    String[] beanNames = applicationContext.getBeanNamesForType(requiredType);
    if (beanNames == null || beanNames.length == 0) {
        throw new NoSuchBeanDefinitionException(requiredType, "No bean of the required type was found.");
    }/*  ww w  .  j ava2 s .  co  m*/
    if (beanNames.length > 1) {
        throw new BeanDefinitionValidationException(
                "There are more than " + beanNames.length + "beans implementing the required type. "
                        + "Use 'getBeanByName(String)' or getBean(String, Class) instead.");
    }
    return (T) getBean(beanNames[0], requiredType);
}

From source file:com.github.steveash.spring.WiringFactoryBeanFactoryPostProcessor.java

private void addPrototypeDef(ConfigurableListableBeanFactory beanFactory, String beanDefName,
        Class<?> protoBeanClass) {
    String beanName = getPrototypeBeanNameFromBeanClass(protoBeanClass);
    if (beanFactory.containsBeanDefinition(beanName)) {
        throw new BeanDefinitionValidationException("Trying to register a bean definition for a synthetic "
                + "prototype bean with name " + beanName + " due to the bean factory of name " + beanDefName
                + " but a bean with this name already exists!");
    }//from  ww  w .j a  v a  2 s  . co m

    GenericBeanDefinition protoBean = new GenericBeanDefinition();
    protoBean.setLazyInit(true);
    protoBean.setBeanClass(protoBeanClass);
    protoBean.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    protoBean.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    protoBean.setBeanClassName(protoBeanClass.getName());

    log.debug("Dynamically adding prototype bean {} from factory {}", beanName, beanDefName);
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
    registry.registerBeanDefinition(beanName, protoBean);
}

From source file:com.griddynamics.banshun.StrictContextParentBean.java

@Override
protected List<String> analyzeDependencies(List<String> configLocations) throws Exception {
    ContextAnalyzer analyzer = new ContextAnalyzer();
    List<Exception> exceptions = new LinkedList<>();

    List<String> limitedLocations = new ArrayList<>();
    for (String loc : configLocations) {
        BeanDefinitionRegistry beanFactory = getBeanFactory(loc);

        String[] beanNames = beanFactory.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
            try {
                if (isExport(beanDefinition)) {
                    analyzer.addExport(beanDefinition);
                    if (checkForRunOnly(beanName)) {
                        limitedLocations.add(loc);
                    }// ww  w .  j ava2  s .co  m
                } else if (isImport(beanDefinition)) {
                    analyzer.addImport(beanDefinition);
                } else if (beanDefinition.getBeanClassName() != null) {
                    checkClassExist(loc, beanName, beanDefinition.getBeanClassName());
                }
            } catch (Exception ex) {
                exceptions.add(ex);
            }
        }
    }

    analyzer.areThereExportsWithoutImport();

    if (analyzer.areThereImportsWithoutExports() || !analyzer.areImportsTypesCorrect()) {
        exceptions.add(new BeanDefinitionValidationException(
                "There are severe errors while parsing contexts. See logs for details"));
    }

    if (!exceptions.isEmpty()) {
        for (Exception exception : exceptions) {
            log.error(exception.getMessage());
        }
        throw exceptions.get(0);
    }

    DependencySorter sorter = new DependencySorter(configLocations.toArray(new String[0]),
            analyzer.getImports(), analyzer.getExports());
    sorter.setProhibitCycles(prohibitCycles);

    locationsGraph = new LocationsGraph(analyzer.getImports(), analyzer.getExports());
    List<String> analyzedConfigLocations = locationsGraph.filterConfigLocations(limitedLocations,
            sorter.sort());

    log.info("ordered list of the contexts: {}", analyzedConfigLocations);

    return analyzedConfigLocations;
}

From source file:org.raistlic.spring.test.flyway.FlywayTestExecutionListenerTest.java

@Test(expected = FlywayTestExecutionException.class)
public void getFlywayBeanWhenMultipleInstancesInContextAndNameIsEmpty() {

    ApplicationContext applicationContext = mock(ApplicationContext.class);
    Flyway flyway1 = mock(Flyway.class);
    Flyway flyway2 = mock(Flyway.class);
    doReturn(new String[] { "flyway1", "flyway2" }).when(applicationContext).getBeanNamesForType(Flyway.class);
    doAnswer(invocationOnMock -> {//from  w w  w . j  a  v a2 s.  c o m

        @SuppressWarnings("unchecked")
        String name = (String) invocationOnMock.getArguments()[0];
        if (name == null || name.isEmpty()) {
            throw new RuntimeException("test exception");
        }
        switch (name) {
        case "flyway1":
            return flyway1;
        case "flyway2":
            return flyway2;
        default:
            throw new BeanDefinitionValidationException(
                    "test exception: Flyway bean not found with name: " + name);
        }
    }).when(applicationContext).getBean(anyString(), eq(Flyway.class));
    doAnswer(invocationOnMock -> {

        @SuppressWarnings("unchecked")
        String name = (String) invocationOnMock.getArguments()[0];
        if (name == null || name.isEmpty()) {
            throw new RuntimeException("test exception");
        }
        switch (name) {
        case "flyway1":
            return flyway1;
        case "flyway2":
            return flyway2;
        default:
            throw new BeanDefinitionValidationException(
                    "test exception: Flyway bean not found with name: " + name);
        }
    }).when(applicationContext).getBean(anyString());
    doThrow(new BeanDefinitionValidationException(
            "test exception: multiple beans exists for the specified type")).when(applicationContext)
                    .getBean(Flyway.class);

    FlywayTestExecutionListener.getFlywayBean(applicationContext, "");
}

From source file:org.iff.infra.util.spring.script.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;
    }//  w w w.  ja v  a 2s .c o m

    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);
    String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE);//add by tylerchen
    if (refreshCheckDelay >= 0 || "groovy".equals(language)) {
        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);//remove by tylerchen
        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);//remove by tylerchen
        ts.setRefreshCheckDelay(-1);//add by tylerchen
        return createRefreshableProxy(ts, interfaces, proxyTargetClass);
    }

    if (isFactoryBean) {
        scriptedObjectBeanName = BeanFactory.FACTORY_BEAN_PREFIX + scriptedObjectBeanName;
    }
    return this.scriptBeanFactory.getBean(scriptedObjectBeanName);
}

From source file:org.raistlic.spring.test.flyway.FlywayTestExecutionListenerTest.java

@Test(expected = FlywayTestExecutionException.class)
public void getFlywayBeanWhenMultipleInstancesInContextAndNameIsNull() {

    ApplicationContext applicationContext = mock(ApplicationContext.class);
    Flyway flyway1 = mock(Flyway.class);
    Flyway flyway2 = mock(Flyway.class);
    doReturn(new String[] { "flyway1", "flyway2" }).when(applicationContext).getBeanNamesForType(Flyway.class);
    doAnswer(invocationOnMock -> {/*from   ww w. jav a  2  s  . c  o  m*/

        @SuppressWarnings("unchecked")
        String name = (String) invocationOnMock.getArguments()[0];
        if (name == null || name.isEmpty()) {
            throw new RuntimeException("test exception");
        }
        switch (name) {
        case "flyway1":
            return flyway1;
        case "flyway2":
            return flyway2;
        default:
            throw new BeanDefinitionValidationException(
                    "test exception: Flyway bean not found with name: " + name);
        }
    }).when(applicationContext).getBean(anyString(), eq(Flyway.class));
    doAnswer(invocationOnMock -> {

        @SuppressWarnings("unchecked")
        String name = (String) invocationOnMock.getArguments()[0];
        if (name == null || name.isEmpty()) {
            throw new RuntimeException("test exception");
        }
        switch (name) {
        case "flyway1":
            return flyway1;
        case "flyway2":
            return flyway2;
        default:
            throw new BeanDefinitionValidationException(
                    "test exception: Flyway bean not found with name: " + name);
        }
    }).when(applicationContext).getBean(anyString());
    doThrow(new BeanDefinitionValidationException(
            "test exception: multiple beans exists for the specified type")).when(applicationContext)
                    .getBean(Flyway.class);

    FlywayTestExecutionListener.getFlywayBean(applicationContext, null);
}

From source file:org.raistlic.spring.test.flyway.FlywayTestExecutionListenerTest.java

@Test(expected = FlywayTestExecutionException.class)
public void getFlywayBeanWhenMultipleInstancesInContextAndNameNotMatchAnyOne() {

    ApplicationContext applicationContext = mock(ApplicationContext.class);
    Flyway flyway1 = mock(Flyway.class);
    Flyway flyway2 = mock(Flyway.class);
    doReturn(new String[] { "flyway1", "flyway2" }).when(applicationContext).getBeanNamesForType(Flyway.class);
    doAnswer(invocationOnMock -> {// w  w w.jav a2s  .  co m

        @SuppressWarnings("unchecked")
        String name = (String) invocationOnMock.getArguments()[0];
        if (name == null || name.isEmpty()) {
            throw new RuntimeException("test exception");
        }
        switch (name) {
        case "flyway1":
            return flyway1;
        case "flyway2":
            return flyway2;
        default:
            throw new BeanDefinitionValidationException(
                    "test exception: Flyway bean not found with name: " + name);
        }
    }).when(applicationContext).getBean(anyString(), eq(Flyway.class));
    doAnswer(invocationOnMock -> {

        @SuppressWarnings("unchecked")
        String name = (String) invocationOnMock.getArguments()[0];
        if (name == null || name.isEmpty()) {
            throw new RuntimeException("test exception");
        }
        switch (name) {
        case "flyway1":
            return flyway1;
        case "flyway2":
            return flyway2;
        default:
            throw new BeanDefinitionValidationException(
                    "test exception: Flyway bean not found with name: " + name);
        }
    }).when(applicationContext).getBean(anyString());
    doThrow(new BeanDefinitionValidationException(
            "test exception: multiple beans exists for the specified type")).when(applicationContext)
                    .getBean(Flyway.class);

    FlywayTestExecutionListener.getFlywayBean(applicationContext, "notFound");
}

From source file:org.raistlic.spring.test.flyway.FlywayTestExecutionListenerTest.java

@Test
public void getFlywayBeanWhenMultipleInstancesInContextAndNameMatchesOneBean() {

    ApplicationContext applicationContext = mock(ApplicationContext.class);
    Flyway flyway1 = mock(Flyway.class);
    Flyway flyway2 = mock(Flyway.class);
    doReturn(new String[] { "flyway1", "flyway2" }).when(applicationContext).getBeanNamesForType(Flyway.class);
    doAnswer(invocationOnMock -> {/*w w  w  .  jav a 2s .c o m*/

        @SuppressWarnings("unchecked")
        String name = (String) invocationOnMock.getArguments()[0];
        if (name == null || name.isEmpty()) {
            throw new RuntimeException("test exception");
        }
        switch (name) {
        case "flyway1":
            return flyway1;
        case "flyway2":
            return flyway2;
        default:
            throw new BeanDefinitionValidationException(
                    "test exception: Flyway bean not found with name: " + name);
        }
    }).when(applicationContext).getBean(anyString(), eq(Flyway.class));
    doAnswer(invocationOnMock -> {

        @SuppressWarnings("unchecked")
        String name = (String) invocationOnMock.getArguments()[0];
        if (name == null || name.isEmpty()) {
            throw new RuntimeException("test exception");
        }
        switch (name) {
        case "flyway1":
            return flyway1;
        case "flyway2":
            return flyway2;
        default:
            throw new BeanDefinitionValidationException(
                    "test exception: Flyway bean not found with name: " + name);
        }
    }).when(applicationContext).getBean(anyString());
    doThrow(new BeanDefinitionValidationException(
            "test exception: multiple beans exists for the specified type")).when(applicationContext)
                    .getBean(Flyway.class);

    Flyway actual = FlywayTestExecutionListener.getFlywayBean(applicationContext, "flyway2");

    assertThat(actual).isEqualTo(flyway2);
}