Search in sources :

Example 31 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project OpenClinica by OpenClinica.

the class QueryStore method init.

public void init() {
    String dbFolder = resolveDbFolder();
    PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(resourceLoader);
    try {
        Resource[] resources = resourceResolver.getResources("classpath:queries/" + dbFolder + "/**/*.properties");
        for (Resource r : resources) {
            Properties p = new Properties();
            p.load(r.getInputStream());
            fileByName.put(StringUtils.substringBeforeLast(r.getFilename(), "."), p);
        }
    } catch (IOException e) {
        throw new BeanInitializationException("Unable to read files from directory 'classpath:queries/" + dbFolder + "'", e);
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) Properties(java.util.Properties)

Example 32 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project motan by weibocom.

the class AnnotationBean method postProcessBeforeInitialization.

/**
 * init reference field
 *
 * @param bean
 * @param beanName
 * @return
 * @throws BeansException
 */
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    if (!isMatchPackage(bean)) {
        return bean;
    }
    Class<?> clazz = bean.getClass();
    if (isProxyBean(bean)) {
        clazz = AopUtils.getTargetClass(bean);
    }
    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        String name = method.getName();
        if (name.length() > 3 && name.startsWith("set") && method.getParameterTypes().length == 1 && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) {
            try {
                MotanReferer reference = method.getAnnotation(MotanReferer.class);
                if (reference != null) {
                    Object value = refer(reference, method.getParameterTypes()[0]);
                    if (value != null) {
                        method.invoke(bean, new Object[] { value });
                    }
                }
            } catch (Throwable t) {
                throw new BeanInitializationException("Failed to init remote service reference at method " + name + " in class " + bean.getClass().getName(), t);
            }
        }
    }
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        try {
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            MotanReferer reference = field.getAnnotation(MotanReferer.class);
            if (reference != null) {
                Object value = refer(reference, field.getType());
                if (value != null) {
                    field.set(bean, value);
                }
            }
        } catch (Throwable t) {
            throw new BeanInitializationException("Failed to init remote service reference at filed " + field.getName() + " in class " + bean.getClass().getName(), t);
        }
    }
    return bean;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Field(java.lang.reflect.Field) MotanReferer(com.weibo.api.motan.config.springsupport.annotation.MotanReferer) Method(java.lang.reflect.Method)

Example 33 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project midpoint by Evolveum.

the class TestSqlRepositoryBeanPostProcessor method postProcessAfterInitialization.

@Override
public Object postProcessAfterInitialization(@NotNull Object bean, @NotNull String beanName) throws BeansException {
    if (!(bean instanceof SessionFactory)) {
        return bean;
    }
    LOGGER.info("Postprocessing session factory - removing everything from database if necessary.");
    // we'll attempt to drop database objects if configuration contains dropIfExists=true and embedded=false
    if (!repoConfig.isDropIfExists() || repoConfig.isEmbedded()) {
        LOGGER.info("We're not deleting objects from DB, drop if exists=false or embedded=true.");
        return bean;
    }
    LOGGER.info("Deleting objects from database.");
    SessionFactory sessionFactory = (SessionFactory) bean;
    Session session = sessionFactory.openSession();
    try {
        session.beginTransaction();
        Query<?> query;
        if (useProcedure(repoConfig)) {
            LOGGER.info("Using truncate procedure.");
            query = session.createNativeQuery("{ call " + TRUNCATE_PROCEDURE + "() }");
            query.executeUpdate();
        } else {
            LOGGER.info("Using truncate function.");
            query = session.createNativeQuery("select " + TRUNCATE_FUNCTION + "();");
            query.uniqueResult();
        }
        session.getTransaction().commit();
    } catch (Exception ex) {
        LOGGER.error("Couldn't cleanup database, reason: " + ex.getMessage(), ex);
        if (session != null && session.isOpen()) {
            Transaction transaction = session.getTransaction();
            if (transaction != null && transaction.isActive()) {
                transaction.rollback();
            }
        }
        throw new BeanInitializationException("Couldn't cleanup database, reason: " + ex.getMessage(), ex);
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }
    return bean;
}
Also used : SessionFactory(org.hibernate.SessionFactory) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Transaction(org.hibernate.Transaction) BeansException(org.springframework.beans.BeansException) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Session(org.hibernate.Session)

Example 34 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project spring-framework by spring-projects.

the class EventListenerMethodProcessor method afterSingletonsInstantiated.

@Override
public void afterSingletonsInstantiated() {
    ConfigurableListableBeanFactory beanFactory = this.beanFactory;
    Assert.state(this.beanFactory != null, "No ConfigurableListableBeanFactory set");
    String[] beanNames = beanFactory.getBeanNamesForType(Object.class);
    for (String beanName : beanNames) {
        if (!ScopedProxyUtils.isScopedTarget(beanName)) {
            Class<?> type = null;
            try {
                type = AutoProxyUtils.determineTargetClass(beanFactory, beanName);
            } catch (Throwable ex) {
                // An unresolvable bean type, probably from a lazy bean - let's ignore it.
                if (logger.isDebugEnabled()) {
                    logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex);
                }
            }
            if (type != null) {
                if (ScopedObject.class.isAssignableFrom(type)) {
                    try {
                        Class<?> targetClass = AutoProxyUtils.determineTargetClass(beanFactory, ScopedProxyUtils.getTargetBeanName(beanName));
                        if (targetClass != null) {
                            type = targetClass;
                        }
                    } catch (Throwable ex) {
                        // An invalid scoped proxy arrangement - let's ignore it.
                        if (logger.isDebugEnabled()) {
                            logger.debug("Could not resolve target bean for scoped proxy '" + beanName + "'", ex);
                        }
                    }
                }
                try {
                    processBean(beanName, type);
                } catch (Throwable ex) {
                    throw new BeanInitializationException("Failed to process @EventListener " + "annotation on bean with name '" + beanName + "'", ex);
                }
            }
        }
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 35 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project spring-framework by spring-projects.

the class PropertyResourceConfigurer method postProcessBeanFactory.

/**
 * {@linkplain #mergeProperties Merge}, {@linkplain #convertProperties convert} and
 * {@linkplain #processProperties process} properties against the given bean factory.
 * @throws BeanInitializationException if any properties cannot be loaded
 */
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    try {
        Properties mergedProps = mergeProperties();
        // Convert the merged properties, if necessary.
        convertProperties(mergedProps);
        // Let the subclass process the properties.
        processProperties(beanFactory, mergedProps);
    } catch (IOException ex) {
        throw new BeanInitializationException("Could not load properties", ex);
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) IOException(java.io.IOException) Properties(java.util.Properties)

Aggregations

BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)41 IOException (java.io.IOException)9 BeansException (org.springframework.beans.BeansException)7 HashMap (java.util.HashMap)4 Bean (org.springframework.context.annotation.Bean)4 File (java.io.File)3 Method (java.lang.reflect.Method)3 Properties (java.util.Properties)3 Field (java.lang.reflect.Field)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Matcher (java.util.regex.Matcher)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 PropertyValue (org.springframework.beans.PropertyValue)2 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)2 BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)2