Skip to content

Support wildcards in @PropertySource #24591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package org.springframework.context.annotation;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayDeque;
import java.util.ArrayList;
Expand All @@ -35,6 +37,8 @@
import java.util.Set;
import java.util.StringJoiner;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -455,6 +459,8 @@ private void processPropertySource(AnnotationAttributes propertySource) throws I
PropertySourceFactory factory = (factoryClass == PropertySourceFactory.class ?
DEFAULT_PROPERTY_SOURCE_FACTORY : BeanUtils.instantiateClass(factoryClass));

locations = getWildcardLocations(locations);

for (String location : locations) {
try {
String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
Expand All @@ -475,6 +481,32 @@ private void processPropertySource(AnnotationAttributes propertySource) throws I
}
}

/**
* Parse locations and expand those with wildcard
*/
private String[] getWildcardLocations(String[] locations) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were to introduce support for wildcards, we would need to use PathMatchingResourcePatternResolver instead of implementing a custom method like you have done here.

List<String> filesNames = new ArrayList<>();
Pattern allFiles = Pattern.compile("(.*[/:])\\*(\\.[a-zA-Z0-9]*)$");
for(String location : locations){
Matcher matcher = allFiles.matcher(location);
if(matcher.matches()){
String folderName = matcher.group(1);
String extension = matcher.group(2);
File folder = this.resourceLoader.getResource(folderName).getFile();
File[] files = folder.listFiles();
for (File file : files){
if(file.getName().endsWith(extension)){
filesNames.add("file:"+file.getPath());
}
}
} else {
filesNames.add(location);
}
}

return filesNames.toArray(new String[filesNames.size()]);
}

private void addPropertySource(PropertySource<?> propertySource) {
String name = propertySource.getName();
MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@
* configuration class and then used when populating the {@code TestBean} object. Given
* the configuration above, a call to {@code testBean.getName()} will return "myTestBean".
*
* <p>Given a folder with several files with the same extension containing the key/value pair
* {@code testbean.name=myTestBean}, the following {@code @Configuration} class
* uses {@code @PropertySource} to contribute all {@code *.properties} to the
* {@code Environment}'s set of {@code PropertySources}.</p>
*
*
* <pre class="code">
* &#064;Configuration
* &#064;PropertySource("classpath:/com/myco/*.properties")
* public class AppConfig {
*
* &#064;Autowired
* Environment env;
*
* &#064;Bean
* public TestBean testBean() {
* TestBean testBean = new TestBean();
* testBean.setName(env.getProperty("testbean.name"));
* return testBean;
* }
* }</pre>
*
* <h3>Resolving <code>${...}</code> placeholders in {@code <bean>} and {@code @Value} annotations</h3>
*
* <p>In order to resolve ${...} placeholders in {@code <bean>} definitions or {@code @Value}
Expand Down Expand Up @@ -156,12 +178,12 @@
* @author Juergen Hoeller
* @author Phillip Webb
* @author Sam Brannen
* @since 3.1
* @see PropertySources
* @see Configuration
* @see org.springframework.core.env.PropertySource
* @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
* @see org.springframework.core.env.MutablePropertySources
* @since 3.1
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
Expand All @@ -172,6 +194,7 @@
/**
* 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()
*/
Expand All @@ -197,22 +220,25 @@
* ignored.
* <p>{@code true} is appropriate if the properties file is completely optional.
* Default is {@code false}.
*
* @since 4.0
*/
boolean ignoreResourceNotFound() default false;

/**
* A specific character encoding for the given resources, e.g. "UTF-8".
*
* @since 4.3
*/
String encoding() default "";

/**
* Specify a custom {@link PropertySourceFactory}, if any.
* <p>By default, a default factory for standard resource files will be used.
* @since 4.3
*
* @see org.springframework.core.io.support.DefaultPropertySourceFactory
* @see org.springframework.core.io.support.ResourcePropertySource
* @since 4.3
*/
Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;

Expand Down