Search in sources :

Example 6 with EncodedResource

use of org.springframework.core.io.support.EncodedResource in project spring-framework by spring-projects.

the class StaticApplicationContextMulticasterTests method createContext.

@Override
protected ConfigurableApplicationContext createContext() throws Exception {
    StaticApplicationContext parent = new StaticApplicationContext();
    Map<String, String> m = new HashMap<>();
    m.put("name", "Roderick");
    parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m));
    m.put("name", "Albert");
    parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m));
    parent.registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, TestApplicationEventMulticaster.class, null);
    parent.refresh();
    parent.addApplicationListener(parentListener);
    parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1");
    this.sac = new StaticApplicationContext(parent);
    sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues());
    sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues());
    sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues());
    PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
    Resource resource = new ClassPathResource("testBeans.properties", getClass());
    reader.loadBeanDefinitions(new EncodedResource(resource, "ISO-8859-1"));
    sac.refresh();
    sac.addApplicationListener(listener);
    sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2");
    return sac;
}
Also used : HashMap(java.util.HashMap) PropertiesBeanDefinitionReader(org.springframework.beans.factory.support.PropertiesBeanDefinitionReader) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) ClassPathResource(org.springframework.core.io.ClassPathResource) EncodedResource(org.springframework.core.io.support.EncodedResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) EncodedResource(org.springframework.core.io.support.EncodedResource)

Example 7 with EncodedResource

use of org.springframework.core.io.support.EncodedResource in project spring-framework by spring-projects.

the class ConfigurationClassParser method processPropertySource.

/**
	 * Process the given <code>@PropertySource</code> annotation metadata.
	 * @param propertySource metadata for the <code>@PropertySource</code> annotation found
	 * @throws IOException if loading a property source failed
	 */
private void processPropertySource(AnnotationAttributes propertySource) throws IOException {
    String name = propertySource.getString("name");
    if (!StringUtils.hasLength(name)) {
        name = null;
    }
    String encoding = propertySource.getString("encoding");
    if (!StringUtils.hasLength(encoding)) {
        encoding = null;
    }
    String[] locations = propertySource.getStringArray("value");
    Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");
    boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");
    Class<? extends PropertySourceFactory> factoryClass = propertySource.getClass("factory");
    PropertySourceFactory factory = (factoryClass == PropertySourceFactory.class ? DEFAULT_PROPERTY_SOURCE_FACTORY : BeanUtils.instantiateClass(factoryClass));
    for (String location : locations) {
        try {
            String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
            Resource resource = this.resourceLoader.getResource(resolvedLocation);
            addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
        } catch (IllegalArgumentException | FileNotFoundException ex) {
            // Placeholders not resolvable or resource not found when trying to open it
            if (ignoreResourceNotFound) {
                if (logger.isInfoEnabled()) {
                    logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
                }
            } else {
                throw ex;
            }
        }
    }
}
Also used : PropertySourceFactory(org.springframework.core.io.support.PropertySourceFactory) DefaultPropertySourceFactory(org.springframework.core.io.support.DefaultPropertySourceFactory) Resource(org.springframework.core.io.Resource) EncodedResource(org.springframework.core.io.support.EncodedResource) FileNotFoundException(java.io.FileNotFoundException) EncodedResource(org.springframework.core.io.support.EncodedResource)

Aggregations

EncodedResource (org.springframework.core.io.support.EncodedResource)7 Resource (org.springframework.core.io.Resource)5 IOException (java.io.IOException)2 Test (org.junit.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 PropertiesBeanDefinitionReader (org.springframework.beans.factory.support.PropertiesBeanDefinitionReader)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 DefaultPropertySourceFactory (org.springframework.core.io.support.DefaultPropertySourceFactory)1 PropertySourceFactory (org.springframework.core.io.support.PropertySourceFactory)1 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)1 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)1 TestBean (org.springframework.tests.sample.beans.TestBean)1