Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Wiring Problems in Spring Integration

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

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

    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.

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

    Question

    Hi Amol,
    Thanks for your reply.
    Do you see a NullPointerException on the server side?
    No i dont see any null pointer on my server side.

    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
    Exactly.if i remove them i will not get anything.

    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 messageService)
            {
              this.messageService = messageService;
            }
    
         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..
    If you go through the post again, you could see this:
    Code:
    <bean id="messageReceiver" class="com.test.messaging.MessageReceiver" >
    <property name="messageService" ref="messageService"/>
    </bean>
    This is the issue i faced when i used setter injection with SPRING INTEGRATION.
    you could use the code above and see the output.

    My code works fine, if i use @Autowired.But when i use Setter injection it goes kaput.

    Regards,
    Annuk
    Last edited by annuk; May 2nd, 2012 at 09:32 AM.

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

    Default

    No i dont see any null pointer on my server side.
    Well then the setter injection is working else you would have got a NullPointerException on the line marked in bold below

    Code:
    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;
    Code:
    If you go through the post again, you could see this:
    I am afraid i still don't see

    Code:
    <bean id="messageReceiver" class="com.test.messaging.MessageReceiver" >
    <property name="messageService" ref="messageService"/>
    </bean>
    but i can just see

    Code:
    <bean id="messageReceiver" class="com.test.messaging.MessageReceiver" />
    <bean id="messageService" class="com.test.messaging.MessageService" />
    Later will work only in case when Autowire annotation is present, in its absence you need to use the previous definition and that is what i want to convey.

    My code works fine, if i use @Autowired.But when i use Setter injection it goes kaput.
    Now if you don't get an exception, what exactly happens thats not working as you expected it to?

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

    Default

    Hi Amol,
    Thanks for your reply.

    I am still confused by this behaviour of SI.

    If i use a setter injection, the property is considered as a service activator and the request is forwarded to that property instead of the service activator configured.

    is any one facing the same issue?

    Regards,
    Annuk

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

    Default

    Hi,

    Any update on this.
    We are still facing issues in the project where we have used Setters and Getters instead of @Autowired.
    What could be the issue here.

    Regards,

    Annuk

Posting Permissions

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