Spring を使って、singletonのbeanからDIされるオブジェクトをprototypeにしたい。

ちょっと考えれば分かることだけど、通常のDIのやり方だと、DI対象のbeanはDI元のbeanよりscopeが小さいか、同じでないとダメ。*1ことを忘れてた。。。

For those of you who are not aware of Method Injection, it allows you to inject methods instead of objects in your class. Method Injection is useful in scenarios where you need to inject a smaller scope bean in a larger scope bean. For example, you have to inject a prototype bean inside an singleton bean , on each method invocation of Singleton bean. Just defining your bean prototype, does not create new instance each time a singleton bean is called because container creates a singleton bean only once, and thus only sets a prototype bean once. So, it is completely wrong to think that if you make your bean prototype you will get new instance each time prototype bean is called.

ただ、要件として、
Springで、singleton(scope小) のbeanからDIされるオブジェクトをprototype(scope大)にしたいといのがある。


その手段として、
*2
がある。



ただ、
指定できるscopeは、singletonやprototypeはダメで、sessionやrequestみたい。singletonはまだしも、prototypeはいけそうな気もするのだが?
*3

You do not need to use the in conjunction with beans that are scoped as singletons or prototypes. It is an error to try to create a scoped proxy for a singleton bean (and the resulting BeanCreationException will certainly set you straight in this regard).

just why do you need this element in the definition of beans scoped at the request, session, globalSession and 'insert your custom scope here' level?

*4をみると、prototypeも大丈夫そうだが。

It seems there is no difference if the bean is of scope prototype or request.

色々探してみたが、prototypeでも、いけそう。
ただ、
この例は、アノテーションベース*5の例。
手元で動作確認してみたので、確かっぽい。

@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")