Prototype Scope: A request can be made to the bean instance either programmatically using getBean() method or by XML for Dependency Injection of secondary type. Generally, we use the prototype scope for all beans that are stateful, while the singleton scope is used for the stateless beans.
The prototype scope If the scope is set to prototype, the Spring IoC container creates a new bean instance of the object every time a request for that specific bean is made. As a rule, use the prototype scope for all state-full beans and the singleton scope for stateless beans.
When a spring bean is scoped as a prototype, the Spring IoC container creates new bean instance every time when a request is made for that bean. We can define the scope of a bean as prototype using scope="prototype" attribute of element or using @Scope(value = ConfigurableBeanFactory. SCOPE_PROTOTYPE) annotation.
When you use singleton-scoped beans with dependencies on prototype beans , be aware that dependencies are resolved at instantiation time. Thus if you dependency-inject a prototype-scoped bean into a singleton-scoped bean, a new prototype bean is instantiated and then dependency-injected into the singleton bean.
Scope prototype means that every time you ask spring (getBean or dependency injection) for an instance it will create a new instance and give a reference to that.
In your example a new instance of LoginAction is created and injected into your HomeController . If you have another controller into which you inject LoginAction you will get a different instance.
If you want a different instance for each call - then you need to call getBean each time - injecting into a singleton bean will not achieve that.
Since Spring 2.5 there's a very easy (and elegant) way to achieve that.
You can just change the params proxyMode
and value
of the @Scope
annotation.
With this trick you can avoid to write extra code or to inject the ApplicationContext every time that you need a prototype inside a singleton bean.
Example:
@Service
@Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class LoginAction {}
With the config above LoginAction
(inside HomeController
) is always a prototype even though the controller is a singleton.
Just because the bean injected into the controller is prototype-scoped doesn't mean the controller is!
@controller is a singleton object, and if inject a prototype bean to a singleton class will make the prototype bean also as singleton unless u specify using lookup-method property which actually create a new instance of prototype bean for every call you make.
As mentioned by nicholas.hauschild injecting Spring context is not a good idea. In your case, @Scope("request") is enough to fix it. But let say you need several instances of LoginAction
in controller method. In this case, I would recommend to create the bean of Supplier (Spring 4 solution):
@Bean
public Supplier<LoginAction> loginActionSupplier(LoginAction loginAction){
return () -> loginAction;
}
Then inject it into controller:
@Controller
public class HomeController {
@Autowired
private Supplier<LoginAction> loginActionSupplier;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With