Search code examples
spring-bootapplication.properties

get properties from application.properties in spring boot


I want to get values using @Value annotation from application.properties in spring boot web project. I thought I can just use @Value annotation because i thought the variables in application.properties are just loaded on the fly. But when I write just @Value annotation it doens't work.

I have a property in application.poperties

google.recaptcha.site-key=???

And I wanted to load the value, so I coded like below.

@Controller
@RequestMapping("/member")
public class MemberController extends BaseController{

    @Value("#{google.recaptcha.site-key}")
    public String recaptchaSiteKey; 

}

But eclipse returns error when I compile the code.

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'google' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:226) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:51) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.ast.OpMinus.getValueInternal(OpMinus.java:98) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:121) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:262) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:161) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    ... 28 common frames omitted

Saying cannot resolve google object since the property name is starting with "google.XXX".

How do I solve this issue??


Solution

  • In @Value("#{google.recaptcha.site-key}") try using $ instead of #.

    EDIT: To clarify this a bit.

    # tells spring to interpret following string as SpEL. So in order to get system property with # you'd have to write it like that:

    • @Value("#{systemProperties['google.recaptcha.site-key']}").

    I think it should work at least