Search in sources :

Example 21 with BeanInitializationException

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

the class PropertySourcesPlaceholderConfigurer method postProcessBeanFactory.

/**
	 * {@inheritDoc}
	 * <p>Processing occurs by replacing ${...} placeholders in bean definitions by resolving each
	 * against this configurer's set of {@link PropertySources}, which includes:
	 * <ul>
	 * <li>all {@linkplain org.springframework.core.env.ConfigurableEnvironment#getPropertySources
	 * environment property sources}, if an {@code Environment} {@linkplain #setEnvironment is present}
	 * <li>{@linkplain #mergeProperties merged local properties}, if {@linkplain #setLocation any}
	 * {@linkplain #setLocations have} {@linkplain #setProperties been}
	 * {@linkplain #setPropertiesArray specified}
	 * <li>any property sources set by calling {@link #setPropertySources}
	 * </ul>
	 * <p>If {@link #setPropertySources} is called, <strong>environment and local properties will be
	 * ignored</strong>. This method is designed to give the user fine-grained control over property
	 * sources, and once set, the configurer makes no assumptions about adding additional sources.
	 */
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (this.propertySources == null) {
        this.propertySources = new MutablePropertySources();
        if (this.environment != null) {
            this.propertySources.addLast(new PropertySource<Environment>(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, this.environment) {

                @Override
                public String getProperty(String key) {
                    return this.source.getProperty(key);
                }
            });
        }
        try {
            PropertySource<?> localPropertySource = new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergeProperties());
            if (this.localOverride) {
                this.propertySources.addFirst(localPropertySource);
            } else {
                this.propertySources.addLast(localPropertySource);
            }
        } catch (IOException ex) {
            throw new BeanInitializationException("Could not load properties", ex);
        }
    }
    processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources));
    this.appliedPropertySources = this.propertySources;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) PropertySourcesPropertyResolver(org.springframework.core.env.PropertySourcesPropertyResolver) PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) Environment(org.springframework.core.env.Environment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) IOException(java.io.IOException)

Example 22 with BeanInitializationException

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

the class QueryStore method resolveDbFolder.

protected String resolveDbFolder() {
    try {
        Connection conn = dataSource.getConnection();
        CoreResources.setSchema(conn);
        String url = conn.getMetaData().getURL();
        if (url.startsWith("jdbc:postgresql")) {
            return "postgres";
        }
        if (url.startsWith("jdbc:oracle")) {
            return "oracle";
        }
        throw new BeanInitializationException("Unrecognized JDBC url " + url);
    } catch (SQLException e) {
        throw new BeanInitializationException("Unable to read datasource information", e);
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 23 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project FredaBlog by yangjinlong86.

the class EncryptPropertyPlaceholderConfigurer method processProperties.

@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {
    try {
        String username = props.getProperty("username");
        if (StringUtils.isNotBlank(username)) {
            props.setProperty("username", EncryptUtil.decode(username));
        }
        String password = props.getProperty("password");
        if (StringUtils.isNotBlank(password)) {
            props.setProperty("password", EncryptUtil.decode(password));
        }
        super.processProperties(beanFactory, props);
    } catch (Exception e) {
        logger.error("encrypt error!", e);
        throw new BeanInitializationException(e.getMessage());
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) BeansException(org.springframework.beans.BeansException) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException)

Example 24 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project com.revolsys.open by revolsys.

the class BeanConfigurrer method processOverride.

/**
 * Process the given key as 'beanName.property' entry.
 */
protected void processOverride(final ConfigurableListableBeanFactory factory, final String key, Object value) {
    try {
        if (value instanceof BeanReference) {
            final BeanReference reference = (BeanReference) value;
            value = reference.getBean();
        }
        final Matcher matcher = BeanConfigurrer.KEY_PATTERN.matcher(key);
        if (matcher.matches()) {
            final String beanName = matcher.group(1);
            final String mapKey = matcher.group(2);
            final String propertyName = matcher.group(3);
            if (mapKey == null) {
                if (propertyName == null) {
                    if (factory.containsBean(beanName)) {
                        BeanDefinition beanDefinition = factory.getBeanDefinition(beanName);
                        try {
                            final ClassLoader classLoader = this.applicationContext.getClassLoader();
                            final String beanClassName = beanDefinition.getBeanClassName();
                            final Class<?> beanClass = Class.forName(beanClassName, true, classLoader);
                            if (Parameter.class.isAssignableFrom(beanClass)) {
                                while (beanDefinition.getOriginatingBeanDefinition() != null) {
                                    beanDefinition = beanDefinition.getOriginatingBeanDefinition();
                                }
                                final MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
                                PropertyValue propertyValue = new PropertyValue("value", value);
                                final PropertyValue typeValue = propertyValues.getPropertyValue("type");
                                if (typeValue != null) {
                                    try {
                                        final Class<?> typeClass;
                                        final Object typeValueObject = typeValue.getValue();
                                        if (typeValueObject instanceof Class<?>) {
                                            typeClass = (Class<?>) typeValueObject;
                                        } else {
                                            final String typeClassName = typeValueObject.toString();
                                            typeClass = Class.forName(typeClassName, true, classLoader);
                                        }
                                        final Object convertedValue = new SimpleTypeConverter().convertIfNecessary(value, typeClass);
                                        propertyValue = new PropertyValue("value", convertedValue);
                                    } catch (final Throwable e) {
                                        Logs.error(this, "Unable to set " + beanName + ".value=" + value, e);
                                    }
                                }
                                propertyValues.addPropertyValue(propertyValue);
                            }
                        } catch (final ClassNotFoundException e) {
                            Logs.error(this, "Unable to set " + beanName + ".value=" + value, e);
                        }
                    } else if (value != null) {
                        newParameterBeanDefinition(factory, beanName, value);
                    }
                } else {
                    setAttributeValue(factory, beanName, propertyName, value);
                    Logs.debug(this, "Property '" + key + "' set to value [" + value + "]");
                }
            } else if (propertyName == null) {
                setMapValue(factory, key, beanName, mapKey, value);
            } else {
                Logs.error(this, "Invalid syntax unable to set " + key + "=" + value);
            }
        }
    } catch (final BeansException ex) {
        final String msg = "Could not process key '" + key + "' in PropertyOverrideConfigurer";
        if (!this.ignoreInvalidKeys) {
            throw new BeanInitializationException(msg, ex);
        }
        Logs.debug(this, msg, ex);
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Matcher(java.util.regex.Matcher) PropertyValue(org.springframework.beans.PropertyValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) SimpleTypeConverter(org.springframework.beans.SimpleTypeConverter) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) BeanReference(com.revolsys.spring.BeanReference) BeansException(org.springframework.beans.BeansException)

Example 25 with BeanInitializationException

use of org.springframework.beans.factory.BeanInitializationException in project com.revolsys.open by revolsys.

the class DatabasePropertyOverrideConfigurer method processKey.

/**
 * Process the given key as 'beanName.property' entry.
 *
 * @param factory The bean factory.
 * @param key The key used to set the property.
 * @param value The value to set. @ If there was an problem setting the value.
 */
protected void processKey(final ConfigurableListableBeanFactory factory, final String key, final String value) {
    final Matcher mapProperetyValueMatcher = MAP_PROPERTY_VALUE_PATTERN.matcher(key);
    if (mapProperetyValueMatcher.matches()) {
        final String beanName = mapProperetyValueMatcher.group(1);
        final String beanProperty = mapProperetyValueMatcher.group(2);
        final String mapKey = mapProperetyValueMatcher.group(3);
        applyMapPropertyValue(factory, beanName, beanProperty, mapKey, value);
    } else {
        final Matcher simpleMatcher = SIMPLE_PROPERTY_PATTERN.matcher(key);
        if (simpleMatcher.matches()) {
            final String beanName = simpleMatcher.group(1);
            final String beanProperty = simpleMatcher.group(2);
            applyPropertyValue(factory, beanName, beanProperty, value);
        } else {
            throw new BeanInitializationException("Invalid key [" + key + "]");
        }
    }
    if (getLog().isDebugEnabled()) {
        getLog().debug("WebProperty '" + key + "' set to [" + value + "]");
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Matcher(java.util.regex.Matcher)

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