java - 为什么 bean 中的环境看不到集成上下文中源的属性?

标签 java spring spring-integration spring-environment

在我的 Spring Integration web 应用程序配置中,我添加了一个属性占位符:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:ctx="http://www.springframework.org/schema/context"
    ...
    xsi:schemaLocation="http://www.springframework.org/schema/context
            ...
            ">

    <ctx:component-scan ... />
    <ctx:annotation-config />
    <mvc:annotation-driven />

    <ctx:property-placeholder location="classpath:config.properties" trim-values="true" />

这是文件内容:

apiPath=/requests

我确信此配置有效,因为我已尝试在 http 入站 channel 适配器中使用该值:

<int-http:inbound-channel-adapter id="/api${apiPath}"
        channel="httpRequestsChannel"
        path="${apiPath}"
        ...>
</int-http:inbound-channel-adapter>

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

但是,在上下文中,我有一个这样配置的端点:

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

    ...

我想在哪里读取属性值:

public class RequestToMessageFile {

    @Autowired
    private Environment env;

    // ...

    public Message<?> transform(LinkedMultiValueMap<String, Object> multipartRequest) {
        System.out.println("Value: " + env.getProperty("apiPath"));

但是在控制台上我看到:

Value: null

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

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

@Configuration
@PropertySource("classpath:config.properties")
public class RequestToMessageFile {

该属性已正确找到,因此我猜这只是一个配置问题。

如果重要的话,这里是web.xml配置集成的部分:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/META-INF/spring.integration/context.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>
<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<小时/>

更新

部分关注this answer我删除了<ctx:property-placeholder>从 XML 文件中,我添加了以下 bean:

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:config.properties")
public class WebappConfig {

}

现在 bean 和 XML 文件都可以看到这些属性。

最佳答案

引用 Martin Deinum:

No it isn't a configuration issue that is how it is supposed to work. The doesn't add properties to the environment. Where as the @PropertySource does.

因此您应该删除 <ctx:property-placeholder>来自您的 XML 配置。继续使用@PropertySource("classpath:config.properties")并添加此 bean 定义:

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

注意它必须如何static不要急切地加载同一个 @Configuration 中的所有其他 bean .

关于java - 为什么 bean 中的环境看不到集成上下文中源的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50407206/

相关文章:

spring - Spring Integration Kafka 消费者的回滚偏移量

java - 入站和出站网关 AMQP 注释

java - 如何通过ubuntu终端将写在.txt中的参数传递给java程序?

java - 由于依赖于 postgres 容器,spring boot 容器未运行

javassist,获取doGet方法的第二个参数

java - Spring中的@Transaction

java - SwingFXUtils 图像序列化替代方案(Javafx、Swing、Raspberry Pi)

java - Android 权限保护级别 18 和 50

java - 使用 MySQL 流式传输大型结果集

spring-integration - 单个上下文中多个入站 channel 适配器/轮询器的行为