Example usage for org.springframework.beans BeanInstantiationException BeanInstantiationException

List of usage examples for org.springframework.beans BeanInstantiationException BeanInstantiationException

Introduction

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

Prototype

public BeanInstantiationException(Method constructingMethod, String msg, @Nullable Throwable cause) 

Source Link

Document

Create a new BeanInstantiationException.

Usage

From source file:org.lightadmin.core.config.context.LightAdminRepositoryRestMvcConfiguration.java

@Bean
public Repositories repositories() {
    try {//from w  w w . j  av a 2s.c  om
        return new RepositoriesFactoryBean(beanFactory).getObject();
    } catch (Exception e) {
        throw new BeanInstantiationException(Repositories.class, "Repositories bean instantiation problem!", e);
    }
}

From source file:gDao.genericDao.SimpleDaoHandler.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    try {/*from  w w  w .j  av a  2 s  .  co m*/
        injectGDao(bean);
    } catch (Exception e) {
        throw new BeanInstantiationException(bean.getClass(), e.getMessage(), e.getCause());
    }
    return bean;
}

From source file:com.frank.search.solr.server.support.SolrClientUtils.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private static SolrClient cloneEmbeddedSolrServer(SolrClient solrClient, String core) {

    CoreContainer coreContainer = ((EmbeddedSolrServer) solrClient).getCoreContainer();
    try {/*from  w  w w  .j  a  v a 2  s  .c  om*/
        Constructor constructor = ClassUtils.getConstructorIfAvailable(solrClient.getClass(),
                CoreContainer.class, String.class);
        return (SolrClient) BeanUtils.instantiateClass(constructor, coreContainer, core);
    } catch (Exception e) {
        throw new BeanInstantiationException(solrClient.getClass(),
                "Cannot create instace of " + solrClient.getClass() + ".", e);
    }
}

From source file:com.frank.search.solr.server.support.SolrClientUtils.java

private static SolrClient cloneHttpSolrClient(SolrClient solrClient, String core) {
    if (solrClient == null) {
        return null;
    }//  w  w w  .  j  a  v  a 2 s .  c  o m

    Method baseUrlGetterMethod = ClassUtils.getMethodIfAvailable(solrClient.getClass(), "getBaseURL");
    if (baseUrlGetterMethod == null) {
        return null;
    }

    String baseUrl = (String) ReflectionUtils.invokeMethod(baseUrlGetterMethod, solrClient);
    String url = appendCoreToBaseUrl(baseUrl, core);

    try {

        HttpClient clientToUse = readAndCloneHttpClient(solrClient);

        if (clientToUse != null) {
            Constructor<? extends SolrClient> constructor = (Constructor<? extends SolrClient>) ClassUtils
                    .getConstructorIfAvailable(solrClient.getClass(), String.class, HttpClient.class);
            if (constructor != null) {
                return (SolrClient) BeanUtils.instantiateClass(constructor, url, clientToUse);
            }
        }

        Constructor<? extends SolrClient> constructor = (Constructor<? extends SolrClient>) ClassUtils
                .getConstructorIfAvailable(solrClient.getClass(), String.class);
        return (SolrClient) BeanUtils.instantiateClass(constructor, url);
    } catch (Exception e) {
        throw new BeanInstantiationException(solrClient.getClass(),
                "Cannot create instace of " + solrClient.getClass() + ". ", e);
    }
}

From source file:com.frank.search.solr.server.support.SolrClientUtils.java

private static LBHttpSolrClient cloneLBHttpSolrClient(SolrClient solrClient, String core) {
    if (solrClient == null) {
        return null;
    }//from w  w  w  .  j a v  a  2s .c o  m

    LBHttpSolrClient clone = null;
    try {
        if (VersionUtil.isSolr3XAvailable()) {
            clone = cloneSolr3LBHttpServer(solrClient, core);
        } else if (VersionUtil.isSolr4XAvailable()) {
            clone = cloneSolr4LBHttpServer(solrClient, core);
        }
    } catch (Exception e) {
        throw new BeanInstantiationException(solrClient.getClass(),
                "Cannot create instace of " + solrClient.getClass() + ". ", e);
    }
    Object o = readField(solrClient, "interval");
    if (o != null) {
        clone.setAliveCheckInterval(Integer.valueOf(o.toString()).intValue());
    }
    return clone;
}

From source file:com.gzj.tulip.jade.rowmapper.BeanPropertyRowMapper.java

/**
 * /*from   w  ww .ja  v a  2  s.c om*/
 * @param clazz
 * @return
 * @throws BeanInstantiationException
 * @see {@link BeanUtils#instantiateClass(Class)}
 */
private static Object instantiateClass(Class<?> clazz) throws BeanInstantiationException {
    /*- spring's BeanUtils.instantiateClass()
     Assert.notNull(clazz, "Class must not be null");
    if (clazz.isInterface()) {
    throw new BeanInstantiationException(clazz, "Specified class is an interface");
    }
    try {
    return instantiateClass(clazz.getDeclaredConstructor((Class[]) null), null);
    }
    catch (NoSuchMethodException ex) {
    throw new BeanInstantiationException(clazz, "No default constructor found", ex);
    }*/

    try {
        return clazz.newInstance();
    } catch (Exception ex) {
        throw new BeanInstantiationException(clazz, ex.getMessage(), ex);
    }
}

From source file:com.laxser.blitz.lama.core.RowMapperFactoryImpl.java

@Override
public RowMapper getRowMapper(Modifier modifier) {
    RowHandler rowHandler = modifier.getAnnotation(RowHandler.class);
    if (rowHandler != null) {
        if (rowHandler.rowMapper() != RowHandler.ByDefault.class) {
            try {
                RowMapper rowMapper = rowHandler.rowMapper().newInstance();
                if (logger.isInfoEnabled()) {
                    logger.info("using rowMapper " + rowMapper + " for " + modifier);
                }//  w  ww.  jav a 2 s . c o m

                return rowMapper;
            } catch (Exception ex) {
                throw new BeanInstantiationException(rowHandler.rowMapper(), ex.getMessage(), ex);
            }
        }
    }
    //

    Class<?> returnClassType = modifier.getReturnType();
    Class<?> rowType = getRowType(modifier);

    // BUGFIX: SingleColumnRowMapper ?  Primitive Type 
    if (rowType.isPrimitive()) {
        rowType = ClassUtils.primitiveToWrapper(rowType);
    }

    // ?  RowMapper
    RowMapper rowMapper;

    // ?(?2Map)
    if (TypeUtils.isColumnType(rowType)) {
        if (returnClassType == Map.class) {
            rowMapper = new MapEntryColumnRowMapper(modifier, rowType);
        } else {
            rowMapper = new SingleColumnRowMapper(rowType);
        }
    }
    // Bean??????
    else {
        if (rowType == Map.class) {
            rowMapper = new ColumnMapRowMapper();
        } else if (rowType.isArray()) {
            rowMapper = new ArrayRowMapper(rowType);
        } else if ((rowType == List.class) || (rowType == Collection.class)) {
            rowMapper = new ListRowMapper(modifier);
        } else if (rowType == Set.class) {
            rowMapper = new SetRowMapper(modifier);
        } else {
            boolean checkColumns = (rowHandler == null) ? true : rowHandler.checkColumns();
            boolean checkProperties = (rowHandler == null) ? false : rowHandler.checkProperties();
            String key = rowType.getName() + "[checkColumns=" + checkColumns + "&checkProperties="
                    + checkProperties + "]";
            rowMapper = rowMappers.get(key);
            if (rowMapper == null) {
                rowMapper = new BeanPropertyRowMapper(rowType, checkColumns, checkProperties); // jade's BeanPropertyRowMapper here
                rowMappers.put(key, rowMapper);
            }
        }
        // DAOMaprowMapper?Map.Entry
        if (returnClassType == Map.class) {
            rowMapper = new MapEntryRowMapper(modifier, rowMapper);
        }
    }

    if (logger.isInfoEnabled()) {
        logger.info("using rowMapper " + rowMapper + " for " + modifier);
    }

    return rowMapper;
}

From source file:net.paoding.rose.jade.rowmapper.DefaultRowMapperFactory.java

@Override
public RowMapper getRowMapper(StatementMetaData modifier) {
    RowHandler rowHandler = modifier.getAnnotation(RowHandler.class);
    if (rowHandler != null) {
        if (rowHandler.rowMapper() != RowHandler.ByDefault.class) {
            try {
                RowMapper rowMapper = rowHandler.rowMapper().newInstance();
                if (logger.isInfoEnabled()) {
                    logger.info("using rowMapper " + rowMapper + " for " + modifier);
                }/*from  w  w w .j  a  v  a2 s .com*/

                return rowMapper;
            } catch (Exception ex) {
                throw new BeanInstantiationException(rowHandler.rowMapper(), ex.getMessage(), ex);
            }
        }
    }
    //

    Class<?> returnClassType = modifier.getMethod().getReturnType();
    Class<?> rowType = getRowType(modifier);

    // BUGFIX: SingleColumnRowMapper ?  Primitive Type 
    if (rowType.isPrimitive()) {
        rowType = ClassUtils.primitiveToWrapper(rowType);
    }

    // ?  RowMapper
    RowMapper rowMapper;

    // ?(?2Map)
    if (TypeUtils.isColumnType(rowType)) {
        if (returnClassType == Map.class) {
            rowMapper = new MapEntryColumnRowMapper(modifier, rowType);
        } else {
            rowMapper = new SingleColumnRowMapper(rowType);
        }
    }
    // Bean??????
    else {
        if (rowType == Map.class) {
            rowMapper = new ColumnMapRowMapper();
        } else if (rowType.isArray()) {
            rowMapper = new ArrayRowMapper(rowType);
        } else if ((rowType == List.class) || (rowType == Collection.class)) {
            rowMapper = new ListRowMapper(modifier);
        } else if (rowType == Set.class) {
            rowMapper = new SetRowMapper(modifier);
        } else {
            boolean checkColumns = (rowHandler == null) ? true : rowHandler.checkColumns();
            boolean checkProperties = (rowHandler == null) ? false : rowHandler.checkProperties();
            String key = rowType.getName() + "[checkColumns=" + checkColumns + "&checkProperties="
                    + checkProperties + "]";
            rowMapper = rowMappers.get(key);
            if (rowMapper == null) {
                rowMapper = new BeanPropertyRowMapper(rowType, checkColumns, checkProperties); // jade's BeanPropertyRowMapper here
                rowMappers.put(key, rowMapper);
            }
        }
        // DAOMaprowMapper?Map.Entry
        if (returnClassType == Map.class) {
            rowMapper = new MapEntryRowMapper(modifier, rowMapper);
        }
    }

    if (logger.isInfoEnabled()) {
        logger.info("using rowMapper " + rowMapper + " for " + modifier);
    }

    return rowMapper;
}

From source file:org.apache.camel.spring.CamelBeanPostProcessor.java

public void setCamelContext(CamelContext camelContext) {
    this.camelContext = camelContext;
    postProcessor = new CamelPostProcessorHelper(camelContext) {
        @Override// w ww .  ja va 2  s.  c o m
        protected RuntimeException createProxyInstantiationRuntimeException(Class<?> type, Endpoint endpoint,
                Exception e) {
            return new BeanInstantiationException(type,
                    "Could not instantiate proxy of type " + type.getName() + " on endpoint " + endpoint, e);
        }

        protected boolean isSingleton(Object bean, String beanName) {
            // no application context has been injected which means the bean
            // has not been enlisted in Spring application context
            if (applicationContext == null || beanName == null) {
                return super.isSingleton(bean, beanName);
            } else {
                return applicationContext.isSingleton(beanName);
            }
        }

        protected void startService(Service service, Object bean, String beanName) throws Exception {
            if (isSingleton(bean, beanName)) {
                getCamelContext().addService(service);
            } else {
                // only start service and do not add it to CamelContext
                ServiceHelper.startService(service);
                if (prototypeBeans.add(beanName)) {
                    // do not spam the log with WARN so do this only once per bean name
                    LOG.warn("The bean with id [" + beanName
                            + "] is prototype scoped and cannot stop the injected service when bean is destroyed: "
                            + service + ". You may want to stop the service manually from the bean.");
                }
            }
        }
    };
}

From source file:org.springframework.beans.BeanUtils.java

/**
 * Convenience method to instantiate a class using the given constructor.
 * <p>Note that this method tries to set the constructor accessible if given a
 * non-accessible (that is, non-public) constructor, and supports Kotlin classes
 * with optional parameters and default values.
 * @param ctor the constructor to instantiate
 * @param args the constructor arguments to apply (use {@code null} for an unspecified
 * parameter if needed for Kotlin classes with optional parameters and default values)
 * @return the new instance/*w w  w .  jav a  2s  .com*/
 * @throws BeanInstantiationException if the bean cannot be instantiated
 * @see Constructor#newInstance
 */
public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException {
    Assert.notNull(ctor, "Constructor must not be null");
    try {
        ReflectionUtils.makeAccessible(ctor);
        return (KotlinDetector.isKotlinType(ctor.getDeclaringClass())
                ? KotlinDelegate.instantiateClass(ctor, args)
                : ctor.newInstance(args));
    } catch (InstantiationException ex) {
        throw new BeanInstantiationException(ctor, "Is it an abstract class?", ex);
    } catch (IllegalAccessException ex) {
        throw new BeanInstantiationException(ctor, "Is the constructor accessible?", ex);
    } catch (IllegalArgumentException ex) {
        throw new BeanInstantiationException(ctor, "Illegal arguments for constructor", ex);
    } catch (InvocationTargetException ex) {
        throw new BeanInstantiationException(ctor, "Constructor threw exception", ex.getTargetException());
    }
}