7

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.

Community
  • 1
  • 1
Krushna
  • 5,059
  • 5
  • 32
  • 49
  • 3
    See here: http://stackoverflow.com/questions/5322632/spring-expression-language-spel-with-value-dollar-vs-hash-vs – Bob Flannigon Apr 17 '13 at 07:32
  • @Bob Flannigon according the answers of the questions both are same so both should work but here it's not working when i put # – Krushna Apr 17 '13 at 07:35
  • Stackoverflow's very own [`el`](http://stackoverflow.com/tags/el/info) tag has some great information on this – andyb Apr 17 '13 at 07:46

2 Answers2

5

#{ } 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}'}")
Vitaly
  • 2,760
  • 2
  • 19
  • 26
0

Here is the source code of spring which shows the expression of getting a property key, and the reason is quite obvious, maybe help you:)

/** 
 * Abstract base class for PropertyEditors that need 
 * to resolve placeholders in paths. 
 * 
 * <p>A path may contain ${...} placeholders, to be resolved as 
 * system properties: e.g. ${user.dir}. 
 * 
 * @author Juergen Hoeller 
 * @since 1.1.2 
 * @see #PLACEHOLDER_PREFIX 
 * @see #PLACEHOLDER_SUFFIX 
 * @see System#getProperty(String) 
 */  
public class AbstractPathResolvingPropertyEditor extends PropertyEditorSupport {  

    public static final String PLACEHOLDER_PREFIX = "${";  

    public static final String PLACEHOLDER_SUFFIX = "}";  

    protected static final Log logger = LogFactory.getLog(  
            AbstractPathResolvingPropertyEditor.class);  
  ...etc
Hunter Zhao
  • 4,589
  • 2
  • 25
  • 39