I have the below spring configuration:
<context:property-placeholder location="classpath:commonSql.properties" />
Now in my class, when I use @value("#{someproperty}")
it did not work. Then, I changed to
@value("${someproperty}")
and it worked.
According to answer of this questions @value("#{someproperty}")
is SpEL syntax, which is far more capable and complex. It can also handle property placeholders, and a lot more besides but in my case why it's not working ? While the simple one is working how both $ and # are use to evaluate the value.
The main thing is @value("#{someproperty}")
is not working while @value("${someproperty}")
is working.
SpEL expressions can be used with XML or annotation based configuration metadata for defining BeanDefinitions. In both cases the syntax to define the expression is of the form #{ <expression string> } .
The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality.
SpEL can be used independently with the usage of ExpressionParser and EvaluationContext or can be used on top of fields, method parameters, constructor arguments via @Value annotation @Value("#{ … }") . SpEL expressions are usually interpreted during runtime, this is good since it provides a lot of dynamic features.
#{ }
is an expression language feature, while ${ }
is a simple property placeholder syntax.
Expression language means that there is a specific syntax, objects, variables and so on.
When you write "#{someproperty}", you actually referring to the object and expression language engine answers you:
Field or property 'someproperty' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
Here is what will work:
@Value("#{'${someproperty}'}")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With