In my Spring Integration webapp configuration I've added a property placeholder:
<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" />
This is that file content:
apiPath=/requests
I'm sure this configuration works because I've tried using that value in a http inbound-channel-adapter:
<int-http:inbound-channel-adapter id="/api${apiPath}"
channel="httpRequestsChannel"
path="${apiPath}"
...>
</int-http:inbound-channel-adapter>
and if I change the property value the frontend application cannot reach the endpoint.
However, further in the context I have an endpoint so configured:
<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>
...
where I want to read the property value:
public class RequestToMessageFile {
@Autowired
private Environment env;
// ...
public Message<?> transform(LinkedMultiValueMap<String, Object> multipartRequest) {
System.out.println("Value: " + env.getProperty("apiPath"));
But on the console I see:
Value: null
I supposed once declared the property source in the XML that would be part of the whole web app environment, what am I missing? Should I declare the source in another place?
I noticed that if I add the following annotations:
@Configuration
@PropertySource("classpath:config.properties")
public class RequestToMessageFile {
the property is correctly found so I guess this is just a configuration problem.
In case it matters, here's the web.xml
portion that configures integration:
<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>
UPDATE
Partly following this answer I removed <ctx:property-placeholder>
from the XML file and I added the following bean:
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:config.properties")
public class WebappConfig {
}
And now both beans and the XML file can see the properties.