PropertySource annotation allows for custom encoding
Issue: SPR-13874
This commit is contained in:
parent
2607a22537
commit
a3a5a03ee3
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue