In a @Configuration
class, a @Bean
method like so
@Bean
@Scope("prototype")
public Thing thing(String name) {
return new Thing(name);
}
is used to register a bean definition and provide the factory for creating the bean. The bean that it defines is only instantiated upon request using arguments that are determined either directly or through scanning that ApplicationContext
.
In the case of a prototype
bean, a new object is created every time and therefore the corresponding @Bean
method is also executed.
You can retrieve a bean from the ApplicationContext
through its BeanFactory#getBean(String name, Object... args)
method which states
Allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition.
Parameters:
args arguments to use if creating a prototype using explicit arguments to a static factory method. It is invalid to use a non-null args value in any other case.
In other words, for this prototype
scoped bean, you are providing the arguments that will be used, not in the constructor of the bean class, but in the @Bean
method invocation. (This method has very weak type guarantees since it uses a name lookup for the bean.)
Alternatively, you can use the typed BeanFactory#getBean(Class requiredType, Object... args)
method which looks up the bean by type.
This is at least true for Spring versions 4+.
Note that, if you don't want to start with the ApplicationContext
or BeanFactory
for your bean retrieval, you can inject an ObjectProvider
(since Spring 4.3).
A variant of
ObjectFactory
designed specifically for injection points, allowing for programmatic optionality and lenient not-unique handling.
and use its getObject(Object... args)
method
Return an instance (possibly shared or independent) of the object managed by this factory.
Allows for specifying explicit construction arguments, along the lines of
BeanFactory.getBean(String, Object)
.
For example,
@Autowired
private ObjectProvider<Thing> things;
[...]
Thing newThing = things.getObject(name);
[...]