Spring将singleton bean注入原型bean,导致单例重新创建

时间:2018-05-09 13:30:24

标签: java spring dependency-injection

我目前有一个简单的singleton bean作为pojo,我将其注入一个工作线程,该线程由@scope("prototype")定义。

我发现正确地将singleton bean注入原型,singleton正在重新初始化,所有值都返回null

以下是注入singleton的代码:

@Component("filterWorkerPaired")
@Scope("prototype")
public class FilteringWorkerPaired implements FilteringWorker {


private RangeFilteringParams rangeFilteringParams;


@Inject
@Named("rangeFilteringParams")
public void setRangeFilteringParams(RangeFilteringParams rangeFilteringParams) {
    this.rangeFilteringParams = rangeFilteringParams;
}

The Pojo:

@Component("rangeFilteringParams")
public class RangeFilteringParams {

private Footprint footprint;
private SpanLength spanLength;
private WindowLength windowLength;
private boolean isPaired;
private List<MappingStrandSE> mappingStrandsSE;
private List<MappingTypeSE> mappingTypesSE;
private List<MappingStrandPE> mappingStrandsPE;
private List<MappingTypePE> mappingTypesPE;
private String suffix;
private boolean isLastStream = false; //default

@PostConstruct
public void init(){
    System.out.println("init filtering ");
}

@PreDestroy
public void destory(){
    System.out.println("destoryed");
}

public boolean isLastStream() {
    return isLastStream;
}

public void setLastStream(boolean lastStream) {
    isLastStream = lastStream;
}

public Footprint getFootprint() {
    return footprint;
}

public void setFootprint(Footprint footprint) {
    this.footprint = footprint;
}

public String getSuffix() {
    return suffix;
}

public void setSuffix(String suffix) {
    this.suffix = suffix;
}

public SpanLength getSpanLength() {
    return spanLength;
}

public void setSpanLength(SpanLength spanLength) {
    this.spanLength = spanLength;
}

public WindowLength getWindowLength() {
    return windowLength;
}

public void setWindowLength(WindowLength windowLength) {
    this.windowLength = windowLength;
}

public boolean isPaired() {
    return isPaired;
}

public void setPaired(boolean paired) {
    isPaired = paired;
}

public List<MappingStrandSE> getMappingStrandsSE() {
    return mappingStrandsSE;
}

public void setMappingStrandsSE(List<MappingStrandSE> mappingStrandsSE) {
    this.mappingStrandsSE = mappingStrandsSE;
}

public List<MappingTypeSE> getMappingTypesSE() {
    return mappingTypesSE;
}

public void setMappingTypesSE(List<MappingTypeSE> mappingTypesSE) {
    this.mappingTypesSE = mappingTypesSE;
}

public List<MappingStrandPE> getMappingStrandsPE() {
    return mappingStrandsPE;
}

public void setMappingStrandsPE(List<MappingStrandPE> mappingStrandsPE) {
    this.mappingStrandsPE = mappingStrandsPE;
}

public List<MappingTypePE> getMappingTypesPE() {
    return mappingTypesPE;
}

public void setMappingTypesPE(List<MappingTypePE> mappingTypesPE) {
    this.mappingTypesPE = mappingTypesPE;
}

}

我知道正在重新创建RangeFileringParams bean,因为调试器中的ID发生了变化,并且init filtering被打印到控制台。

以这种方式将单例注入原型是否有问题?

感谢。

2 个答案:

答案 0 :(得分:1)

正如你所说它已经解决了但无论如何。如果您正在创建具有初始状态的bean,我建议使用配置而不仅仅是@Component注释,然后您可以创建bean并在一个位置初始化字段。 例如:

@Configuration
public class RangeFilteringParamsConfiguration {

    @Bean
    public RangeFilteringParams rangeFilteringParams() {
        RangeFilteringParams params = new RangeFilteringParams();
        //set the fields here...
        return params;
    }
}

答案 1 :(得分:1)

如果仅使用new运算符创建原型bean,则无法自动连接单例bean。

@Component
@Scope("prototype")
public class ExamplePType implements Serializable {
    @Autowired
    private transient Cache cache
}

下面一些代码不会给出在ExamplePType类中注入的自动接线bean。

  ExamplePType obj = new ExamplePType ();

使用“应用程序上下文”创建对象,以识别注入的bean来保留自动装配的bean。

@Component
public class Factory implements ApplicationContextAware {   
    private ApplicationContext ctx;
    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        this.ctx = ctx;

    }       
    public String run() {               
        ExamplePType obj = ctx.getBean(ExamplePType.class);        
    }   
}