当前位置: 动力学知识库 > 问答 > 编程问答 >

Avoid duplication using Spring and Java Config beans

问题描述:

I am having in Class A the following beans:

@Bean

public AsyncItemProcessor OneUploadAsyncItemProcessor() {

// ...

asyncItemProcessor.setDelegate(processor(OVERRIDDEN_BY_EXPRESSION, OVERRIDDEN_BY_EXPRESSION));

// ...

return asyncItemProcessor;

}

@Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)

@Bean

public ItemProcessor<MyItem, MyItem> processor(@Value("#{jobParameters[pushMessage]}") String pushMessage, @Value("#{jobParameters[jobId]}") String jobId) {

return new PushItemProcessor(pushMessage, jobId);

}

Now I have in a class B the following:

@Bean

public AsyncItemProcessor TwpUploadAsyncItemProcessor() {

// ...

asyncItemProcessor.setDelegate(processor(OVERRIDDEN_BY_EXPRESSION, OVERRIDDEN_BY_EXPRESSION));

return asyncItemProcessor;

}

How I can Inject into class B the bean processor (which defined on class A) without duplicate it.

网友答案:

You just need to "autowire" it in class B. Something like:

class B {
  @Autowire
  //@Qualifier(value = "OneUploadAsyncItemProcessor")
  [modifier] AsyncItemProcessor OneUploadAsyncItemProcessor;
}

I'm assuming you do not want –and hence going to delete- the bean TwpUploadAsyncItemProcessor from class B and include/autowire the (already defined) bean OneUploadAsyncItemProcessor from class A. If so, the @Qualifier annotation is not needed.

On the other hand if you want to autowire OneUploadAsyncItemProcessor without deleting TwpUploadAsyncItemProcessor and/or define (in the future) another bean of type AsyncItemProcessor you are going to need the @Qualifier annotation.

分享给朋友:
您可能感兴趣的文章:
随机阅读: