Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in the below case @value("${someProperty}") is working while @value("#{someProperty}"), is not working [duplicate]

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.

like image 685
Krushna Avatar asked Apr 17 '13 07:04

Krushna


People also ask

Which annotations uses SpEL expression language to access the property values?

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> } .

What makes SpEL more powerful than el?

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.

What can you reference using SpEL?

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.


1 Answers

#{ } 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}'}")
like image 175
Vitaly Avatar answered Oct 12 '22 23:10

Vitaly