Skip to content

Commit a3a5a03

Browse files
committed
PropertySource annotation allows for custom encoding
Issue: SPR-13874
1 parent 2607a22 commit a3a5a03

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import org.springframework.core.env.PropertySource;
6767
import org.springframework.core.io.Resource;
6868
import org.springframework.core.io.ResourceLoader;
69+
import org.springframework.core.io.support.EncodedResource;
6970
import org.springframework.core.io.support.ResourcePropertySource;
7071
import org.springframework.core.type.AnnotationMetadata;
7172
import org.springframework.core.type.MethodMetadata;
@@ -354,16 +355,15 @@ private void processMemberClasses(ConfigurationClass configClass, SourceClass so
354355
*/
355356
private void processPropertySource(AnnotationAttributes propertySource) throws IOException {
356357
String name = propertySource.getString("name");
358+
String encoding = propertySource.getString("encoding");
357359
String[] locations = propertySource.getStringArray("value");
358360
boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");
359361
Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");
360362
for (String location : locations) {
361363
try {
362364
String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
363365
Resource resource = this.resourceLoader.getResource(resolvedLocation);
364-
ResourcePropertySource rps = (StringUtils.hasText(name) ?
365-
new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource));
366-
addPropertySource(rps);
366+
addPropertySource(createPropertySource(name, encoding, resource));
367367
}
368368
catch (IllegalArgumentException ex) {
369369
// from resolveRequiredPlaceholders
@@ -380,6 +380,19 @@ private void processPropertySource(AnnotationAttributes propertySource) throws I
380380
}
381381
}
382382

383+
private ResourcePropertySource createPropertySource(String name, String encoding, Resource resource) throws IOException {
384+
if (StringUtils.hasText(name)) {
385+
return (StringUtils.hasText(encoding) ?
386+
new ResourcePropertySource(name, new EncodedResource(resource, encoding)) :
387+
new ResourcePropertySource(name, resource));
388+
}
389+
else {
390+
return (StringUtils.hasText(encoding) ?
391+
new ResourcePropertySource(new EncodedResource(resource, encoding)) :
392+
new ResourcePropertySource(resource));
393+
}
394+
}
395+
383396
private void addPropertySource(ResourcePropertySource propertySource) {
384397
String name = propertySource.getName();
385398
MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();

spring-context/src/main/java/org/springframework/context/annotation/PropertySource.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -148,14 +148,19 @@
148148
public @interface PropertySource {
149149

150150
/**
151-
* Indicate the name of this property source. If omitted, a name
152-
* will be generated based on the description of the underlying
153-
* resource.
151+
* Indicate the name of this property source. If omitted, a name will
152+
* be generated based on the description of the underlying resource.
154153
* @see org.springframework.core.env.PropertySource#getName()
155154
* @see org.springframework.core.io.Resource#getDescription()
156155
*/
157156
String name() default "";
158157

158+
/**
159+
* A specific character encoding for the given resources, e.g. "UTF-8".
160+
* @since 4.3
161+
*/
162+
String encoding() default "";
163+
159164
/**
160165
* Indicate the resource location(s) of the properties file to be loaded.
161166
* For example, {@code "classpath:/com/myco/app.properties"} or

0 commit comments

Comments
 (0)