【问题标题】:Spring MVC - inject context-param from web.xmlSpring MVC - 从 web.xml 注入上下文参数
【发布时间】:2015-11-05 10:12:52
【问题描述】:

我已经在 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 注释。但是,它似乎没有阅读它们。它始终为空

@组件 公共类 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>

请告诉我我错过了什么

【问题讨论】:

  • 一般bean定义和设置都包含在@Configuration注解的类中。您是否尝试将TokBoxSettings@Component 更改为@Configuration?这两种刻板印象的顺序可能不同。

标签: java spring-mvc web.xml spring-annotations


【解决方案1】:

我是初级 spring + hibernate 开发人员,我有这个问题的另一种解决方案,可能你知道

我使用 bean name 和它的属性来直接设置 context-param 值

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

【讨论】:

    【解决方案2】:

    在 bean 声明中将属性 ignoreUnresolvablePlaceholders 设置为 true。即在spring-servlet.xml中添加以下标签

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

    它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-04
      • 2014-03-16
      • 2018-06-26
      • 2010-11-06
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      相关资源
      最近更新 更多