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

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

Introduction

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

Prototype

public NoSuchBeanDefinitionException(ResolvableType type, String message) 

Source Link

Document

Create a new NoSuchBeanDefinitionException .

Usage

From source file:com.shouyingbao.pbs.core.framework.spring.context.utils.SpringContextUtil.java

/**
 * ?Class?//  ww  w.ja  va2s. c  o m
 * @param type
 * @return
 */
public static <T> T getBeanOfType(Class<T> type) {
    Map<String, T> beans = getBeansOfType(type);
    if (beans.size() == 0) {
        throw new NoSuchBeanDefinitionException(type,
                "Unsatisfied dependency of type [" + type + "]: expected at least 1 matching bean");
    }
    if (beans.size() > 1) {
        throw new NoSuchBeanDefinitionException(type,
                "expected single matching bean but found " + beans.size() + ": " + beans.keySet());
    }
    return beans.values().iterator().next();
}

From source file:org.simbasecurity.core.locator.SpringAwareLocator.java

@Override
public <B> B locate(Class<B> type) {
    String[] names = getApplicationContext().getBeanNamesForType(type);

    if (names.length != 1) {
        throw new NoSuchBeanDefinitionException(type,
                String.format("found {0} definitions instead of 1: {1}", names.length, Arrays.asList(names)));
    }// www .jav a  2s.co m

    return locate(names[0], type);
}

From source file:com.laxser.blitz.lama.provider.jdbc.JdbcDataAccessProvider.java

@Override
protected DataSourceFactory createDataSourceFactory() {
    Map<?, ?> beansOfType = applicationContext.getBeansOfType(DataSourceFactory.class);
    if (beansOfType.size() > 1) {
        throw new NoSuchBeanDefinitionException(DataSourceFactory.class,
                "expected single bean but found " + beansOfType.size());
    } else if (beansOfType.size() == 1) {
        return (DataSourceFactory) beansOfType.values().iterator().next();
    }/*from   w ww  . j a  va  2s  .c o m*/
    SpringDataSourceFactory dataSourceFactory = new SpringDataSourceFactory();
    dataSourceFactory.setApplicationContext(applicationContext);
    return dataSourceFactory;
}

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  .  jav a  2s .  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:org.simbasecurity.core.locator.SpringAwareLocator.java

@Override
public Object locate(String naam) {
    Object bean = getApplicationContext().getBean(naam);

    if (bean == null) {
        throw new NoSuchBeanDefinitionException(naam, String.format("found null for: {0}", naam));
    }/*w w  w  .j  av  a  2s  .c  o  m*/

    return bean;
}

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

public Object getTarget() throws BeansException {
    Object localTarget = target.get();

    if (localTarget == null) {
        if (!rootContext.containsBean(exportProxyName)) {
            throw new NoSuchBeanDefinitionException(exportProxyName, String
                    .format("can't find export declaration for lookup(%s, %s)", serviceName, serviceInterface));
        }//www. ja va  2  s  . c o  m
        ExportTargetSource exportProxy = rootContext.getBean(exportProxyName, ExportTargetSource.class);

        // verify if service interfaces on both sides are compatible
        if (!serviceInterface.isAssignableFrom(exportProxy.getTargetClass())) {
            throw new BeanNotOfRequiredTypeException(serviceName, serviceInterface,
                    exportProxy.getTargetClass());
        }

        if (target.compareAndSet(null, localTarget = exportProxy.getTarget())) {
            return localTarget;

        } else {
            // log potentially redundant instance initialization
            log.warn("Bean {} was created earlier", serviceName);
            return target.get();
        }
    }
    return localTarget;
}

From source file:ch.nydi.spring.context.support.PrimaryResolverListableBeanFactory.java

@Override
public <T> T getBean(Class<T> requiredType) throws BeansException {
    Assert.notNull(requiredType, "Required type must not be null");
    String[] beanNames = getBeanNamesForType(requiredType);
    String primaryCandidate = null;
    if (beanNames.length > 1) {
        final ArrayList<String> autowireCandidates = new ArrayList<String>();

        for (final String beanName : beanNames) {
            final BeanDefinition beanDefinition = getBeanDefinition(beanName);
            if (beanDefinition.isAutowireCandidate()) {
                autowireCandidates.add(beanName);
                if (beanDefinition.isPrimary()) {
                    primaryCandidate = beanName;
                }/*ww w .  java 2  s . c om*/
            }
        }
        if (autowireCandidates.size() > 0) {
            beanNames = autowireCandidates.toArray(new String[autowireCandidates.size()]);
        }
    }
    if (beanNames.length == 1) {
        return getBean(beanNames[0], requiredType);
    } else if (beanNames.length > 1) { // more than one bean defined, lookup primary candidate
        if (primaryCandidate != null) {
            return getBean(primaryCandidate, requiredType);
        }
        throw new NoSuchBeanDefinitionException(requiredType, "expected single bean but found "
                + beanNames.length + ": " + StringUtils.arrayToCommaDelimitedString(beanNames));
    } else if (beanNames.length == 0 && getParentBeanFactory() != null) {
        return getParentBeanFactory().getBean(requiredType);
    } else {
        throw new NoSuchBeanDefinitionException(requiredType, "expected single bean but found "
                + beanNames.length + ": " + StringUtils.arrayToCommaDelimitedString(beanNames));
    }
}

From source file:net.easysmarthouse.service.context.ProxiedResolverGenericXmlApplicationContext.java

@Override
public <T extends Object> T getBean(Class<T> requiredType) throws BeansException {
    Assert.notNull(requiredType, "Required type must not be null");
    String[] beanNames = getBeanNamesForType(requiredType);
    String primaryCandidate = null;

    if (beanNames.length > 1) {
        ArrayList<String> autowireCandidates = new ArrayList<String>();

        for (String beanName : beanNames) {
            BeanDefinition beanDefinition = getBeanDefinition(beanName);
            if (beanDefinition.isAutowireCandidate()) {
                autowireCandidates.add(beanName);
                if (beanDefinition.isPrimary()) {
                    primaryCandidate = beanName;
                }/* w w  w.jav  a 2  s . c  om*/
            }
        }

        for (String autowireCandidate : autowireCandidates) {
            if (autowireCandidates.contains(autowireCandidate + "Proxied")) {
                primaryCandidate = autowireCandidate;
            }
        }

        if (autowireCandidates.size() > 0) {
            beanNames = autowireCandidates.toArray(new String[autowireCandidates.size()]);
        }
    }

    if (beanNames.length == 1) {
        return getBean(beanNames[0], requiredType);

    } else if (beanNames.length > 1) {
        // more than one bean defined, lookup primary candidate
        if (primaryCandidate != null) {
            return getBean(primaryCandidate, requiredType);
        }
        throw new NoSuchBeanDefinitionException(requiredType, "expected single bean but found "
                + beanNames.length + ": " + StringUtils.arrayToCommaDelimitedString(beanNames));

    } else if (beanNames.length == 0 && getParentBeanFactory() != null) {
        return getParentBeanFactory().getBean(requiredType);

    } else {
        throw new NoSuchBeanDefinitionException(requiredType, "expected single bean but found "
                + beanNames.length + ": " + StringUtils.arrayToCommaDelimitedString(beanNames));

    }
}

From source file:uk.org.ponder.rsac.support.BeanDefUtil.java

static AbstractBeanDefinition getMergedBeanDefinition(ConfigurableListableBeanFactory factory, String beanName,
        BeanDefinition bd) throws BeansException {

    if (!(bd instanceof AbstractBeanDefinition)) {
        throw new IllegalArgumentException("Bean definition " + beanName + " of " + bd.getClass()
                + " which is not descended from AbstractBeanDefinition can not be recognised");
    }/*  w  w w . j  ava  2  s .  co m*/
    AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
    String parentName = getParentDefinitionName(abd);
    if (parentName == null) {
        return abd;
    } else {
        AbstractBeanDefinition pbd = null;
        if (!beanName.equals(parentName)) {
            pbd = getMergedBeanDefinition(factory, parentName, true);
        } else {
            if (factory.getParentBeanFactory() instanceof ConfigurableListableBeanFactory) {
                ConfigurableListableBeanFactory parentFactory = (ConfigurableListableBeanFactory) factory
                        .getParentBeanFactory();
                pbd = getMergedBeanDefinition(parentFactory, parentName, true);
            } else {
                throw new NoSuchBeanDefinitionException(parentName,
                        "Parent name '" + parentName + "' is equal to bean name '" + beanName
                                + "' - cannot be resolved without an AbstractBeanFactory parent");
            }
        }

        // deep copy with overridden values
        pbd.overrideFrom(abd);
        return pbd;
    }

}

From source file:org.jboss.spring.facade.KernelControllerListableBeanFactory.java

public <T> T getBean(Class<T> tClass) throws BeansException {
    Set<ControllerContext> foundContexts = new HashSet<ControllerContext>();
    for (ControllerContext controllerContext : kernelController.getContexts(tClass,
            ControllerState.INSTALLED)) {

    }// ww  w .ja v  a2 s  .  co m
    if (foundContexts.size() != 1) {
        throw new NoSuchBeanDefinitionException(tClass, "No controller of this class found");
    } else {
        return (T) foundContexts.iterator().next().getTarget();
    }
}