java - 为什么 bean 中的 Environment 在集成上下文中看不到来自源的属性?
问题描述
在我的 Spring Integration webapp 配置中,我添加了一个属性占位符:
<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 inbound-channel-adapter 中使用该值:
<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>
更新
部分遵循这个答案,我<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 的话:
不,这不是应该如何工作的配置问题。不会向环境添加属性。正如@PropertySource 所做的那样。
因此,您应该<ctx:property-placeholder>
从 XML 配置中删除。继续使用@PropertySource("classpath:config.properties")
并添加这个 bean 定义:
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
注意它是static
如何不要急切地加载所有其他 bean 的@Configuration
。
推荐阅读
- c - scanf 注册新的转换说明符
- java - 为什么我的应用程序图标在 android studio 的应用程序网格列表中显示为空白?
- utf-8 - 如何使用 HiveQL 提取表情符号?
- c# - 用 3 个字符月份转换字符串日期
- python - 在不支持步幅的情况下有效地将卷积滤波器转换为带状矩阵
- javascript - Cordova 找不到插入式 OTG 设备
- python - Simple_Salesforce 动态批量查询
- mongodb - 如何通过 Postman 将 ISODate 插入 mongoDB?
- c++ - 类和对象实践
- e2e-testing - 长时间延迟(5 分钟)后显示的 UI 功能的端到端测试