Example usage for org.springframework.beans.factory BeanCreationNotAllowedException BeanCreationNotAllowedException

List of usage examples for org.springframework.beans.factory BeanCreationNotAllowedException BeanCreationNotAllowedException

Introduction

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

Prototype

public BeanCreationNotAllowedException(String beanName, String msg) 

Source Link

Document

Create a new BeanCreationNotAllowedException.

Usage

From source file:com.agileapes.motorex.cli.value.impl.SpringValueReaderContext.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    final String[] names = beanFactory.getBeanDefinitionNames();
    for (String name : names) {
        final BeanDefinition definition = beanFactory.getBeanDefinition(name);
        if (definition.isSingleton()) {
            final String className = definition.getBeanClassName();
            try {
                final Class<?> beanType = ClassUtils.forName(className, beanFactory.getBeanClassLoader());
                if (ValueReader.class.isAssignableFrom(beanType)) {
                    register(beanFactory.getBean(name, ValueReader.class));
                }//from ww  w  .  ja v a2s .co  m
            } catch (ClassNotFoundException e) {
                throw new BeanCreationNotAllowedException(name, "Failed to access bean");
            }
        }
    }
}

From source file:de.olivergierke.ninjector.FieldInjectionRejectingBeanPostProcessor.java

@Override
public Object postProcessBeforeInstantiation(final Class<?> beanClass, final String beanName)
        throws BeansException {

    ReflectionUtils.doWithFields(beanClass, new FieldCallback() {

        @Override/*from   w  ww .  j  a  v  a  2s  . c  o m*/
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {

            for (Class<? extends Annotation> annotationType : INJECTION_ANNOTATIONS) {
                if (AnnotationUtils.findAnnotation(field, annotationType) != null) {
                    throw new BeanCreationNotAllowedException(beanName, String.format(ERROR,
                            annotationType.getSimpleName(), field.getName(), beanClass.getSimpleName()));
                }
            }
        }
    });

    return null;
}

From source file:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.java

/**
 * Return the (raw) singleton object registered under the given name,
 * creating and registering a new one if none registered yet.
 * @param beanName the name of the bean//from   w  w w . j  a v a  2 s. c o m
 * @param singletonFactory the ObjectFactory to lazily create the singleton
 * with, if necessary
 * @return the registered singleton object
 */
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
    Assert.notNull(beanName, "Bean name must not be null");
    synchronized (this.singletonObjects) {
        Object singletonObject = this.singletonObjects.get(beanName);
        if (singletonObject == null) {
            if (this.singletonsCurrentlyInDestruction) {
                throw new BeanCreationNotAllowedException(beanName,
                        "Singleton bean creation not allowed while singletons of this factory are in destruction "
                                + "(Do not request a bean from a BeanFactory in a destroy method implementation!)");
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
            }
            beforeSingletonCreation(beanName);
            boolean newSingleton = false;
            boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
            if (recordSuppressedExceptions) {
                this.suppressedExceptions = new LinkedHashSet<>();
            }
            try {
                singletonObject = singletonFactory.getObject();
                newSingleton = true;
            } catch (IllegalStateException ex) {
                // Has the singleton object implicitly appeared in the meantime ->
                // if yes, proceed with it since the exception indicates that state.
                singletonObject = this.singletonObjects.get(beanName);
                if (singletonObject == null) {
                    throw ex;
                }
            } catch (BeanCreationException ex) {
                if (recordSuppressedExceptions) {
                    for (Exception suppressedException : this.suppressedExceptions) {
                        ex.addRelatedCause(suppressedException);
                    }
                }
                throw ex;
            } finally {
                if (recordSuppressedExceptions) {
                    this.suppressedExceptions = null;
                }
                afterSingletonCreation(beanName);
            }
            if (newSingleton) {
                addSingleton(beanName, singletonObject);
            }
        }
        return singletonObject;
    }
}