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

Document how SpEL defaulting syntax differs from PropertyPlaceholder defaulting [SPR-7674] #12330

Closed
spring-projects-issues opened this issue Oct 22, 2010 · 1 comment
Assignees
Labels
type: documentation A documentation task type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 22, 2010

Daniel Hopper opened SPR-7674 and commented

Currently there is support for default values with @Value when using property place holders

@Value("${fee:25}")
private int overdraft;

But this doesn't work with SPeL expressions. While this isn't inherently an issue at this point I think this will be problematic with the new features in Spring 3.1 for the Environment configuration profiles.

public class SpelExpressionDefaultTest {

	@Test
	public void testSpelDefaultValue() {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
			PropertyPlaceholderConfigurer.class, FeeService.class
		);
		
		Assert.assertEquals(25, ctx.getBean(FeeService.class).getOverdraft());
	}
	
	@Component
	public static class FeeService {
		@Value("#{systemProperties['fee']:25}")
		private int overdraft;

		public int getOverdraft() {
			return overdraft;
		}
	}
}

This particular test will fail on a parsing exception from SPeL. This directly relates to #11472 which was closed as invalid as there is a work around to just use ${:default} syntax instead.

If we change the FeeService to

@Component
public static class FeeService {
	@Value("${fee:25}")
	private int overdraft;

	public int getOverdraft() {
		return overdraft;
	}
}

This will work as expected. SPeL should probably support the default value option especially with the new Environment Profiles coming in 3.1


Affects: 3.0.4

Referenced from: commits f20ded5

@spring-projects-issues
Copy link
Collaborator Author

Daniel Hopper commented

Thinking about this a little more I did test the following expression using the Elvis Operator and it does work this way.

@Value("#{systemProperties['fee']?:25}")

So I guess this then does function as it should in terms of defaults, but maybe the documentation should be updated to show this.

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

No branches or pull requests

2 participants