【问题标题】:Inject property to spring bean using annotation使用注解将属性注入spring bean
【发布时间】:2012-08-06 03:15:30
【问题描述】:

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

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

<?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>

我有一个名为 Configuration 的 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;
    }

}

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

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

api_url=http://platform.fatsecret.com/js?
api_key=SomeKey
api_secret=SomeSecret

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

${api_key}
${api_secret}
${api_url}

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

我错过了什么吗?

UPDATE1:我将 PropertyPlaceholderConfigurer bean 替换为:

<context:property-placeholder location="classpath:fatProperties.properties"/>

得到相同的结果

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    好的,明白了!

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

    【讨论】:

    • 实际上,“看到”有点误导。根据forum.spring.io/forum/spring-projects/container/…,没有父上下文的 BeanPostProcessors 应用于子上下文的 bean。如果您要进行一些手动占位符替换,您仍然可以自动装配并使用父上下文中的 property-placeholder-configurer。
    【解决方案2】:

    将此添加到您的应用程序上下文文件中:

    <context:property-placeholder location="classpath:fatProperties.properties" />
    

    【讨论】:

    • 这看起来是个好主意,但是当使用它而不是我的属性占位符时,我得到了完全相同的结果
    • 除此之外,我的设置完全相同,我的@Value 注释也可以正常工作。也许尝试打开 org.springframework 的调试级别日志记录以寻找线索。祝你好运。
    • 谢谢 nickdos。我认为这可能与 spring MVC 结构(2 个不同的上下文)有关。您的案例是否还包括一个 Spring MVC 应用程序?
    【解决方案3】:

    试试

    @Value("#{props['api_key']}")
    private String apiKey;
    

    【讨论】:

    • 我为什么要试试?我不想使用SpEL,我不需要。我的定义有什么问题?
    猜你喜欢
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2016-12-26
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多