Skip to content
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

Placeholder resolution within @PropertySource resource locations can not resolve properties from prior sources [SPR-12110] #16726

Closed
spring-projects-issues opened this issue Aug 21, 2014 · 1 comment
Assignees
Labels
in: core type: enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Aug 21, 2014

Eric Sirianni opened SPR-12110 and commented

Consider the following example:

@PropertySources({
    @PropertySource("classpath:/bootstrap.properties"),
    @PropertySource("file:${config.dir}/app.properties")
})

Where bootstrap.properties is:

config.dir=/etc

And /etc/app.properties is:

foo=...
bar=...

ConfigurationClassParser is unable to resolve the config.dir placeholder in the second @PropertySource. The issue is that the resolved PropertySource s are not added to the Environment incrementally, but rather by the parent ConfigurationClassPostProcessor after all PropertySources are resolved:

From ConfigurationClassParser:

for (String location : locations) {
     try {
          String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
          Resource resource = this.resourceLoader.getResource(resolvedLocation);
          ResourcePropertySource ps = new ResourcePropertySource(resource);
          this.propertySources.add((StringUtils.hasText(name) ? name : ps.getName()), ps);
     }

From ConfigurationClassPostProcessor:

		// Parse each @Configuration class
		ConfigurationClassParser parser = new ConfigurationClassParser(
				this.metadataReaderFactory, this.problemReporter, this.environment,
				this.resourceLoader, this.componentScanBeanNameGenerator, registry);
		parser.parse(configCandidates);
		parser.validate();

		// Handle any @PropertySource annotations
		List<PropertySource<?>> parsedPropertySources = parser.getPropertySources();
		if (!parsedPropertySources.isEmpty()) {
			if (!(this.environment instanceof ConfigurableEnvironment)) {
				logger.warn("Ignoring @PropertySource annotations. " +
						"Reason: Environment must implement ConfigurableEnvironment");
			}
			else {
				MutablePropertySources envPropertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
				for (PropertySource<?> propertySource : parsedPropertySources) {
					envPropertySources.addLast(propertySource);
				}
			}
		}

This code could be restructured to support the use case mentioned above.


Affects: 4.0.6

Issue Links:

  • #16731 PropertySource named cannot be added relative to itself (@PropertySource processing fails)
  • #16727 PropertySources added via @PropertySource aren't available in the environment during component scanning

Referenced from: commits 7c60888

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 21, 2014

Juergen Hoeller commented

This simply wasn't support before, but through a reordering of the processing algorithm, this has been addressed for 4.1 GA now, along with #16726.

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants