I have two beans of the same class defined:
@Bean
@Scope("singleton")
public MyBean myBeanSingleton(){
return new MyBean()
}
@Bean
@Scope("request")
public MyBean myBeanRequest(){
return new MyBean()
}
And another prototype scoped bean:
@Component
@Scope("prototype")
public class MyPrototype {
@Autowired
private MyBean myBean
}
Is it possible for the Spring Application context to inject correct bean automatically when there is no request available?
I would like for the myBeanRequest to be injected when getting MyPrototype bean in a web context (for example in controller) and i would like the myBeanSingleton to be injected when there is no web context (for example when i run some code via the Quartz scheduler).
Currently spring tries to inject the myBeanRequest every time, therefore it fails when there is no request available.
I would very much like to stick with ONE bean definition of MyPrototype because there will be many different beans with will require similar behaviour, therefore using qualifier and two different bean definitions (for request and singleton) is not suitable in my case.
Maybe there is some spring component that would allow me to choose programatically which bean to inject?