gpt4 book ai didi

java - Spring 将单例 bean 注入(inject)原型(prototype) bean 导致单例重新创建

转载 作者:行者123 更新时间:2023-12-02 02:09:19 28 4
gpt4 key购买 nike

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

我发现singleton bean被正确注入(inject)到原型(prototype)中,singleton正在被重新初始化并且所有值都返回null .

这是注入(inject)单例的代码:

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


private RangeFilteringParams rangeFilteringParams;


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

波乔:

@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 过滤 被打印到控制台。

以这种方式将单例注入(inject)原型(prototype)有什么问题吗?

谢谢。

最佳答案

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

@Configuration
public class RangeFilteringParamsConfiguration {

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

关于java - Spring 将单例 bean 注入(inject)原型(prototype) bean 导致单例重新创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50254673/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com