-1

I have the below static helper method. I am trying to refactor this method by using Spring's method injection.

public static MessageSender getMessageSender(){
    return new MailMessageSender(getMailSession());
}

//getMailSession() is a private static method which used to refresh 
//and return a Session object to the constructor 

I tried to use beans in this context, but it won't work as my methods are static.

Here is what I have tried,(asked in a StackOverflow question)

Note: I have no clear idea how to call my getMailSession() and pass that value to MailMessageSender constructor while using method injection.

How can I make this above method to use Spring's method injection?

Community
  • 1
  • 1
Jude Niroshan
  • 4,280
  • 8
  • 40
  • 62
  • Please write the reason before you are downvoting the question. Sometimes that reason could be solved by editing the question. – Jude Niroshan Mar 27 '17 at 05:08

1 Answers1

1

Change

<bean id="context" class="org.abc.Context">

   <property name="messageSender"><ref bean="mailMessageSender"/></property>
</bean>

To

<bean id="context" class="org.abc.Context">

   <property name="messageSender1"><ref bean="mailMessageSender"/></property>
</bean>

Then

Add the following setter method to your class org.abc.Context:

private void setMessageSender1(MessageSender ms) {
    Context.messageSender = ms;
}
VHS
  • 9,534
  • 3
  • 19
  • 43