Announcement Announcement Module
No announcement yet.
SpEL @Value to get headers Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • SpEL @Value to get headers

    Is it possible to use @Value to get the message headers?

    Want to get the message headers that were set in the config file, but don't even know if this is possible.
    I've tried @Value("#{headers}") which gives me a big fat null.

    Also, is there a way to use @Value and SpEL to get all of the header-enricher values?

  • #2
    @Value is used for injection of values that are available at configuration time whereas "headers" are available per-Message instance at runtime.

    Could you provide a little bit more context about what you are trying to accomplish?



    • #3
      In my configuration I have
            <int:header name="fred"
                        value="wilma" overwrite="false"/>
            <int:header name="barney"
                        value="betty"  overwrite="false"/>
      In my test I want to make sure that fred goes with wilma and not betty.
      I would like to be able to get the fred value with @Value in the test class.
      private String fredValue;
      (I also posted the question on StackOverflow.)
      Last edited by kxen; Apr 12th, 2013, 12:54 PM.


      • #4
        You need to provide much more context - as Mark said, @Value is to initialize beans in the context - one time only during initialization, whereas messages are run-time objects that come and go.

        If you can explain exactly what you are trying to (and what class fredValue lives in), I am sure we can help.


        • #5
          Here is where I would like fred to live
          @ContextConfiguration( {
                                 } )
          @RunWith( SpringJUnit4ClassRunner.class )
          public class BedrockTest {
            @Value("#{T(com.slate.HeaderKeys).FRED_KEY}") // I can do this
            private String _fredKey;
            @Value("#{headers[#{T(com.slate.HeaderKeys).FRED_KEY}]}") // I don't know if I can do this
            private String _fredValue;
          @Test public void testSomething() {}
          In my config I have
                <int:header name="#{T(com.slate.HeaderKeys).FRED_KEY}"
                            expression="@destinationProvider.destination"  overwrite="false"/>
          or even
                <int:header name="#{T(com.slate.HeaderKeys).FRED_KEY}"
                            expression="#{T(com.slate.Contants).X}"  overwrite="false"/>
          I want to get the value of "@destinationProvider.destination"

          I think you've already answered my question. The headers change at run time and are passed along with each method. I suppose there is no grand headers storage area that I can grab (that I hoped to do), and be able to get anything that was added by a header-enricher during runtime.

          My purpose for doing this, is that I want to see what the header values were when a service activator was called (where the message went through head enrichers, an outbound-gateway, and a couple transformers before the service activator was invoked).

          SpEL syntax is a weak point for me, I don't know what is available at a config time versus run time.


          • #6
            There are several ways to do what you want; one way is to have your test add an interceptor at the appropriate place, and capture the message as it moves through the channel.

            Gist here...


            • #7
              Thanks. The example from the Gist worked great, and I even learned more about how channels work.