application-context.xml spring中的属性placeholder bean

application-context.xml spring中的属性placeholder bean,spring,spring-mvc,Spring,Spring Mvc,我有一个Spring MVC应用程序,其中我的web.xml如下所示: <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.

我有一个Spring MVC应用程序,其中我的web.xml如下所示:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <display-name>Test</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

但属性未加载到变量中。配置有什么问题吗。另外,我将属性占位符bean从application-context.xml移动到mvc-dispatcher-servlet.xml,然后我无法在application-context.xml中使用属性值,比如说创建数据源。

您可以使用

<util:properties id="myProps" location="WEB-INF/config/prop.properties"/>

这应该可以在根上下文或mvc上下文中使用。

您可以使用

<util:properties id="myProps" location="WEB-INF/config/prop.properties"/>

这应该在根上下文或mvc上下文中工作。

PropertyPlaceHolderConfigure是BeanFactoryPostProcessor,它将仅在定义它的上下文中操作。因此,它将只处理ContextLoaderListener加载的bean。您需要在DispatcherServlet的上下文中添加另一个。@M.Deinum我想在两个地方声明同一个bean不是正确的做法!!!有没有什么解决办法,我只需要创建一个bean?PropertyPlaceHolderConfigure是一个BeanFactoryPostProcessor,它只会在定义它的上下文上运行。因此,它将只处理ContextLoaderListener加载的bean。您需要在DispatcherServlet的上下文中添加另一个。@M.Deinum我想在两个地方声明同一个bean不是正确的做法!!!有没有什么办法可以让我只需要创建一个bean?这正好解决了我的问题。非常感谢。这正好解决了我的问题。谢谢。
package com.test;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

    @Value("${message}")
    String message;

    @RequestMapping(value = "/")
    public String test() {
        String message = "Hello World, Spring 3.0!";
        System.out.println(this.message);
        return "page";
    }
}
<util:properties id="myProps" location="WEB-INF/config/prop.properties"/>
@Value("#{myProps['message']}")