From a3a5a03ee36d259f6d06b679ec62e93d58678c71 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 26 Jan 2016 18:00:05 +0100 Subject: [PATCH] PropertySource annotation allows for custom encoding Issue: SPR-13874 --- .../annotation/ConfigurationClassParser.java | 19 ++++++++++++++++--- .../context/annotation/PropertySource.java | 13 +++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index 8451afd9240..96f284d392f 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -66,6 +66,7 @@ import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; +import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.ResourcePropertySource; import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.MethodMetadata; @@ -354,6 +355,7 @@ class ConfigurationClassParser { */ private void processPropertySource(AnnotationAttributes propertySource) throws IOException { String name = propertySource.getString("name"); + String encoding = propertySource.getString("encoding"); String[] locations = propertySource.getStringArray("value"); boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound"); Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required"); @@ -361,9 +363,7 @@ class ConfigurationClassParser { try { String resolvedLocation = this.environment.resolveRequiredPlaceholders(location); Resource resource = this.resourceLoader.getResource(resolvedLocation); - ResourcePropertySource rps = (StringUtils.hasText(name) ? - new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource)); - addPropertySource(rps); + addPropertySource(createPropertySource(name, encoding, resource)); } catch (IllegalArgumentException ex) { // from resolveRequiredPlaceholders @@ -380,6 +380,19 @@ class ConfigurationClassParser { } } + private ResourcePropertySource createPropertySource(String name, String encoding, Resource resource) throws IOException { + if (StringUtils.hasText(name)) { + return (StringUtils.hasText(encoding) ? + new ResourcePropertySource(name, new EncodedResource(resource, encoding)) : + new ResourcePropertySource(name, resource)); + } + else { + return (StringUtils.hasText(encoding) ? + new ResourcePropertySource(new EncodedResource(resource, encoding)) : + new ResourcePropertySource(resource)); + } + } + private void addPropertySource(ResourcePropertySource propertySource) { String name = propertySource.getName(); MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources(); diff --git a/spring-context/src/main/java/org/springframework/context/annotation/PropertySource.java b/spring-context/src/main/java/org/springframework/context/annotation/PropertySource.java index 115766c6879..c060c4d1e82 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/PropertySource.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/PropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -148,14 +148,19 @@ import java.lang.annotation.Target; public @interface PropertySource { /** - * Indicate the name of this property source. If omitted, a name - * will be generated based on the description of the underlying - * resource. + * Indicate the name of this property source. If omitted, a name will + * be generated based on the description of the underlying resource. * @see org.springframework.core.env.PropertySource#getName() * @see org.springframework.core.io.Resource#getDescription() */ String name() default ""; + /** + * A specific character encoding for the given resources, e.g. "UTF-8". + * @since 4.3 + */ + String encoding() default ""; + /** * Indicate the resource location(s) of the properties file to be loaded. * For example, {@code "classpath:/com/myco/app.properties"} or