简体   繁体   中英

How to get a prototype bean for a singleton Controller in Spring?

I'm using annotation configuration and I currently cannot use request scope for my controller, but I need one of the bean that controller uses to be a prototype. So I figured the best way would be getting prototypes for a singleton controller via method injection.

But then I realized that Spring's method injection needs an abstract class so that I couldn't use my annotation configuration ...

Could please anybody tell me how to do that ? It seems to me that it is very common scenario, but currently it can be realized only via "request scope" of controller.

Or I'd have to make my controller ApplicationContextAware and get the bean from context. But can annotation-config @Controller be ApplicationContextAware ?

You can simply @Inject ApplicationContext ctx , but you need your other bean to be defined in the child context ( dispatcher-servlet.xml ) (you need that anyway). And then you can look it up.

There is no way to define lookup-method injection with annotations currently. There is an open issue about that . So for this particular controller you can use xml configuration to define the lookup-method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM