Sam
Sam

Reputation: 1304

Spring injecting singleton bean into prototype bean causing singleton re-creation

I currently have a simple singleton bean as a pojo, which i'm injecting into a worker thread, which is defined with @scope("prototype").

I'm finding that rather the singleton bean being correctly injected into the prototype,the singleton is being re-initialised and all the values are returning null.

Here's the code to inject the 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;
}

}

I know that the RangeFileringParams bean is being recreated, as the ID changes in the debugger, and init filtering is printed to the console.

Is there something wrong with injecting a singleton into a prototype in this way?

thanks.

Upvotes: 0

Views: 1505

Answers (2)

Ananthakrishnan K C
Ananthakrishnan K C

Reputation: 11

You can not auto-wired the singleton bean if you are just creating the prototype bean with the new operator.

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

Below some won't give the auto-wired bean which is injected in ExamplePType class.

  ExamplePType obj = new ExamplePType ();

Create object using Application Context to aware the injected bean to persist the auto-wired 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);        
    }   
}

Upvotes: 1

aussie
aussie

Reputation: 119

As you said it is solved but anyways. If you are creating a bean with initial state i would recommend using configuration instead of just the @Component annotation and then you can create the bean and initialize the fields in one place. For example:

@Configuration
public class RangeFilteringParamsConfiguration {

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

Upvotes: 1

Related Questions