【问题标题】:Injecting property file values into Controller via Spring not working通过Spring将属性文件值注入控制器不起作用
【发布时间】:2014-04-01 10:56:18
【问题描述】:

希望您能提供帮助。如果解决方案非常明显,我的 Spring 知识非常有限。

以下是相关文件的内容:

应用程序上下文.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:util="http://www.springframework.org/schema/util"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.1.xsd
         http://www.springframework.org/schema/util
         http://www.springframework.org/schema/util/spring-util-3.1.xsd">

    <context:annotation-config />

    <util:properties id="messageConfig" location="classpath:customMessages.properties"/>

</beans>

/src/main/resources/customMessages.properties:

sms.message=Test

Java 类:

@Controller
public class ApiRestController implements ApiRest {
    @Value("#{messageConfig['sms.message']}")
    private String smsMessage;

    public final void setSmsMessage (String smsMessage)
    {
        this.smsMessage = smsMessage;
    }

    ...
}

但是,当在 Java 类的其他地方引用“smsMessage”变量时,该值为 NULL。有什么想法我可能会错过吗?任何指针将不胜感激。

【问题讨论】:

  • 你怎么称呼这个控制器?

标签: java spring dependency-injection


【解决方案1】:

添加

<context:component-scan base-package="spring-controllers-package"/>

application-context.xml

【讨论】:

  • 感谢您的快速回复。我已将此添加到 application-context.xml(将“spring-controllers-package”替换为我的控制器所在的包),但不幸的是我仍然看到同样的问题。
猜你喜欢
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
  • 2015-02-03
  • 1970-01-01
相关资源
最近更新 更多