简体   繁体   English

在运行时选择spring bean

[英]select spring bean during runtime

I have a problem to select the correct spring bean that should get injected. 我在选择应该注射的正确春豆时遇到问题。 I need a way to tell the spring container what bean to inject depeding on the call to a previous class. 我需要一种方法来告诉Spring容器在上一类的调用中注入什么豆子。 I do all the spring bean wiring in xml. 我在xml中完成所有的spring bean接线。

My question : is this possible and if it is any reference on an implementation? 我的问题 :这可能吗?是否对实现有任何参考?

I have created some sample code to illustrate what i´m trying to accomplish. 我创建了一些示例代码来说明我要完成的工作。 Feel free to change it so that it will work to get the correct ReportHeader bean injected depending on the selected reportType during runtime. 随时对其进行更改,以便它将在运行时根据所选的reportType获得正确的ReportHeader bean注入。

public enum ReportType{
    Credit,
    Annul   
}

public class ReportService {

    private ReportHeaderService reportHeaderService;
    private ReportType reportType;

    public ReportService (){}

    public setReportType(ReportType reportType){
        this.reportType = reportType;
    }

    public void setReportHeaderService(ReportHeaderService reportHeaderService){
        this.reportHeaderService = reportHeaderService;
    }

    private void generateHeader(){
        //i would like to call my service like this and have the correct bean injected to ReportHeader.
        reportHeaderService.generateHeader(reportType)
    }
}

public class ReportHeaderService {

    private ReportHeader reportHeader;

    //this will call the injected bean that needs to be selected accoring to the ReportType
    public void generateHeader(ReportType type){
        reportHeader.createHeader();
    }
}

public interface ReportHeader{
    public void createHeader();
}

public class CreditReportHeader implements ReportHeader{
    public void createHeader(){
        ..dostuff();
    }
}

public class AnnulReportHeader implements ReportHeader{
    public void createHeader(){
        ..dostuff();
    }
}

Consider injecting a Map<ReportType, ReportHeader> to ReportHeaderService, so that generateHeader works as: 考虑将Map<ReportType, ReportHeader>注入ReportHeaderService,以便generateHeader的工作方式如下:

public class ReportHeaderService {

    private Map<ReportType, ReportHeader> reportHeaderMap;

    public void generateHeader(ReportType type){
        ReportHeader reportHeader = reportHeaderMap.get(type);
        if (reportHeader != null) {
            reportHeader.createHeader();
        }
    }
}

You can define a ReportHeaderFactory to get the ReportHeader according to ReportType : 您可以定义一个ReportHeaderFactory以根据ReportType获取ReportHeader

public class ReportHeaderFactory {
    private CreditReportHeader creditReportHeader;
    private AnnulReportHeader annulReportHeader;

    public ReportHeader getReportHeader(ReportType reportType) {
        switch (reportType) {
        case Credit:
            return creditReportHeader;
        case Annul:
            return annulReportHeader;
        default:
            throw new IllegalArgumentException("No Such Header");
        }
    }
}

Re-define the ReportHeaderService with an instance of ReportHeaderFactory : ReportHeaderService的实例重新定义ReportHeaderFactory

public class ReportHeaderService {

    //private ReportHeader reportHeader;
    private ReportHeaderFactory headerFactory;

    //this will call the injected bean that needs to be selected accoring to the ReportType
    public void generateHeader(ReportType type){
        //reportHeader.createHeader();
        headerFactory.getReportHeader(type);
    }
}

As you are doing all the spring bean wiring in xml, you just need to make below entries in the config file: 在xml中进行所有spring bean接线时,只需在配置文件中进行以下输入即可:

    <bean id="ReportHeaderService" class="x.y.ReportHeaderService">
        <property name="headerFactory" ref="headerFactory" />
    </bean>

    <bean id="headerFactory" class="x.y.ReportHeaderFactory">
        <property name="creditReportHeader" ref="creditReportHeader" />
        <property name="annulReportHeader" ref="annulReportHeader" />
    </bean>

    <bean id="creditReportHeader" class="x.y.CreditReportHeaderImpl" />
    <bean id="annulReportHeader" class="x.y.AnnulReportHeaderImpl" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM