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.env.PropertySource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
|
import org.springframework.core.io.support.EncodedResource;
|
||||||
import org.springframework.core.io.support.ResourcePropertySource;
|
import org.springframework.core.io.support.ResourcePropertySource;
|
||||||
import org.springframework.core.type.AnnotationMetadata;
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
import org.springframework.core.type.MethodMetadata;
|
import org.springframework.core.type.MethodMetadata;
|
||||||
|
|
@ -354,6 +355,7 @@ class ConfigurationClassParser {
|
||||||
*/
|
*/
|
||||||
private void processPropertySource(AnnotationAttributes propertySource) throws IOException {
|
private void processPropertySource(AnnotationAttributes propertySource) throws IOException {
|
||||||
String name = propertySource.getString("name");
|
String name = propertySource.getString("name");
|
||||||
|
String encoding = propertySource.getString("encoding");
|
||||||
String[] locations = propertySource.getStringArray("value");
|
String[] locations = propertySource.getStringArray("value");
|
||||||
boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");
|
boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");
|
||||||
Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");
|
Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");
|
||||||
|
|
@ -361,9 +363,7 @@ class ConfigurationClassParser {
|
||||||
try {
|
try {
|
||||||
String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
|
String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
|
||||||
Resource resource = this.resourceLoader.getResource(resolvedLocation);
|
Resource resource = this.resourceLoader.getResource(resolvedLocation);
|
||||||
ResourcePropertySource rps = (StringUtils.hasText(name) ?
|
addPropertySource(createPropertySource(name, encoding, resource));
|
||||||
new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource));
|
|
||||||
addPropertySource(rps);
|
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException ex) {
|
catch (IllegalArgumentException ex) {
|
||||||
// from resolveRequiredPlaceholders
|
// 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) {
|
private void addPropertySource(ResourcePropertySource propertySource) {
|
||||||
String name = propertySource.getName();
|
String name = propertySource.getName();
|
||||||
MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
|
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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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 {
|
public @interface PropertySource {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicate the name of this property source. If omitted, a name
|
* Indicate the name of this property source. If omitted, a name will
|
||||||
* will be generated based on the description of the underlying
|
* be generated based on the description of the underlying resource.
|
||||||
* resource.
|
|
||||||
* @see org.springframework.core.env.PropertySource#getName()
|
* @see org.springframework.core.env.PropertySource#getName()
|
||||||
* @see org.springframework.core.io.Resource#getDescription()
|
* @see org.springframework.core.io.Resource#getDescription()
|
||||||
*/
|
*/
|
||||||
String name() default "";
|
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.
|
* Indicate the resource location(s) of the properties file to be loaded.
|
||||||
* For example, {@code "classpath:/com/myco/app.properties"} or
|
* For example, {@code "classpath:/com/myco/app.properties"} or
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue