I am working on Spring auto wiring using spring configuration file(xml configuration). I want to inject beans based on a condition. Let me go into details.
- There are two classes '
EmailSender
' and 'SmsSender
' which implement the interfaceIMessageSender
. Beans are configured for both classes in the configurations file.
I have another class
SenderUser
which has a instance variable of typeIMessageSender
in it.package org.pradeep.core;
public class SenderUser { private String name; private String Type;
private IMessageSender msg; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return Type; } public void setType(String type) { Type = type; } public IMessageSender getMsg() { return msg; } public void setMsg(IMessageSender msg) { this.msg = msg; }
}
- I want to inject
IMessageSender
into bean of SenderUser based on the value of SenderUser.getType(). That means first SernderUser.type should be set and then based on it's value (if value is 'email' then bean with the name 'email' should be wired else bean with the name 'sms' should be wired.) msg should be wired.
- I want to inject
Please help me resolve the issue.