Skip to content

Placeholder resolution for annotation attributes is not active by default [SPR-14140] #18712

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
spring-projects-issues opened this issue Apr 9, 2016 · 9 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Apr 9, 2016

Bjorn Harvold opened SPR-14140 and commented

I created ticket #18525 pre v4.3.RC1.

I updated to 4.3.RC1 today and found that the placeholder still doesn't convert.

The below code fails:

public void logoutUser(@RequestHeader(name = "${auth.token.name}", required = true) String authToken) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Auth-Token: %s", authToken));
        }
        preAuthenticatedTokenCacheService.deleteExistingSessions(Collections.singletonList(authToken));
	}
2016-04-09 11:13:29,346 E DefaultExceptionHandler - Http Status: 400 : Missing request header '${auth.token.name}' for method parameter of type String
org.springframework.web.bind.ServletRequestBindingException: Missing request header '${auth.token.name}' for method parameter of type String

Please advise


Affects: 4.3 RC1

Issue Links:

Referenced from: commits 0f0aa46, cb95496

0 votes, 5 watchers

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

My guess is that you did not declare a PropertySourcesPlaceholderConfigurer a.k.a. <context:property-placeholder> in your application context. In that case, $... placeholders are not getting resolved at all, neither in bean definitions nor in annotation attribute values.

This can be rather unintuitive, in particular since #... style expressions are active by default. We should consider supporting placeholders in annotation attributes by default.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

We're registering a default embedded value resolver for the default environment now, in case of none having been registered through post-processors (like PropertyPlaceholderConfigurer and PropertySourcesPlaceholderConfigurer do, with configurable placeholder syntax etc).

This will make it into 4.3 RC2. However, it would be great if you could confirm that a manually added PropertySourcesPlaceholderConfigurer solves the problem in your scenario...

@spring-projects-issues
Copy link
Collaborator Author

Bjorn Harvold commented

This is how I'm registering my environment specific content:

@Configuration
@PropertySource(value = "classpath:server-local.properties")
public class AppLocalConfig {
}

So you are saying this won't work until 4.3RC2?

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Apr 10, 2016

Sam Brannen commented

I updated to 4.3.RC1 today and found that the placeholder still doesn't convert.

You have always had to manually register a PropertySourcesPlaceholderConfigurer for this to work with @Configuration classes.

Here is an excerpt from the Javadoc for PropertySource:

In order to resolve ${...} placeholders in <bean> definitions or @Value annotations using properties from a PropertySource, one must register a PropertySourcesPlaceholderConfigurer. This happens automatically when using context:property-placeholder in XML, but must be explicitly registered using a static @Bean method when using @Configuration classes. See the "Working with externalized values" section of @Configuration's javadoc and "a note on BeanFactoryPostProcessor-returning @Bean methods" of @Bean's javadoc for details and examples.

In other words, adding the following static bean to one of your @Configuration classes should solve your problem.

 @Bean
public static PropertySourcesPlaceholderConfigurer ppc() {
    // instantiate, configure and return ppc ...
}

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Indeed, even a plain

@Bean
public static PropertySourcesPlaceholderConfigurer ppc() {
    return new PropertySourcesPlaceholderConfigurer();
}

should do the job.

The only thing that changes as of 4.3 RC2 is that standard placeholder resolution in annotation attributes will work even without such a placeholder configurer bean in your context.

@spring-projects-issues
Copy link
Collaborator Author

Bjorn Harvold commented

Hi guys,

I added this code to see if it would change anything. I still get the same error.

@Configuration
@PropertySource(value = "classpath:server-local.properties")
public class AppLocalConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer ppc() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Hmm, this should actually work against 4.3 RC1 that way...

Could you alternatively try against 4.3.0.BUILD-SNAPSHOT where that extra bean definition shouldn't even be necessary?

@spring-projects-issues
Copy link
Collaborator Author

Bjorn Harvold commented

Hi Juergen,

We haven't configured our Maven repo to retrieve external snapshots. I'll put a footnote in my code and test this again with RC2. If it doesn't work in RC2, I'm probably doing something wrong on my end.

P.S. I do wonder if you guys ever sleep. The response time on my tickets is phenomenal!

@ipleten
Copy link

ipleten commented May 1, 2020

If someone still wondering why this doesn't work in 2020 with ENV variables (and you still use Spring 4.3.X) check that spring-context-3.1.xsd is 3.1 and not 3.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants