簡體   English   中英

Spring MVC-從web.xml注入context-param

[英]Spring MVC - inject context-param from web.xml

我已經在web.xml中定義了上下文參數

<context-param>
        <param-name>apikey</param-name>
        <param-value>45370652</param-value>
    </context-param>
    <context-param>
        <param-name>secretkey</param-name>
        <param-value>3eada72ef0ae12e15b138ae098c268c087f08ca8</param-value>
    </context-param>
</web-app>

我在Bean類的類和字段級別啟用了@Component@Value批注。 但是,似乎沒有閱讀它們。 永遠為空

@Component公共類TokBoxSettings {

@Value("${apikey}")
private  String apikey; 

@Value("${secretkey}")
private  String secretkey; 

我還將以下bean映射添加到spring-servlet.xml用於配置PropertyPlaceholder

<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
</bean>

請讓我知道我錯過了什么

我是Spring + Hibernate的初級開發人員,對於這個問題,我有一個替代解決方案,也許您知道

我使用bean名稱及其屬性來設置直接的context-param值

<bean ...>
   <property name="apikey" value="${apikey}" />
</bean>

在bean聲明中,將屬性ignoreUnresolvablePlaceholders設置為true。 即在spring-servlet.xml添加以下標簽

<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

它為我工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM