根据运行时的条件从数据库中获取@Qualifier名称

时间:2018-02-03 05:04:27

标签: java spring-mvc

我已将属性文件中的限定符名称设置为isomessage.qualifier=isoMessageMember1:

public class BankBancsConnectImpl implements BankBancsConnect{

    @Autowired
    @Resource(name="${isomessage.qualifier}")
    private Iso8583Message iso8583Message;

    public BancsConnectTransferComp getFundTransfer(IpsDcBatchDetail ipsDcBatchDetail) {
        bancsxfr = iso8583Message.getFundTransfer(bancsxfr);
    }
}

$ {isomessage.qualifier}的值是静态的,因为它在属性文件中定义。但是我希望它是动态的,并根据某些条件从数据库中获取它的价值。例如,我有多个Iso8583Message(成员明智)的实现,并且必须调用当前登录的各个成员ID类。请指导我以最好的java弹簧方式实现这一点。

我的实现类看起来像这样:

@Service("isoMessageMember1")
public class Iso8583MessageEBLImpl implements Iso8583Message{
    public BancsConnectTransferComp getFundTransfer(BancsConnectTransferComp bancsxfr) throws Exception {
    ...
    }

2 个答案:

答案 0 :(得分:0)

You can use Condition instead Qualifier if you are using Spring4+.

  1. First, you need a ConfigDAO which read the qualifier name which you need from database.

    public class ConfigDAO {
    
        public static String readFromDataSource() {
            return " ";
        }
    }
    
  2. Suppose there are two implemetions of Iso8583Message, you can create two Condition objects.

    IsoMessageMember1_Condition

    public class IsoMessageMember1_Condition implements Condition { 
    
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            String qualifier = ConfigDAO.readFromDataSource();
            if (qualifier.equals("IsoMessageMember1_Condition")) {
                return true;
            } else {
                return false;
            }
        }
    }
    

    IsoMessageMember2_Condition

    public class IsoMessageMember2_Condition implements Condition {
    
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            String qualifier = ConfigDAO.readFromDataSource();
            if (qualifier.equals("IsoMessageMember2_Condition")) {
                return true;
            } else {
                return false;
            }
        }
    }
    
  3. Return different implemetion according to condition in config class.

    @Configuration
    public class MessageConfiguration {
    
        @Bean(name = "iso8583Message")
        @Conditional(IsoMessageMember1_Condition.class)
        public Iso8583Message isoMessageMember1() {
            return new Iso8583MessageEBLImpl();
        }
    
        @Bean(name = "iso8583Message")
        @Conditional(IsoMessageMember2_Condition.class)
        public Iso8583Message isoMessageMember2() {
            return new OtherMessageEBLImpl();
        }
    }
    
  4. Remove the @Qulifier and @Autowire annotations which you do not need anymore, you can retrieve the message from context every time you use it.

    public class BankBancsConnectImpl implements BankBancsConnect{
    
        private Iso8583Message iso8583Message;
    
        public BancsConnectTransferComp getFundTransfer(IpsDcBatchDetail ipsDcBatchDetail) {
            iso8583Message = (Iso8583Message)context.getBean("iso8583Message");
            bancsxfr = iso8583Message.getFundTransfer(bancsxfr);
        }
    }
    

答案 1 :(得分:0)

在春季,可以自动连接应用程序上下文,并根据其名称检索任何bean。 例如,您的界面签名类似于以下语法

 public interface Iso8583Message {
        public String getFundDetails(String uniqueId);
    }

和2种不同的实现方式遵循以下格式

@Service("iso8583-message1")
public class Iso8583MessageImpl1 implements Iso8583Message {
    @Override
    public String getFundDetails(String uniqueId) {
        return "Iso8583MessageImpl1 details ";
    }
}

@Service("iso8583-message2")
public class Iso8583MessageImpl2 implements Iso8583Message {
@Override
public String getFundDetails(String uniqueId) {
     return "Iso8583MessageImpl2 details ";
   }
}

我们可以按以下方式检索bean

    public class BankBancsConnectImpl implements BankBancsConnect{

           @Autowired
            private ApplicationContext applicationContext;


            public BancsConnectTransferComp getFundTransfer(IpsDcBatchDetail 
             ipsDcBatchDetail) {
            //for retrieving 1st implementation
            Iso8583Message iso8583Message=applicationContext.getBean("iso8583-message1", Iso8583Message.class);
           //For retrieving 2nd implementation
            Iso8583Message iso8583Message=applicationContext.getBean("iso8583-message2", Iso8583Message.class);
                String result = iso8583Message.getFundTransfer(bancsxfr);
            }
        }

在这种情况下,我们可以配置数据库名称而不是硬编码值(“ iso8583-message1”,“ iso8583-message2”)。