Search in sources :

Example 16 with BeansException

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

the class WebApplicationContextFacesELResolver method getType.

@Override
public Class<?> getType(ELContext elContext, Object base, Object property) throws ELException {
    if (base != null) {
        if (base instanceof WebApplicationContext) {
            WebApplicationContext wac = (WebApplicationContext) base;
            String beanName = property.toString();
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting to resolve property '" + beanName + "' in root WebApplicationContext");
            }
            if (wac.containsBean(beanName)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Successfully resolved property '" + beanName + "' in root WebApplicationContext");
                }
                elContext.setPropertyResolved(true);
                try {
                    return wac.getType(beanName);
                } catch (BeansException ex) {
                    throw new ELException(ex);
                }
            } else {
                // Mimic standard JSF/JSP behavior when base is a Map by returning null.
                return null;
            }
        }
    } else {
        if (WEB_APPLICATION_CONTEXT_VARIABLE_NAME.equals(property)) {
            elContext.setPropertyResolved(true);
            return WebApplicationContext.class;
        }
    }
    return null;
}
Also used : ELException(javax.el.ELException) WebApplicationContext(org.springframework.web.context.WebApplicationContext) BeansException(org.springframework.beans.BeansException)

Example 17 with BeansException

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

the class WebApplicationContextFacesELResolver method getValue.

@Override
public Object getValue(ELContext elContext, Object base, Object property) throws ELException {
    if (base != null) {
        if (base instanceof WebApplicationContext) {
            WebApplicationContext wac = (WebApplicationContext) base;
            String beanName = property.toString();
            if (logger.isTraceEnabled()) {
                logger.trace("Attempting to resolve property '" + beanName + "' in root WebApplicationContext");
            }
            if (wac.containsBean(beanName)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Successfully resolved property '" + beanName + "' in root WebApplicationContext");
                }
                elContext.setPropertyResolved(true);
                try {
                    return wac.getBean(beanName);
                } catch (BeansException ex) {
                    throw new ELException(ex);
                }
            } else {
                // Mimic standard JSF/JSP behavior when base is a Map by returning null.
                return null;
            }
        }
    } else {
        if (WEB_APPLICATION_CONTEXT_VARIABLE_NAME.equals(property)) {
            elContext.setPropertyResolved(true);
            return getWebApplicationContext(elContext);
        }
    }
    return null;
}
Also used : ELException(javax.el.ELException) WebApplicationContext(org.springframework.web.context.WebApplicationContext) BeansException(org.springframework.beans.BeansException)

Example 18 with BeansException

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

the class HttpServletBean method init.

/**
	 * Map config parameters onto bean properties of this servlet, and
	 * invoke subclass initialization.
	 * @throws ServletException if bean properties are invalid (or required
	 * properties are missing), or if subclass initialization fails.
	 */
@Override
public final void init() throws ServletException {
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing servlet '" + getServletName() + "'");
    }
    // Set bean properties from init parameters.
    try {
        PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
        ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
        bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
        initBeanWrapper(bw);
        bw.setPropertyValues(pvs, true);
    } catch (BeansException ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
        }
        throw ex;
    }
    // Let subclasses do whatever initialization they like.
    initServletBean();
    if (logger.isDebugEnabled()) {
        logger.debug("Servlet '" + getServletName() + "' configured successfully");
    }
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeanWrapper(org.springframework.beans.BeanWrapper) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValues(org.springframework.beans.PropertyValues) ResourceEditor(org.springframework.core.io.ResourceEditor) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeansException(org.springframework.beans.BeansException)

Example 19 with BeansException

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

the class ConvertingEncoderDecoderSupport method getConversionService.

/**
	 * Strategy method used to obtain the {@link ConversionService}. By default this
	 * method expects a bean named {@code 'webSocketConversionService'} in the
	 * {@link #getApplicationContext() active ApplicationContext}.
	 * @return the {@link ConversionService} (never null)
	 */
protected ConversionService getConversionService() {
    ApplicationContext applicationContext = getApplicationContext();
    Assert.state(applicationContext != null, "Unable to locate the Spring ApplicationContext");
    try {
        return applicationContext.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class);
    } catch (BeansException ex) {
        throw new IllegalStateException("Unable to find ConversionService: please configure a '" + CONVERSION_SERVICE_BEAN_NAME + "' or override the getConversionService() method", ex);
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) BeansException(org.springframework.beans.BeansException)

Example 20 with BeansException

use of org.springframework.beans.BeansException in project disconf by knightliao.

the class ReloadingPropertyPlaceholderConfigurer method propertiesReloaded.

/**
     * 当配置更新时,被调用
     *
     * @param event
     */
public void propertiesReloaded(PropertiesReloadedEvent event) {
    Properties oldProperties = lastMergedProperties;
    try {
        //
        Properties newProperties = mergeProperties();
        //
        // 获取哪些 dynamic property 被影响
        //
        Set<String> placeholders = placeholderToDynamics.keySet();
        Set<DynamicProperty> allDynamics = new HashSet<DynamicProperty>();
        for (String placeholder : placeholders) {
            String newValue = newProperties.getProperty(placeholder);
            String oldValue = oldProperties.getProperty(placeholder);
            if (newValue != null && !newValue.equals(oldValue) || newValue == null && oldValue != null) {
                if (logger.isInfoEnabled()) {
                    logger.info("Property changed detected: " + placeholder + (newValue != null ? "=" + newValue : " removed"));
                }
                List<DynamicProperty> affectedDynamics = placeholderToDynamics.get(placeholder);
                allDynamics.addAll(affectedDynamics);
            }
        }
        //
        // 获取受影响的beans
        //
        Map<String, List<DynamicProperty>> dynamicsByBeanName = new HashMap<String, List<DynamicProperty>>();
        Map<String, Object> beanByBeanName = new HashMap<String, Object>();
        for (DynamicProperty dynamic : allDynamics) {
            String beanName = dynamic.getBeanName();
            List<DynamicProperty> l = dynamicsByBeanName.get(beanName);
            if (l == null) {
                dynamicsByBeanName.put(beanName, (l = new ArrayList<DynamicProperty>()));
                Object bean = null;
                try {
                    bean = applicationContext.getBean(beanName);
                    beanByBeanName.put(beanName, bean);
                } catch (BeansException e) {
                    // keep dynamicsByBeanName list, warn only once.
                    logger.error("Error obtaining bean " + beanName, e);
                }
                //
                try {
                    if (bean instanceof IReconfigurationAware) {
                        // hello!
                        ((IReconfigurationAware) bean).beforeReconfiguration();
                    }
                } catch (Exception e) {
                    logger.error("Error calling beforeReconfiguration on " + beanName, e);
                }
            }
            l.add(dynamic);
        }
        //
        // 处理受影响的bean
        //
        Collection<String> beanNames = dynamicsByBeanName.keySet();
        for (String beanName : beanNames) {
            Object bean = beanByBeanName.get(beanName);
            if (// problems obtaining bean, earlier
            bean == null) {
                continue;
            }
            BeanWrapper beanWrapper = new BeanWrapperImpl(bean);
            // for all affected ...
            List<DynamicProperty> dynamics = dynamicsByBeanName.get(beanName);
            for (DynamicProperty dynamic : dynamics) {
                String propertyName = dynamic.getPropertyName();
                String unparsedValue = dynamic.getUnparsedValue();
                // obtain an updated value, including dependencies
                String newValue;
                removeDynamic(dynamic);
                currentBeanName = beanName;
                currentPropertyName = propertyName;
                try {
                    newValue = parseStringValue(unparsedValue, newProperties, new HashSet());
                } finally {
                    currentBeanName = null;
                    currentPropertyName = null;
                }
                if (logger.isInfoEnabled()) {
                    logger.info("Updating property " + beanName + "." + propertyName + " to " + newValue);
                }
                // assign it to the bean
                try {
                    beanWrapper.setPropertyValue(propertyName, newValue);
                } catch (BeansException e) {
                    logger.error("Error setting property " + beanName + "." + propertyName + " to " + newValue, e);
                }
            }
        }
        //
        for (String beanName : beanNames) {
            Object bean = beanByBeanName.get(beanName);
            try {
                if (bean instanceof IReconfigurationAware) {
                    ((IReconfigurationAware) bean).afterReconfiguration();
                }
            } catch (Exception e) {
                logger.error("Error calling afterReconfiguration on " + beanName, e);
            }
        }
    } catch (IOException e) {
        logger.error("Error trying to reload net.unicon.iamlabs.spring.properties.example.net.unicon.iamlabs" + ".spring" + ".properties: " + e.getMessage(), e);
    }
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) HashMap(java.util.HashMap) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanWrapper(org.springframework.beans.BeanWrapper) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) BeansException(org.springframework.beans.BeansException)

Aggregations

BeansException (org.springframework.beans.BeansException)64 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)14 BeanWrapper (org.springframework.beans.BeanWrapper)11 Test (org.junit.Test)10 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)9 BeanCreationException (org.springframework.beans.factory.BeanCreationException)8 MalformedURLException (java.net.MalformedURLException)7 ArrayList (java.util.ArrayList)7 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)7 ApplicationContext (org.springframework.context.ApplicationContext)7 Map (java.util.Map)6 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)5 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)5 IOException (java.io.IOException)4 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 FileSystemXmlApplicationContext (org.springframework.context.support.FileSystemXmlApplicationContext)4 UrlResource (org.springframework.core.io.UrlResource)4 File (java.io.File)3 HashMap (java.util.HashMap)3