Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用注释将属性注入Springbean_Spring_Spring Mvc - Fatal编程技术网

使用注释将属性注入Springbean

使用注释将属性注入Springbean,spring,spring-mvc,Spring,Spring Mvc,正如所解释的,如何做是非常清楚的,但似乎仍然无法使其工作。 我只是喜欢使用@Value注释,以便向Springbean注入属性。我用一个控制器和一个bean创建了一个基本的SpringMVC项目 以下是我的应用程序上下文: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.sprin

正如所解释的,如何做是非常清楚的,但似乎仍然无法使其工作。 我只是喜欢使用@Value注释,以便向Springbean注入属性。我用一个控制器和一个bean创建了一个基本的SpringMVC项目

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

<?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>
现在我只有一个控制器,注入了这个配置类,当我调用这个控制器时,我发现配置类中的值没有正确填充

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

api_url=http://platform.fatsecret.com/js?
api_key=SomeKey
api_secret=SomeSecret
文件名为fatProperties.properties。 当我在调用控制器时调试服务器时,我看到配置类的内容是:

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

我是不是遗漏了什么

更新1:我将PropertyPlaceholderConfigurer bean替换为:

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

得到同样的结果

试试看

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

将其添加到应用程序上下文文件:

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

好的,明白了


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

我为什么要尝试它?我不想使用SpEL,我没有必要。我的定义有什么问题?这看起来是个好主意,但是当使用它而不是我的属性占位符时,我从那一行得到了完全相同的ResultPart,我有完全相同的设置,我的@Value注释工作正常。也许可以尝试打开org.springframework的调试级日志来寻找线索。祝你好运,谢谢你。我认为这可能与SpringMVC结构有关(两种不同的上下文)。您的案例是否也包括spring MVC应用程序?实际上,“看到”有点误导。根据no-beans,父上下文的处理器应用于子上下文的bean。如果要手动替换占位符,可以从父上下文自动连接并使用属性占位符配置器。
<context:property-placeholder location="classpath:fatProperties.properties" />