Announcement Announcement Module
Collapse
No announcement yet.
Wiring Problems in Spring Integration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 03:14 AM.

  • #2
    Hi,
    has any one faced the above issue.

    Regards,
    Annuk

    Comment


    • #3
      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.

      Comment


      • #4
        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

        Comment


        • #5
          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>

          Comment


          • #6
            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, 01:33 PM.

            Comment


            • #7
              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, 10:32 AM.

              Comment


              • #8
                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?

                Comment


                • #9
                  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

                  Comment


                  • #10
                    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

                    Comment


                    • #11
                      It looks to me like the last post by Amol explains what you are doing wrong. If you are using setter injection without auto-wiring, you must include the <property> sub-element in your bean definition.

                      Comment


                      • #12
                        Hi Mark,

                        Thanks for your reply.

                        Originally posted by Mark Fisher View Post
                        It looks to me like the last post by Amol explains what you are doing wrong. If you are using setter injection without auto-wiring, you must include the <property> sub-element in your bean definition.
                        Using the property element creates the problem. When the property element is used with SI, instead of going to the next defined channel, it considers the property element as a channel and it results in an error.

                        Say channel1 is my input channel, channel2 my output channel, myService as a service activator which has property myDAO in it. If myDAO is autowired, the code works fine.

                        But if setter injection is used, instead of going into the output channel it considers myDAO as the channel.

                        Hope you understood the problem.

                        Regards,
                        Annuk

                        Comment


                        • #13
                          I suggest you show us your actual configuration that exhibits the behavior that you claim, together with a DEBUG log. I suggested that in post #5 but didn't see any response.

                          People use setters with SI all the time so there must be some basic misunderstanding here.

                          Comment

                          Working...
                          X