2015-11-05 41 views
0

注入的context-param我已经定义在web.xml中的上下文参数Spring MVC的 - 从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; 

我还增加了以下豆映射spring-servlet.xml配置PropertyPlaceholder

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

请让我知道我错过了

+0

通常,bean定义和设置包含在'@ Configuration'注释类中。是否尝试将'@ Component'更改为'TokBoxSettings'的'@ Configuration'?这两种刻板印象在排序上可能有所不同。 – px5x2

回答

1

我是初级spring + hibernate开发者,我有一个替代解决方案来完成这个任务离子,可能是你知道

我用bean的名字和它的属性设置直接的context-param值豆声明中

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

设置属性ignoreUnresolvablePlaceholders为true。即在下面添加标签spring-servlet.xml

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

它为我工作。

相关问题