Results 1 to 10 of 13

Thread: Wiring Problems in Spring Integration

Hybrid View

  1. #1
    Join Date
    May 2011
    Location
    Mumbai, India
    Posts
    93

    Question Wiring Problems in Spring Integration

    Hi,

    While using SI for software development i came across a problem.

    In my case, Spring Integration does not work properly if i use Setter Injection

    When i use @Autowired for wiring my beans, it works fine. But when i set the property using Setter Injection.
    It is acting wierd.

    Instead of my message going to the next channel i.e set, it goes to the Property bean.

    Say my configuration is:

    Code:
    <bean id="messageElementRefBean"
          class="org.springbyexample.springindepth.bean.Message">
        <property name="message" ref="springMessage"/></property>
    </bean>
    In this case it will print the springMessageBean object.

    And if i use @Autowired.The program works properly.

    What am i doing wrong?

    Regards,
    Annuk
    Last edited by annuk; Apr 16th, 2012 at 02:14 AM.

  2. #2
    Join Date
    May 2011
    Location
    Mumbai, India
    Posts
    93

    Default

    Hi,
    has any one faced the above issue.

    Regards,
    Annuk

  3. #3
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,017

    Default

    I didn't see your original message for some reason.

    I don't understand what you are saying; especially the bit about a message "going" to a property bean. And, what is "it" when you say "...it will print"?

    You need to provide more context - what you are trying to do, code, config etc.
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

  4. #4
    Join Date
    May 2011
    Location
    Mumbai, India
    Posts
    93

    Question Setter injection not working properly with SI

    Hi Gary,

    Thanks for your reply.
    I am just trying to create a simple spring integration project.

    It works fine only when i use @Autowired to wire the beans.

    Say my config is:
    Code:
    <bean id="smsAdapter.htm"  name="/smsAdapter.htm"  class="org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway">
      <constructor-arg value="true" />
      <property name="requestChannel" ref="smsChannel" />
      <property name="replyChannel" ref="gatewayChannel" />
      <property name="supportedMethodNames" >
        <list>
          <value>GET</value>
        </list>
      </property>
    </bean>
    
    <int:transformer id="smsSyncMasters" input-channel="smsChannel"
    		output-channel="gatewayChannel" ref="messageReceiver"  >
    </int:transformer>
    
    <bean id="messageReceiver" class="com.test.messaging.MessageReceiver" />
    
    <bean id="messageService" class="com.test.messaging.MessageService" />
    My message Receiver class is as follows:
    Code:
    public class MessageReceiver
    {
    
           @Autowired
            private ImessageService messageService;
    
    	public Message<String> recieveRequest(Message<Map<String,List<String>>> smsData)
    	{
    		System.out.println("Request Received::"+smsData.getPayload());
    		
    		Map<String,List<String>> dataString = smsData.getPayload();
    		
    		List<String> lists =  dataString.get("content");
    		
    		System.out.println("Content list "+lists);
    		
    		String data = messageService.get(lists.get(0));
    		
    		System.out.println("Data is "+data);
    		return MessageBuilder.withPayload(data).build();
    		
    	}
    }
    If i Autowire my messageService property, i get a response back.
    But if i use a setter injection instead of Autowire, i dont get any response from the messaging gateway.

    EG:

    Code:
    public class MessageReceiver
    {
    
            private ImessageService messageService;
    
            public void setMessageService(imessageService args)
            {
              this.messageService = args;
            }
    
         public imessageService getMessageService()
            {
             return  this.messageService;
            }
    	public Message<String> recieveRequest(Message<Map<String,List<String>>> smsData)
    	{
    		System.out.println("Request Received::"+smsData.getPayload());
    		
    		Map<String,List<String>> dataString = smsData.getPayload();
    		
    		List<String> lists =  dataString.get("content");
    		
    		System.out.println("Content list "+lists);
    		
    		String data = messageService.get(lists.get(0));
    		
    		System.out.println("Data is "+data);
    		return MessageBuilder.withPayload(data).build();
    		
    	}
    }
    
    
    change in configuration
    
    <bean id="messageReceiver" class="com.test.messaging.MessageReceiver" >
    <property name="messageService" ref="messageService"/>
    </bean>

    Setter injection gives me no output whereas Autowire works properly.
    Hope you understood the problem.

    Regards,
    Annuk

  5. #5
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,017

    Default

    That doesn't make any sense; I suggest you run with DEBUG logging; you should be able to see what's going on.

    By the way, you really don't need to expose the messaging system to your service; it can look like this...

    Code:
    public class MessageReceiver
    {
    
           @Autowired
            private ImessageService messageService;
    
    	public String recieveRequest(Map<String,List<String>> smsData)
    	{
    		System.out.println("Request Received::"+smsData);
    		
    		List<String> lists =  smsData.get("content");
    		
    		System.out.println("Content list "+lists);
    		
    		String data = messageService.get(lists.get(0));
    		
    		System.out.println("Data is "+data);
    		return data;
    		
    	}
    }
    It shouldn't make any difference to your problem, but I hope you can see that it removes all deps on S.I. and you have a more easily testable unit.

    You could go a step further and use a SpEL expression to eliminate your MessageReceiver altogether and invoke your message service directly...

    Code:
    <int:transformer id="smsSyncMasters" input-channel="smsChannel"
    		output-channel="gatewayChannel" expression="@messageService.get('payload['content'].get(0))"  >
    </int:transformer>
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

  6. #6
    Join Date
    Oct 2011
    Location
    Mumbai, India
    Posts
    213

    Default

    But if i use a setter injection instead of Autowire, i dont get any response from the messaging gateway.
    Do you see a NullPointerException on the server side?

    <bean id="messageReceiver" class="com.test.messaging.MessageReceiver" />

    <bean id="messageService" class="com.test.messaging.MessageService" />
    This is the config you have given, you haven't got the property sub element to set the messageService attribute. So you'll not get anything if you remove the @Autowire

    Secondly, this is not a spring integration question and is a basic Spring dependency injection query. Also you have posted this
    Code:
    private ImessageService messageService;
    
            public void setMessageService(imessageService args)
            {
              this.messageService = args;
            }
    
         public imessageService getMessageService()
            {
             return  this.messageService;
            }
    This will never get compiled, please take your time and frame the question correctly and try to convey maximum possible information.

    Let us know if it helps sort your issue..
    Last edited by Amol Nayak; Apr 30th, 2012 at 12:33 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •