[英]get properties from application.properties in spring boot
我想使用 @Value 注釋從 spring boot web 項目中的 application.properties 獲取值。 我以為我可以只使用 @Value 注釋,因為我認為 application.properties 中的變量只是即時加載的。 但是當我只寫 @Value 注釋時,它不起作用。
我在 application.poperties 中有一個屬性
google.recaptcha.site-key=???
我想加載這個值,所以我編碼如下。
@Controller
@RequestMapping("/member")
public class MemberController extends BaseController{
@Value("#{google.recaptcha.site-key}")
public String recaptchaSiteKey;
}
但是當我編譯代碼時 eclipse 返回錯誤。
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
說無法解析 google 對象,因為屬性名稱以“google.XXX”開頭。
我該如何解決這個問題??
在@Value("#{google.recaptcha.site-key}")
嘗試使用$
而不是#
。
編輯:澄清一下。
#
告訴 spring 將以下字符串解釋為 SpEL。 因此,為了使用#
獲取系統屬性,您必須這樣編寫:
@Value("#{systemProperties['google.recaptcha.site-key']}")
。我認為它至少應該起作用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.