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 all predefined variables from ApplicationContext available in SpEL expressions #25037

Closed
quaff opened this issue May 8, 2020 · 5 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Milestone

Comments

@quaff
Copy link
Contributor

quaff commented May 8, 2020

Currently the document only mentioned systemProperties as predefined variable, actually all builtin beans registered by AbstractApplicationContext are predefined variables, such as environment and systemEnvironment and messageSource and applicationEventMulticaster and lifecycleProcessor. It's worthy to add a new section describe those predefined variables.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 8, 2020
@quaff
Copy link
Contributor Author

quaff commented May 8, 2020

Same for javadoc of StandardBeanExpressionResolver

@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 8, 2020
@jhoeller jhoeller added this to the 5.2.7 milestone May 8, 2020
@quaff
Copy link
Contributor Author

quaff commented May 9, 2020

https://github.com/spring-projects/spring-framework/blob/master/src/docs/asciidoc/core/core-expressions.adoc#the-elvis-operator

ExpressionParser parser = new SpelExpressionParser();

String name = parser.parseExpression("name?:'Unknown'").getValue(String.class);
System.out.println(name);  // 'Unknown'

It throws exception instead of print Unknown because no variable name defined

Exception in thread "main" org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:91)
	at org.springframework.expression.spel.ast.Elvis.getValueInternal(Elvis.java:53)
	at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:117)
	at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:177)

@sbrannen
Copy link
Member

It throws exception instead of print Unknown because no variable name defined

Can you please open a separate issue for that?

@sbrannen sbrannen changed the title Improve expressions document Document all predefined variables from ApplicationContext available in SpEL expressions May 10, 2020
@quaff
Copy link
Contributor Author

quaff commented May 11, 2020

It throws exception instead of print Unknown because no variable name defined

Can you please open a separate issue for that?

@sbrannen I've created #25045

@jhoeller jhoeller self-assigned this May 18, 2020
@jhoeller
Copy link
Contributor

I've revised the entire section a bit, adding a note that generally all beans in the context are available as predefined variables in SpEL expressions, including standard context beans... singling out environment, systemProperties and systemEnvironment (including their exposed Java type). This should cover all common cases since SPI beans in the context are really not meant to be commonly accessed in SpEL expressions.

FelixFly pushed a commit to FelixFly/spring-framework that referenced this issue Aug 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

4 participants