0

我在选择应该注入的正确 spring bean 时遇到问题。我需要一种方法来告诉 spring 容器注入什么 bean,这取决于对前一个类的调用。我在 xml 中完成所有 spring bean 接线。

我的问题:这是否可能,如果它是对实现的任何参考?

我创建了一些示例代码来说明我想要完成的工作。随意更改它,以便在运行时根据所选的 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();
    }
}
4

2 回答 2

3

考虑将 a 注入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();
        }
    }
}
于 2013-09-27T08:36:22.520 回答
1

您可以定义 aReportHeaderFactory以获取ReportHeader根据ReportType

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");
        }
    }
}

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);
    }
}

当您在 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" />
于 2013-09-27T10:19:17.073 回答