Java 为什么';bean中的t环境在集成上下文中查看源中的属性?

Java 为什么';bean中的t环境在集成上下文中查看源中的属性?,java,spring,spring-integration,spring-environment,Java,Spring,Spring Integration,Spring Environment,在我的Spring Integration webapp配置中,我添加了一个属性占位符: 这是文件内容: apiPath=/requests 我确信此配置有效,因为我已尝试在http入站通道适配器中使用该值: 如果我更改属性值,前端应用程序将无法到达端点 但是,在上下文中,我还配置了一个端点: <int:header-value-router input-channel="httpRequestsChannel" ... > <int:mapping valu

在我的Spring Integration webapp配置中,我添加了一个属性占位符:


这是文件内容:

apiPath=/requests
我确信此配置有效,因为我已尝试在http入站通道适配器中使用该值:


如果我更改属性值,前端应用程序将无法到达端点

但是,在上下文中,我还配置了一个端点:

<int:header-value-router input-channel="httpRequestsChannel" ... >
    <int:mapping value="POST" channel="httpRequestsPostChannel" />
    ...
</int:header-value-router>

<int:channel id="httpRequestsPostChannel" />

<int:chain input-channel="httpRequestsPostChannel">

    <int:transformer method="transform">
        <bean class="transformers.RequestToMessageFile" />
    </int:transformer>

    ...
我想一旦在XML中声明了属性源,它将成为整个web应用程序环境的一部分,我缺少什么?我应该在其他地方申报来源吗

我注意到,如果我添加以下注释:

@配置
@PropertySource(“classpath:config.properties”)
公共类RequestToMessageFile{
该属性已正确找到,因此我猜这只是一个配置问题

如果有必要,下面是配置集成的
web.xml
部分:


上下文配置位置
/META-INF/spring.integration/context.xml
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener

使现代化 我从XML文件中删除了
,并添加了以下bean:

import org.springframework.context.annotation.Configuration;
导入org.springframework.context.annotation.PropertySource;
@配置
@PropertySource(“classpath:config.properties”)
公共类WebappConfig{
}
现在,bean和XML文件都可以看到属性了。

引用Martin Deinum:


不,这不是一个配置问题,而是它应该如何工作。不向环境中添加属性。正如@PropertySource所做的那样

因此,您应该从XML配置中删除
。继续使用
@PropertySource(“classpath:config.properties”)
,并添加此bean定义:

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

注意它必须是静态的。
不要急切地加载同一
@配置中的所有其他bean。

不,它的工作方式不是配置问题。
不会向环境添加属性。
@PropertySource
在哪里做。@M.Deinum那么我如何声明对于XML和所有bean,属性源只在一个位置?或者,XML与
@PropertySource
的等价物是什么?没有。只需在Java中声明它,所有内容都是同一应用程序上下文的一部分。您需要使用
@PropertySource
,还需要声明
属性资源占位符配置器
静态
@Bean
在您的
@配置中
。并且所有内容都不是
。我删除了XML文件条目并添加了一个新Bean(请参阅上面的更新)现在它可以工作了;我是Spring的新手,你能详细说明为什么我需要额外的
PropertySourcePlaceHolderConfigurer
bean吗?因为这就是它必须用Java和Annotations配置的方式。有关更多信息,请参阅它的JavaDocs
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}