使用注释将属性注入到 spring bean

for*_*has 2 spring spring-mvc

正如这里这里所解释的,很清楚如何做到这一点,但似乎仍然无法使其发挥作用。我只是喜欢使用 @Value 注释来将属性注入到 spring bean 中。我创建了一个基本的 spring MVC 项目,其中包含一个控制器和一个 bean。

这是我的应用程序上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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" xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   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/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
   http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd">


<!-- Root Context: defines shared resources visible to all other web components -->

<context:component-scan base-package="me.co.fatsecret" />

<!-- Properties -->

<bean id="props"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:fatProperties.properties" />
</bean>


</beans>
Run Code Online (Sandbox Code Playgroud)

我有一个名为配置的 bean:

package me.co.fatsecret;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Configuration {

    /*--- Members ---*/

    @Value("${api_key}")
    protected String API_KEY;
    @Value("${api_secret}")
    protected String API_SECRET;
    @Value("${api_url}")
    protected String API_URL;

    /*--- Constructors ---*/

    public Configuration() {
    }

    /*--- Getters & Setters ---*/

    public String getAPI_KEY() {
    return API_KEY;
    }

    public void setAPI_KEY(String aPI_KEY) {
    API_KEY = aPI_KEY;
    }

    public String getAPI_SECRET() {
    return API_SECRET;
    }

    public void setAPI_SECRET(String aPI_SECRET) {
    API_SECRET = aPI_SECRET;
    }

    public String getAPI_URL() {
    return API_URL;
    }

    public void setAPI_URL(String aPI_URL) {
    API_URL = aPI_URL;
    }

}
Run Code Online (Sandbox Code Playgroud)

现在我只有一个控制器,注入了此配置类,当我调用此控制器时,我发现配置类中的值填充不正确。

我的属性文件位于资源文件夹 (src/main/resources) 下,并且是我的类路径的一部分(默认情况下完成,因为这是一个 Maven 项目)。这里是:

api_url=http://platform.fatsecret.com/js?
api_key=SomeKey
api_secret=SomeSecret
Run Code Online (Sandbox Code Playgroud)

文件名为 fatProperties.properties。当我在调用控制器时调试服务器时,我看到 Configuration 类的内容是:

${api_key}
${api_secret}
${api_url}
Run Code Online (Sandbox Code Playgroud)

这是字符串的实际值,这意味着属性文件中的值由于某种原因没有被注入。

我在这里错过了什么吗?

UPDATE1:我用以下内容替换了 PropertyPlaceholderConfigurer bean:

<context:property-placeholder location="classpath:fatProperties.properties"/>
Run Code Online (Sandbox Code Playgroud)

得到相同的结果

for*_*has 5

好的,我知道了!

我正在使用 Spring MVC 项目,这意味着我的 Web 层(控制器)有一个单独的上下文。使用 @Value 注释保存属性的“配置”bean 被注入到控制器中。我的属性占位符是在我的根上下文中定义的,因此从我的控制器中看不到它。为了解决这个问题,我只需将属性占位符定义添加到我的 DispatcherServlet 上下文中,它就像一个魅力:)